--[[ 橋接模式:將抽象部分與它的實現部分分離,使它們都可以獨立地變化。 其實橋接模式在開發應用中使用的非常廣泛,甚至即使我們沒有學過橋接模式,也可能會潛意識地使用到。 例如:在游戲開發中,玩家類(Role)合成了背包(Knapsack);在背包類(Knapsack)中,又合成的物品(Item);其實這種合成就是橋接模式中的”橋“。注意的是這裡是合成,而不是繼承! 下面以手機為例 ]]-- System = {} function System:new(n) o = {} setmetatable(o,self) self.__index = self o.phonename = n return o; end function System:GetSystem() if self.phonename == "Iphone5s" then return "IOS 7" elseif self.phonename == "Lumia1020" then return "WP 8" end end CPU = {} function CPU:new(n) o = {} setmetatable(o,self) self.__index = self o.phonename = n return o; end function CPU:GetCPU() if self.phonename == "Iphone5s" then return "蘋果 A7/M7協處理器" elseif self.phonename == "Lumia1020" then return "高通 Adreno 225" end end Memory = {} function Memory:new(n) o = {} setmetatable(o,self) self.__index = self o.phonename = n return o; end function Memory:GetMemory() if self.phonename == "Iphone5s" then return "RAM容量:1GB " elseif self.phonename == "Lumia1020" then return "RAM容量:2GB" end end Cellphone = {} function Cellphone:new(n,o) o = o or {} setmetatable(o,self) self.__index = self self.system = System:new(n) self.cpu = CPU:new(n) self.memory = Memory:new(n) self.phonename = n return o; end function Cellphone:ShowDetail() print(self.phonename) print(self.system:GetSystem()) print(self.cpu:GetCPU()) print(self.memory:GetMemory()) end Iphone5s = Cellphone:new() function Iphone5s:Description() print("就三個字,'土豪金'") end Lumia1020 = Cellphone:new() function Lumia1020:Description() print("最好的相機手機,諾基亞,質量的保證") end phone = Iphone5s:new("Iphone5s") phone:Description() phone:ShowDetail() phone = Lumia1020:new("Lumia1020") phone:Description() phone:ShowDetail()