File tree Expand file tree Collapse file tree 5 files changed +50
-115
lines changed Expand file tree Collapse file tree 5 files changed +50
-115
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ A curated collection of idiomatic design & application patterns for Go language.
18
18
| [ Abstract Factory] ( /creational/abstract_factory.md ) | Provides an interface for creating families of releated objects | ✘ |
19
19
| [ Builder] ( /creational/builder.md ) | Builds a complex object using simple objects | ✘ |
20
20
| [ Factory Method] ( /creational/factory.md ) | Defers instantiation of an object to a specialized function for creating instances | ✘ |
21
- | [ Object Pool] ( /creational/object_pool .md ) | Instantiates and maintains a group of objects instances of the same type | ✔ |
21
+ | [ Object Pool] ( /creational/object-pool .md ) | Instantiates and maintains a group of objects instances of the same type | ✔ |
22
22
| [ Singleton] ( /creational/singleton.md ) | Restricts instantiation of a type to one object | ✔ |
23
23
24
24
## Structural Patterns
Original file line number Diff line number Diff line change 5
5
* [ Abstract Factory] ( /creational/abstract_factory.md )
6
6
* [ Builder] ( /creational/builder.md )
7
7
* [ Factory Method] ( /creational/factory.md )
8
- * [ Object Pool] ( /creational/object_pool .md )
8
+ * [ Object Pool] ( /creational/object-pool .md )
9
9
* [ Singleton] ( /creational/singleton.md )
10
10
* [ Structural Patterns] ( /README.md#structural-patterns )
11
11
* [ Adapter] ( /structural/adapter.md )
Original file line number Diff line number Diff line change
1
+ # Object Pool Pattern
2
+
3
+ The object pool creational design pattern is used to prepare and keep multiple
4
+ instances according to the demand expectation.
5
+
6
+ ## Implementation
7
+
8
+ ``` go
9
+ package pool
10
+
11
+ type Pool chan *Object
12
+
13
+ func New (total int ) *Pool {
14
+ p := make (chan *Object, total)
15
+
16
+ for i := 0 ; i < total; i++ {
17
+ p <- new (Object)
18
+ }
19
+
20
+ return p
21
+ }
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Given below is a simple lifecycle example on an object pool.
27
+
28
+ ``` go
29
+ p := pool.New (2 )
30
+
31
+ select {
32
+ case obj := <- p:
33
+ obj.Do ( /* ...*/ )
34
+
35
+ p <- obj
36
+ default :
37
+ // No more objects left retry later or fail
38
+ return
39
+ }
40
+ ```
41
+
42
+ ## Rules of Thumb
43
+
44
+ - Object pool pattern is useful in cases where object initialization is more
45
+ expensive than the object maintenance.
46
+ - If there are spikes in demand as opposed to a steady demand, the maintenance
47
+ overhead might overweigh the benefits of an object pool.
48
+ - It has positive effects on performance due to object being initialized beforehand.
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments