-
Notifications
You must be signed in to change notification settings - Fork 246
/
abstract_factory.go
70 lines (57 loc) · 1.83 KB
/
abstract_factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package abstract_factory is an example of the Abstract Factory Pattern.
package abstract_factory
// AbstractFactory provides an interface for creating families of related objects.
type AbstractFactory interface {
CreateWater(volume float64) AbstractWater
CreateBottle(volume float64) AbstractBottle
}
// AbstractWater provides a water interface.
type AbstractWater interface {
GetVolume() float64
}
// AbstractBottle provides a bottle interface.
type AbstractBottle interface {
PourWater(water AbstractWater) // Bottle interacts with a water.
GetBottleVolume() float64
GetWaterVolume() float64
}
// CocaColaFactory implements AbstractFactory interface.
type CocaColaFactory struct {
}
// NewCocaColaFactory is the CocaColaFactory constructor.
func NewCocaColaFactory() AbstractFactory {
return &CocaColaFactory{}
}
// CreateWater implementation.
func (f *CocaColaFactory) CreateWater(volume float64) AbstractWater {
return &CocaColaWater{volume: volume}
}
// CreateBottle implementation.
func (f *CocaColaFactory) CreateBottle(volume float64) AbstractBottle {
return &CocaColaBottle{volume: volume}
}
// CocaColaWater implements AbstractWater.
type CocaColaWater struct {
volume float64 // Volume of drink.
}
// GetVolume returns volume of drink.
func (w *CocaColaWater) GetVolume() float64 {
return w.volume
}
// CocaColaBottle implements AbstractBottle.
type CocaColaBottle struct {
water AbstractWater // Bottle must contain a drink.
volume float64 // Volume of bottle.
}
// PourWater pours water into a bottle.
func (b *CocaColaBottle) PourWater(water AbstractWater) {
b.water = water
}
// GetBottleVolume returns volume of bottle.
func (b *CocaColaBottle) GetBottleVolume() float64 {
return b.volume
}
// GetWaterVolume returns volume of water.
func (b *CocaColaBottle) GetWaterVolume() float64 {
return b.water.GetVolume()
}