装饰模式lua实现

来源:互联网 发布:ubuntu14.04安装mysql 编辑:程序博客网 时间:2024/04/20 01:28
Person = {}Decorator = {}function Person:new(o)o = o or {}setmetatable(o,self)self.__index = selfreturn o;endfunction Person:Show()print("我是人")endDecorator = Person:new{component = nil}function Decorator:Decorate(com)self.component = comendfunction Decorator:Show()print("我是装饰者")endShirt = Decorator:new()function Shirt:Show()print("最后衬衫")if self.component ~= nil thenself.component:Show()endendTrouser = Decorator:new()function Trouser:Show()print("然后裤子")if self.component ~= nil thenself.component:Show()endendShoe = Decorator:new()function Shoe:Show()print("先穿鞋子")if self.component ~= nil thenself.component:Show()endendperson = Person:new()shirt = Shirt:new()shirt:Decorate(person)trouser = Trouser:new()trouser:Decorate(shirt)shoe = Shoe:new()shoe:Decorate(trouser)shoe:Show()

代码输出:

先穿鞋子
然后裤子
最后衬衫
我是人

原创粉丝点击