装飾モード--lua実現
1172 ワード
Person = {}
Decorator = {}
function Person:new(o)
o = o or {}
setmetatable(o,self)
self.__index = self
return o;
end
function Person:Show()
print(" ")
end
Decorator = Person:new{component = nil}
function Decorator:Decorate(com)
self.component = com
end
function Decorator:Show()
print(" ")
end
Shirt = Decorator:new()
function Shirt:Show()
print(" ")
if self.component ~= nil then
self.component:Show()
end
end
Trouser = Decorator:new()
function Trouser:Show()
print(" ")
if self.component ~= nil then
self.component:Show()
end
end
Shoe = Decorator:new()
function Shoe:Show()
print(" ")
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()
コード出力:
まず靴を履いてズボンの最後のシャツは私は人です.