-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfactory.go
65 lines (51 loc) · 1.05 KB
/
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
package factory
import (
"fmt"
)
const (
ItalianType = 1
)
const (
FerrariModel = 1
CarWithFiveWheelModel = 2
)
type Vehicle interface {
NumOfWheels() int
GetModelName() string
}
type VehicleFactory interface {
Build(v int) (Vehicle, error)
}
type ItalianFactory struct{}
type CarWithFiveWheelType struct{}
func (f *CarWithFiveWheelType) NumOfWheels() int {
return 5
}
func (f *CarWithFiveWheelType) GetModelName() string {
return "Star"
}
type FerrariModelType struct {
}
func (f *FerrariModelType) NumOfWheels() int {
return 4
}
func (f *FerrariModelType) GetModelName() string {
return "Ferrari"
}
func (i *ItalianFactory) Build(v int) (Vehicle, error) {
switch v {
case FerrariModel:
return new(FerrariModelType), nil
case CarWithFiveWheelModel:
return new(CarWithFiveWheelType), nil
}
return nil, fmt.Errorf("No Italian cars of type %d\n", v)
}
func BuildFactory(f int) (VehicleFactory, error) {
switch f {
case ItalianType:
return new(ItalianFactory), nil
default:
return nil, fmt.Errorf("No factory with id %d\n", f)
}
}