mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-25 13:16:02 +03:00
add objectpool model
This commit is contained in:
parent
e8318e8b01
commit
d4c3346164
@ -4,7 +4,6 @@
|
||||
|
||||
如果抽象工厂退化成生成的对象无关联的或者单一的产品种类则成为工厂函数模式。
|
||||
|
||||
|
||||
参考:[对比](https://blog.csdn.net/wyxhd2008/article/details/5597975)
|
||||
|
||||
![对比图片](../../images/abstract-factorys-method.png)
|
||||
|
3
creation/24_object_pool/README.md
Normal file
3
creation/24_object_pool/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Object Pool Pattern
|
||||
|
||||
对象池创建模式,用于一次性准备大量的预置对象
|
23
creation/24_object_pool/obejct_poo.go
Normal file
23
creation/24_object_pool/obejct_poo.go
Normal 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() {
|
||||
|
||||
}
|
20
creation/24_object_pool/object_pool_test.go
Normal file
20
creation/24_object_pool/object_pool_test.go
Normal 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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user