-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure.go
58 lines (44 loc) · 1.29 KB
/
structure.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
package factory
import "fmt"
// Product declares the interface, which is common to all objects that can be produced by the creator and its subclasses.
type Product interface {
doStuff() string
}
// ConcreteProductA Concrete Products are different implementations of the prod- uct interface.
type ConcreteProductA struct {
}
func (p *ConcreteProductA) doStuff() string {
fmt.Printf("ConcreteProductA.doStuff()\n")
return "ConcreteProductA"
}
type ConcreteProductB struct {
}
func (p *ConcreteProductB) doStuff() string {
fmt.Printf("ConcreteProductB.doStuff()\n")
return "ConcreteProductB"
}
// The Creator class declares the factory method that returns new product objects. It’s important that the return type of this method matches the product interface.
type Creator struct {
create ProductCreator
}
type ProductCreator interface {
createProduct() Product
}
func (c *Creator) someOperation() string {
fmt.Printf("Creator.someOperation()\n")
p := c.createProduct()
return p.doStuff()
}
func (c *Creator) createProduct() Product {
return c.create.createProduct()
}
type ConcreteCreatorA struct {
}
func (c *ConcreteCreatorA) createProduct() Product {
return &ConcreteProductA{}
}
type ConcreteCreatorB struct {
}
func (c *ConcreteCreatorB) createProduct() Product {
return &ConcreteProductB{}
}