Skip to content

Commit 170191a

Browse files
committed
limit the number of objects available
1 parent 74dec87 commit 170191a

File tree

4 files changed

+72
-10
lines changed

4 files changed

+72
-10
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88
* [Object Pool](creational/pool) [:notebook:](http://en.wikipedia.org/wiki/Object_Pool_pattern)
99
* [Prototype](creational/prototype) [:notebook:](http://en.wikipedia.org/wiki/Prototype_pattern)
1010
* [Singleton](creational/singleton) [:notebook:](http://en.wikipedia.org/wiki/Singleton_pattern) (is considered an anti-pattern! :no_entry:)
11+
12+
## [Structural](structural)
13+
14+
* [Composite](structural/composite) [:notebook:](http://en.wikipedia.org/wiki/Composite_pattern)

creational/pool/pool.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package pool
22

33
import (
44
"container/list"
5+
"errors"
6+
"log"
7+
"strconv"
58
)
69

710
type PoolObject struct {
@@ -21,19 +24,24 @@ func InitPool() Pool {
2124
return *pool
2225
}
2326

24-
func (p *Pool) Loan() PoolObject {
27+
func (p *Pool) Loan() (*PoolObject, error) {
2528
if p.idle.Len() > 0 {
2629
for e, i := p.idle.Front(), 0; e != nil; e, i = e.Next(), i+1 {
2730
if i == 0 {
2831
object := e.Value.(PoolObject)
29-
return object
32+
return &object, nil
3033
}
3134
}
3235
}
3336

37+
log.Println(strconv.Itoa(p.active.Len()))
38+
if p.NumberOfObjectsInPool() >= 3 {
39+
return nil, errors.New("...")
40+
}
41+
3442
object := PoolObject{p.NumberOfObjectsInPool() + 1}
3543
p.active.PushBack(object)
36-
return object
44+
return &object, nil
3745
}
3846

3947
func (p *Pool) Receive(object PoolObject) {

creational/pool/pool_test.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestWhenObjectIsNeededItIsRequestedFromThePool(t *testing.T) {
1818
t.Fatal("There shouldnt be more than zero object in pool")
1919
}
2020

21-
objectOne := pool.Loan()
21+
objectOne, _ := pool.Loan()
2222
if pool.NumberOfActiveObjects() != 1 {
2323
t.Fatal("Something went wrong")
2424
}
@@ -31,15 +31,15 @@ func TestWhenObjectIsNeededItIsRequestedFromThePool(t *testing.T) {
3131
t.Fatal("object id should be 1")
3232
}
3333

34-
pool.Receive(objectOne)
34+
pool.Receive(*objectOne)
3535
if pool.NumberOfActiveObjects() != 0 {
3636
t.Fatal("There should not be active object")
3737
}
3838
if pool.NumberOfIdleObjects() != 1 {
3939
t.Fatal("Actually there should be zero idle objects in pool")
4040
}
4141

42-
secondCall := pool.Loan()
42+
secondCall, _ := pool.Loan()
4343
if secondCall.id != createdId {
4444
t.Fatal(strings.Join([]string{
4545
"Pool should return same object: we now have ",
@@ -56,16 +56,31 @@ func TestWhenObjectIsNeededItIsRequestedFromThePool(t *testing.T) {
5656
func TestPool(t *testing.T) {
5757
pool := InitPool()
5858

59-
_ = pool.Loan()
60-
foo := pool.Loan()
61-
_ = pool.Loan()
59+
_, _ = pool.Loan()
60+
foo, _ := pool.Loan()
61+
_, _ = pool.Loan()
6262

6363
if pool.NumberOfActiveObjects() != 3 {
6464
t.Fatal("Actually there should be zero idle objects in pool")
6565
}
6666

67-
pool.Receive(foo)
67+
pool.Receive(*foo)
6868
if pool.NumberOfActiveObjects() != 2 {
6969
t.Fatal("Actually there should be zero idle objects in pool")
7070
}
7171
}
72+
73+
func TestPoolLimitNumberOfAvailableObject(t *testing.T) {
74+
pool := InitPool()
75+
_, _ = pool.Loan()
76+
_, _ = pool.Loan()
77+
_, err := pool.Loan()
78+
if err != nil {
79+
t.Fatal("There should be one more available object in pool")
80+
}
81+
82+
_, err = pool.Loan()
83+
if err == nil {
84+
t.Fatal("Current pool should manage only four object")
85+
}
86+
}

structural/composite/composite.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type Person struct{ name string }
9+
10+
type Swimmer struct{}
11+
12+
func (s *Swimmer) Swim(name string) {
13+
fmt.Println(strings.Join([]string{
14+
name,
15+
" is swimming",
16+
}, ""))
17+
}
18+
19+
type IronMan struct {
20+
person Person
21+
swimmer Swimmer
22+
}
23+
24+
func (i *IronMan) Swim() {
25+
i.swimmer.Swim(i.person.name)
26+
}
27+
28+
func main() {
29+
ironMan := IronMan{
30+
person: Person{"Mariottide"},
31+
swimmer: Swimmer{},
32+
}
33+
34+
ironMan.Swim()
35+
}

0 commit comments

Comments
 (0)