《23种设计模式 Lua篇》 - 中介者模式

来源:互联网 发布:ajax jquery json data 编辑:程序博客网 时间:2024/04/30 01:48

北漂生涯 房源都掌握在房屋中介手上,租房的只能通过中介去联系房东。 PS:北京的租房中介费是一个月房租啊~ 坑爹啊 有木有


一、Custom.lua 客户基类 房东和租房者 准确说都是中介的客户

--/**************************************************************************--    Copyright:    www.SaintGrail.com--    Author:     liu jing---   Date:     2014-08-27---   Description:  客户基类---**************************************************************************/Custom={}-- 客户名称Custom.name=nil-- 与客户打交道的房屋中介Custom.mediator=nil     function Custom:new(_o)    _o = _o or {}    setmetatable(_o,self)    self.__index=self    return _oendfunction Custom:Notify(_str)    self.mediator:Notify(_str,self)endfunction Custom:Say(_str)    print(self.name.."  get message :".._str)end

二、Mediator.lua  房屋中介 (掌握着双方的信息)

--/**************************************************************************--    Copyright:    www.SaintGrail.com--    Author:     liu jing---   Date:     2014-08-27---   Description:  中介者---**************************************************************************/-- 中介Mediator={}-- 房东Mediator.landlord=nil-- 租房者Mediator.tenant=nilfunction Mediator:new(_o)    _o = _o or {}    setmetatable(_o,self)    self.__index = self    return _oendfunction Mediator:Notify(_str,_obj)    if _obj==self.landlord then        self.tenant:Say(_str)    else        self.landlord:Say(_str)    endend

三、 mian.lua 主函数

require  ("Mediator")require  ("Custom") local function main()    -- 房地产 中介    local mediator = Mediator:new()       -- 房东    local landlord = Custom:new()    landlord.mediator=mediator    landlord.name = "fangdong"    -- 租房客    local tenant = Custom:new()    tenant.mediator=mediator    tenant.name = "fangke"        mediator.landlord=landlord    mediator.tenant=tenant    tenant:Notify("    you fang zi ma?")    landlord:Notify("    mei fang zi")endmain()

四、输出结果如下

Debugger v1.1.0Debugger: Trying to connect to 127.0.0.1:10000 ... Debugger: Connection succeed.fangdong  get message :    you fang zi ma?fangke  get message :    mei fang zi



0 0
原创粉丝点击