diff --git a/creation/05_abstract_factory/README.md b/creation/05_abstract_factory/README.md index 96fd58b..9b39e34 100644 --- a/creation/05_abstract_factory/README.md +++ b/creation/05_abstract_factory/README.md @@ -4,7 +4,6 @@ 如果抽象工厂退化成生成的对象无关联的或者单一的产品种类则成为工厂函数模式。 - 参考:[对比](https://blog.csdn.net/wyxhd2008/article/details/5597975) ![对比图片](../../images/abstract-factorys-method.png) diff --git a/creation/24_object_pool/README.md b/creation/24_object_pool/README.md new file mode 100644 index 0000000..dc28c10 --- /dev/null +++ b/creation/24_object_pool/README.md @@ -0,0 +1,3 @@ +# Object Pool Pattern + +对象池创建模式,用于一次性准备大量的预置对象 diff --git a/creation/24_object_pool/obejct_poo.go b/creation/24_object_pool/obejct_poo.go new file mode 100644 index 0000000..15c9dc8 --- /dev/null +++ b/creation/24_object_pool/obejct_poo.go @@ -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() { + +} diff --git a/creation/24_object_pool/object_pool_test.go b/creation/24_object_pool/object_pool_test.go new file mode 100644 index 0000000..2e7baa6 --- /dev/null +++ b/creation/24_object_pool/object_pool_test.go @@ -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 + } +}