add objectpool model

This commit is contained in:
Edward 2020-04-24 18:09:58 +08:00
parent e8318e8b01
commit d4c3346164
4 changed files with 46 additions and 1 deletions

View File

@ -4,7 +4,6 @@
如果抽象工厂退化成生成的对象无关联的或者单一的产品种类则成为工厂函数模式。 如果抽象工厂退化成生成的对象无关联的或者单一的产品种类则成为工厂函数模式。
参考:[对比](https://blog.csdn.net/wyxhd2008/article/details/5597975) 参考:[对比](https://blog.csdn.net/wyxhd2008/article/details/5597975)
![对比图片](../../images/abstract-factorys-method.png) ![对比图片](../../images/abstract-factorys-method.png)

View File

@ -0,0 +1,3 @@
# Object Pool Pattern
对象池创建模式,用于一次性准备大量的预置对象

View File

@ -0,0 +1,23 @@
package objectpool
type doctor struct {
name string
kind int //科室
}
type pool chan *doctor
func newPool(total int) pool {
p := make(pool, total)
for i := 0; i < total; i++ {
p <- new(doctor)
}
return p
}
//surgery
func (d doctor) surgery() {
}

View File

@ -0,0 +1,20 @@
package objectpool
import (
"testing"
)
func TestObjectPool(t *testing.T) {
p := newPool(2)
select {
case obj := <-p:
obj.surgery( /*...*/ )
p <- obj
default:
// No more objects left — retry later or fail
return
}
}