设计模式实例(Lua)笔记之一(Factory Method工厂方法模式)
来源:互联网 发布:巨拟叶螽淘宝 编辑:程序博客网 时间:2024/06/11 18:14
1.描述
女娲补天的故事大家都听说过吧,今天不说这个,说女娲创造人的故事,可不是“造人”的工作,这个词被现代人滥用了。这个故事是说,女娲在补了天后,下到凡间一看,哇塞,风景太优美了,天空是湛蓝的,水是清澈的,空气是清新的,太美丽了,然后就待时间长了就有点寂寞了,没有动物,这些看的到都是静态的东西呀,怎么办?别忘了是神仙呀,没有办不到的事情,于是女娲就架起了八卦炉(技术术语:建立工厂)开始创建人,具体过程是这样的:先是泥巴捏,然后放八卦炉里烤,再扔到地上成长,但是意外总是会产生的:第一次烤泥人,兹兹兹兹~~,感觉应该熟了,往地上一扔,biu~,一个白人诞生了,没烤熟!第二次烤泥人,兹兹兹兹兹兹兹兹~~,上次都没烤熟,这次多烤会儿,往地上一扔,嘿,熟过头了,黑人哪!第三次烤泥人,兹~兹~兹~,一边烤一边看着,嘿,正正好,Perfect!优品,黄色人类!这个过程还是比较有意思的,先看看类图:
2.代码:
-- 加载文件require "class"-------------------------------- 产品-------------------------------- 人类IHuman = class()function IHuman:Laugh()print("IHuman Laugh")endfunction IHuman:Cry()print("IHuman Cry")endfunction IHuman:Talk()print("IHuman Talk")end-- 黄种人YellowHuman = class(IHuman)function YellowHuman:Laugh()print("黄色人种会哭")endfunction YellowHuman:Cry()print("黄色人种会大笑,幸福呀")endfunction YellowHuman:Talk()print("黄色人种会说话,一般说的都是双字节")end-- 白种人WhiteHuman = class(IHuman)function WhiteHuman:Laugh()print("白色人种会哭")endfunction WhiteHuman:Cry()print("白色人种会大笑,侵略的笑声")endfunction WhiteHuman:Talk()print("白色人种会说话,一般都是单字节")end-- 黑种人BlackHuman = class(IHuman)function BlackHuman:Laugh()print("黑人会哭")endfunction BlackHuman:Cry()print("黑人会笑")endfunction BlackHuman:Talk()print("黑人可以说话,一般人听不懂")end----------------- 工厂---------------IHumanFactory = class()function IHumanFactory:CreateHuman()print("IHumanFactory")end-- 创造黄种人工厂类YellowHumanFactory = class(IHumanFactory)function YellowHumanFactory:CreateHuman()return YellowHuman:new()end-- 创造白种人工厂类WhiteHumanFactory = class(IHumanFactory)function WhiteHumanFactory:CreateHuman()return WhiteHuman:new()end-- 创造黑种人工厂类BlackHumanFactory = class(IHumanFactory)function BlackHumanFactory:CreateHuman()return BlackHuman:new()end-- 测试代码function DoFactoryMethod(factory)if factory == nil then return endIHuman = factory:CreateHuman()IHuman:Cry()IHuman:Laugh()IHuman:Talk()end--- main ---function main()print("----------第一批人是这样的:黄种人工厂来生产黄种人")DoFactoryMethod(YellowHumanFactory:new())print("----------第二批人是这样的:白种人工厂来生产白种人")DoFactoryMethod(WhiteHumanFactory:new())print("----------第一批人是这样的:黑种人工厂来生产黑种人")DoFactoryMethod(BlackHumanFactory:new())endmain()
运行结果如下 :
注释:
class.lualocal _class={}function class(super)local class_type={}class_type.ctor=falseclass_type.super=superclass_type.new=function(...)local obj={}dolocal createcreate = function(c,...)if c.super thencreate(c.super,...)endif c.ctor thenc.ctor(obj,...)endendcreate(class_type,...)endsetmetatable(obj,{ __index=_class[class_type] })return objendlocal vtbl={}_class[class_type]=vtblsetmetatable(class_type,{__newindex=function(t,k,v)vtbl[k]=vend})if super thensetmetatable(vtbl,{__index=function(t,k)local ret=_class[super][k]vtbl[k]=retreturn retend})endreturn class_typeend
1 0
- 设计模式实例(Lua)笔记之一(Factory Method工厂方法模式)
- 设计模式之一 工厂方法模式(Factory Method)
- 设计模式(c++)笔记之一(Factory Method工厂方法模式)
- 设计模式之一---工厂方法模式【Factory Method模式】
- 设计模式之一---工厂方法模式【Factory Method模式】
- 设计模式之一---工厂方法模式【Factory Method模式】
- 设计模式之一---工厂方法模式【Factory Method模式】
- 设计模式之一---工厂方法模式【Factory Method模式】
- 设计模式之一---工厂方法模式【Factory Method模式】
- 设计模式学习笔记四:工厂方法(Factory Method)
- 设计模式-工厂方法(Factory Method)
- 工厂方法(Factory Method)设计模式
- java设计模式之一工厂模式(Factory Method)
- 设计模式之一---工厂方法模式【Factory Method模式】 (java版)
- 设计模式笔记之工厂模式--工厂方法模式(Factory Method)
- 设计模式 一 工厂方法Factory Method(工厂模式)
- 设计模式-工厂方法模式(factory method)
- 设计模式-工厂方法模式(Factory Method)
- PackageManager详解
- 花生壳路由器如何设置
- 遍历Map的四种方法
- iOS Parse教程——如何使用Parse在iOS应用创建后台服务
- 开启/关闭 iOS 原生 左滑动 pop 视图功能
- 设计模式实例(Lua)笔记之一(Factory Method工厂方法模式)
- java远程调试
- 20 个强大图象处理功能的 SVG 工具
- pat 两个有序序列的中位数
- 天池大数据比赛
- Java在Web开发语言上败给了PHP?(这是用的问号,各位php、java大神勿喷,看看就好 -_-||)
- hdu 1233 还是畅通工程(prim||kruskal)
- hadoop,jdk在centos上的的安装
- java中重载与重写的区别