Skip to content

Commit bfc0dc1

Browse files
committed
add usage paraprah
1 parent ebcd771 commit bfc0dc1

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Go Design Patterns
22

33
## [Creational](creational)
4-
4+
55
* [Abstract Factory method](creational/abstract_factory) [:notebook:](http://en.wikipedia.org/wiki/Abstract_Factory_pattern)
66
* [Builder](creational/builder) [:notebook:](http://en.wikipedia.org/wiki/Builder_pattern)
77
* [Factory method](creational/factory) [:notebook:](http://en.wikipedia.org/wiki/Factory_pattern)

creational/abstract_factory/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,26 @@ func TestFoo(t *testing.T) {
2121
t.Logf("%v car has %d wheels", car.GetModelName(), car.NumOfWheels())
2222
}
2323
```
24+
25+
## Usage
26+
27+
BuildFactory provide a factory of italian cars. Then italian builder build a particular model of car. This model must have 4 wheels.
28+
29+
```go
30+
fca, _ := BuildFactory(ItalianType)
31+
m, _ := fca.Build(FerrariModel)
32+
car, err := m.(Vehicle)
33+
if model.NumOfWheels() != 4 {
34+
panic("Ferrari shoud have 4 wheels")
35+
}
36+
```
37+
38+
BuildFactory provide a factory of italian cars. Then italian builder build a particular model of car. This model must have 5 wheels.
39+
40+
```go
41+
fca, _ := BuildFactory(ItalianType)
42+
model, _ := fca.Build(CarWithFiveWheelModel)
43+
if model.NumOfWheels() != 5 {
44+
panic("the car should have 5 wheels")
45+
}
46+
```

creational/builder/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,25 @@ func (c *BiciclettaBuilder) Build() Product {
110110
return c.p
111111
}
112112
```
113+
114+
## Usage
115+
116+
The director must build a vehicle like a car. Once the builder is provided. The director ask the builder to build the car. Finally we'll have right model of car.
117+
118+
```go
119+
director := ManufacturingDirector{}
120+
121+
carBuilder := &CarBuilder{}
122+
director.SetBuilder(carBuilder)
123+
director.Construct()
124+
car := carBuilder.Build()
125+
```
126+
127+
The director must build a vehicle like a bike. Once the builder is provided. The director ask the builder to build the bike. Finally we'll have right model of bike.
128+
129+
```go
130+
biciclettaBuilder := &BiciclettaBuilder{}
131+
director.SetBuilder(biciclettaBuilder)
132+
director.Construct()
133+
bike := biciclettaBuilder.Build()
134+
```

creational/pool/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,20 @@ func (p *Pool) Receive(object PoolObject) {
7373
}
7474
}
7575
```
76+
77+
## Usage
78+
79+
First, the pool provide two object. Second, the pool receive one used object. Third, another loan is requested but instead of provide another instance of new object, thirdObject contains firstObject. No time is spent to build another object.
80+
81+
```go
82+
pool := InitPool()
83+
firstObject := pool.Loan()
84+
secondObject := pool.Loan()
85+
86+
pool.Receive(firstObject)
87+
thirdObject := pool.Loan()
88+
89+
if firstObject.id != thirdObject.id {
90+
panic("thir object must contain firs one")
91+
}
92+
```

creational/prototype/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,16 @@ if thirdInstance.NumberOfCalls() != 3 {
2525
## Few words …
2626

2727
Is not good pattern if used to bring the state of the applicatoin and can cnange during its lifecycle. Making something global to avoid passing it around is a code smell. But use it to read configuration is good. Used to load a resource just first time is requested and to provide that resource everywere is a good way to use this pattern.
28+
29+
## Usage
30+
31+
Two kind of t-shirt are needed. Same model, with different SKU. Instead of recreeate same white shirt from scratch, base model is cloned.
32+
33+
```go
34+
shirtCache := GetShirtsCloner()
35+
firstItem, err := shirtCache.GetClone(White)
36+
firstItem.SKU = "abc"
37+
38+
secondItem, err := shirtCache.GetClone(White)
39+
secondItem.SKU = "xxx"
40+
```

0 commit comments

Comments
 (0)