Skip to content

Commit 6a717fe

Browse files
committed
creational/object-pool: reimplement the object pool pattern
1 parent cf0e410 commit 6a717fe

File tree

5 files changed

+50
-115
lines changed

5 files changed

+50
-115
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ A curated collection of idiomatic design & application patterns for Go language.
1818
| [Abstract Factory](/creational/abstract_factory.md) | Provides an interface for creating families of releated objects ||
1919
| [Builder](/creational/builder.md) | Builds a complex object using simple objects ||
2020
| [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 ||
2222
| [Singleton](/creational/singleton.md) | Restricts instantiation of a type to one object ||
2323

2424
## Structural Patterns

SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* [Abstract Factory](/creational/abstract_factory.md)
66
* [Builder](/creational/builder.md)
77
* [Factory Method](/creational/factory.md)
8-
* [Object Pool](/creational/object_pool.md)
8+
* [Object Pool](/creational/object-pool.md)
99
* [Singleton](/creational/singleton.md)
1010
* [Structural Patterns](/README.md#structural-patterns)
1111
* [Adapter](/structural/adapter.md)

creational/object-pool.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.

creational/object_pool.go

Lines changed: 0 additions & 106 deletions
This file was deleted.

creational/object_pool.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)