forked from woshihuo12/LuaDesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator.lua
65 lines (46 loc) · 889 Bytes
/
decorator.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
Person = {}
function Person:new(o)
o = o or {}
setmetatable(o,self)
self.__index = self
return o
end
function Person:Show()
print("i am person")
end
Decorator = Person:new{component = nil}
function Decorator:Decorate(com)
self.component = com
end
function Decorator:Show()
print("i am decorator")
end
Shirt = Decorator:new()
function Shirt:Show()
print("i am shirt")
if self.component ~= nil then
self.component:Show()
end
end
Trouser = Decorator:new()
function Trouser:Show()
print("i am Trouser")
if self.component ~= nil then
self.component:Show()
end
end
Shoe = Decorator:new()
function Shoe:Show()
print("i am Shoe")
if self.component ~= nil then
self.component:Show()
end
end
person = Person:new()
shirt = Shirt:new()
shirt:Decorate(person)
trouser = Trouser:new()
trouser:Decorate(shirt)
shoe = Shoe:new()
shoe:Decorate(trouser)
shoe:Show()