One of the design pattern instance (Lua) Notes (Factory method Factory mode)

Source: Internet
Author: User

1. Description
Nu wa fill the story of the day everyone has heard that, today do not say this, said Nu WA to create a man's story, not "make people" work, the word is abused by modern man. The story is that Nu wa in the days after, down to where a look, wow, the scenery is too beautiful, the sky is blue, the water is clear, the air is fresh, too beautiful, and then long time is a little lonely, no animals, these to see are static things ah, how to do? Don't forget it's a fairy, Nothing can not be done, so nu wa set up the gossip stove (technical terminology: building a factory) began to create people, the concrete process is this: first mud pinch, and then put gossip stove baked, and then thrown to the ground to grow, but the accident will always produce: the first bake clay, Zzzzizz ~ ~, the feeling should be ripe, to the ground to throw ~, a white man was born, not cooked! The second time baked clay figurines, Zzzizzzzizziz ~ ~, the last time did not roast, this time more baking, to the ground a throw, hey, ripe, black! The third time to bake clay figurines, and then ~ ~ ~, while the roast side looked, hey, just right, perfect! excellent products, yellow human! This process is more interesting, first look at the class diagram:







2. Code:
--Load file require "class"--------------------------------product--------------------------------Human Ihuman = Class () function Ihuman:laugh () print ("Ihuman Laugh") endfunction ihuman:cry () print ("Ihuman Cry") endfunction Ihuman:talk () print (" Ihuman talk ") end--yellow man Yellowhuman = Class (Ihuman) function Yellowhuman:laugh () print (" Yellow race will Cry ") Endfunction Yellowhuman: Cry () print ("Yellow race laughs, happiness") endfunction Yellowhuman:talk () print ("The yellow race speaks, and generally says double-byte") end--Caucasian Whitehuman = Class ( Ihuman) function Whitehuman:laugh () print ("White race Will Cry") endfunction whitehuman:cry () print ("White race will laugh, aggression laughter") endfunction Whitehuman:talk () print ("White people speak, generally single-byte") end--black Blackhuman = Class (Ihuman) function Blackhuman:laugh () print ("The Negro will Cry ") endfunction Blackhuman:cry () print (" Black will Laugh ") endfunction Blackhuman:talk (" Black can speak, ordinary people do not understand ") End-----------------Factory---------------ihumanfactory = Class () function Ihumanfactory:createhuman () print (" Ihumanfactory ") end--Create yellow man factory class Yellowhumanfactory = Class (ihumanfactory) function Yellowhumanfactory:createhuman () Return YELLOWHUman:new () end--Create Caucasian factory class Whitehumanfactory = Class (ihumanfactory) function Whitehumanfactory:createhuman () return Whitehuman:new () end--Create Black factory class Blackhumanfactory = Class (ihumanfactory) function Blackhumanfactory:createhuman () Return blackhuman:new () end--test code function Dofactorymethod (Factory) If factory = Nil then return Endihuman = Factory:creat Ehuman () ihuman:cry () Ihuman:laugh () ihuman:talk () End---main---function main () print ("---------- The first people are like this: Yellow man factory to produce yellow people "Dofactorymethod (Yellowhumanfactory:new ()) print ("----------The second group of people: white factory to produce white people ") Dofactorymethod (Whitehumanfactory:new ()) print ("----------The first people are like this: the Black factory to produce blacks") Dofactorymethod ( Blackhumanfactory:new ()) Endmain ()

The results of the operation are as follows:


notes:Class.lua
Local _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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

One of the design pattern instance (Lua) Notes (Factory method Factory mode)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.