-
Notifications
You must be signed in to change notification settings - Fork 140
/
factory_method.go
63 lines (53 loc) · 1.25 KB
/
factory_method.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
package factory_method
import "fmt"
/*
更灵活的创建对象的方法
The Factory Method pattern is a design pattern used to define a runtime interface for creating an object. It’s called a factory because it creates various types of objects without necessarily knowing what kind of object it creates or how to create it.
Purpose
Allows the sub-classes to choose the type of objects to create at runtime
It provides a simple way of extending the family of objects with minor changes in application code.
Promotes the loose-coupling by eliminating the need to bind application-specific structs into the code
*/
type IGun interface {
getName() string
getPower() int
}
type Gun struct {
name string
power int
}
func (g *Gun) getName() string {
return g.name
}
func (g *Gun) getPower() int {
return g.power
}
type ak47 struct {
Gun
}
type m16 struct {
Gun
}
func GetGun(gunType string) (IGun, error) {
switch gunType {
case "ak47":
return &ak47{Gun{
name: "ak47",
power: 9,
}}, nil
case "m16":
return &m16{Gun{
name: "m16",
power: 10,
}}, nil
default:
return &ak47{Gun{
name: "ak47",
power: 9,
}}, nil
}
}
func PrintDetails(g IGun) {
fmt.Printf("Gun: %s \n", g.getName())
fmt.Printf("Power: %d \n", g.getPower())
}