-
Notifications
You must be signed in to change notification settings - Fork 33
/
builder.lua
112 lines (90 loc) · 2.11 KB
/
builder.lua
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
--[[
将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示,这样的模式被称为建造者模式
1 当创建复杂对象的算法应该独立于该对象的组成部分以及他们的装配方式时
2 当构造过程必须允许被构造的对象有不同表示时
]]
Director = {}
function Director:new()
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Director:Construct(builder)
if builder ~= nil then
builder:Coating()
builder:Engine()
builder:Radar()
end
end
Builder = {}
function Builder:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
Product = {}
function Product:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Product:Show()
print(self.unit)
end
F22Builder = Builder:new()
function F22Builder:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o.product = Product:new()
return o
end
function F22Builder:Coating()
if self.product.unit == nil then
self.product.unit = "F22:"
end
self.product.unit = self.product.unit .. " 隐形涂料"
end
function F22Builder:Engine()
self.product.unit = self.product.unit .. " 2台 PW发动机"
end
function F22Builder:Radar()
self.product.unit = self.product.unit .. " 77雷达"
end
function F22Builder:GetProduct()
return self.product
end
J10Builder = Builder:new()
function J10Builder:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o.product = Product:new()
return o
end
function J10Builder:Coating()
if self.product.unit == nil then
self.product.unit = "J10:"
end
self.product.unit = self.product.unit .. " sss形涂料"
end
function J10Builder:Engine()
self.product.unit = self.product.unit .. " 1台 PW发动机"
end
function J10Builder:Radar()
self.product.unit = self.product.unit .. " 66雷达"
end
function J10Builder:GetProduct()
return self.product
end
theF22Builder = F22Builder:new()
Director:Construct(theF22Builder)
F22 = theF22Builder:GetProduct()
F22:Show()
j10Builder = J10Builder:new()
Director:Construct(j10Builder)
J10 = j10Builder:GetProduct()
J10:Show()