Design pattern Instance (Lua) Note IV (Builder mode)

Source: Internet
Author: User

1. Description:


Another Wednesday, near work, the eldest brother suddenly pulled me, Exasperate told me, "The Cow Fork Company is satisfied with our model, and signed a contract, Mercedes-Benz, BMW's vehicle model to me our company produced, but this time added a new demand: Car start, stop, horn Sound, The engine sound has the customer own control, he wants what order in what order, this is OK? ".
Looking at the boss's ardent eyes, I can say what, certainly nod, "no problem!", overtime to do Bai, "again bitter and tired when they are 250 more difficult to risk when their own two skin face and the June mutual encouragement!" This sentence speaks of my heart. The task is next, how do we do it? First we think that Mercedes-Benz, BMW are a product, they have a common attribute, the company is concerned about a single model, Mercedes-Benz Model A is the first engine sound, and then start; Mercedes-Benz Model B is the first to start up, and then there is the engine sound, This is the Cow Fork company to concern, that to our boss side, is to meet the needs of others, what order can be produced in what order of the model out, I will be responsible for the boss of the requirements to achieve, but also if the batch, do not understand? It's okay, keep looking, first I produce N multiple Mercedes-Benz and BMW vehicle models, these vehicle models have a run method, but specific to each model of the Run method may be in the middle of the order of the execution of the task is different, the boss said to what order, I give what order, the final customer can only be the established model after buying. Look at the class diagram first:





Comments:
Main (), customer
Ccarmodel, product model
Cbenzmodel, Mercedes Model
Cbmwmodel, BMW model
Icarbuilder, Builders ' interface
Cbenzbuilder, Mercedes-Benz Builder
Cbmwbuilder, BMW Builder
Cdirector, Director

Description: Ccarmodel implements the template method, builder is responsible for starting the construction of the product. When building a product, the order of construction is determined by the Director or Main.

Note: The builder pattern is very similar to the abstract factory. Builders pay more attention to the logical sequence of product construction, while abstract factories pay more attention to producing different types of products, and abstract factories do not care about order.


2. Code:

Require "class"--------Carmodel--------Carmodel = Class () function Carmodel:ctor () self.m_psequence = {}endfunction Carmodel:start () print ("Carmodel Start") endfunction carmodel:stop () print ("Carmodel Stop") endfunction Carmodel:alarm () print ("Carmodel Alarm") endfunction Carmodel:engineboom () print ("Carmodel engineboom") endfunction Carmodel: Setsequence (pseq) self.m_psequence = Pseqendfunction Carmodel:run () if self.m_psequence thenfor _,v in pairs (self.m_ psequence) Doif v = = "Start" Thenself:start () ElseIf v = = "Stop" thenself:stop () ElseIf v = = "Alarm" thenself:alarm () ElseIf v = = "Engineboom" Thenself:engineboom () endendendend--------Benzmodel--------Benzmodel = Class (Carmodel) function Benzmodel:start () print ("Benz launch ...") endfunction benzmodel:stop () print ("Mercedes-Benz Parking ...") endfunction Benzmodel:alarm () print ( "Mercedes Horn") endfunction Benzmodel:engineboom () print ("Mercedes engine sound is like this ...") End--------Bmwmodel--------Bmwmodel = Class ( Carmodel) function Bmwmodel:start () print ("BMW launches ...") endfunction bmwmodel:stop () print ("BMW parking ...") endfunction Bmwmodel:alarm () print ("BMW horn") endfunction Bmwmodel:engineboom () print ("BMW engine sound is like this ....") End-------------------------------------Icarbuilder-------------------------------------Icarbuilder = Class () function Icarbuilder:setsequence (PSEQ) endfunction Icarbuilder:getcarmodel () end--------Benzbuilder-------- Benzbuilder = Class (Icarbuilder) function benzbuilder:ctor () Self.m_pbenz = Benzmodel.new () endfunction Benzbuilder: Clear () Print ("Benzbuilder:clear") Self.m_pbenz = Nilendfunction benzbuilder:setsequence (pseq) Self.m_pbenz: Setsequence (PSEQ) endfunction Benzbuilder:getcarmodel () return self.m_pbenzend--------Bmwbuilder--------Bmwbuilder = Class (Icarbuilder) function Bmwbuilder:ctor ()--print ("Benzbuilder:ctor") SELF.M_PBMW = Bmwmodel.new () endfunction Bmwbuilder:clear () SELF.M_PBMW = Nilendfunction bmwbuilder:setsequence (pseq) self.m_pBMW:SetSequence (PSEQ) Endfunction Bmwbuilder:getcarmodel () return self.m_pbmwend--[[a model of Mercedes-Benz vehicle model is only start (start), stop method, other engine sound, speakers are not; The model B Mercedes Benz is the first to start the engine Boom), then start (Star), then stop (stop), no horn; The C model BMW is the first horn to call (alarm), then (start), then the parking (stop), the engine does not roar; D Model of the BMW car on a start (start), and then run all the way to black, perpetual motive, no stopping method, no horn, no engine roar; E-Model, F-model ... Wait, there can be many, start (start), Stop (stop), horn (alarm), engine boom)]----------------------------------Director------------ ----------------------Director = Class () function Director:ctor () self.m_pseqence = {}self.m_pbenzbuilder = Benzbuilder.new () Self.m_pbmwbuilder = Bmwbuilder.new () endfunction director:clear () self.m_pseqence = {}self.m_ Pbenzbuilder = Nilself.m_pbmwbuilder = nilend--A model of Mercedes Benz function Director:getabenzmodel () print ("--------A Model Mercedes Benz--------") self.m_pseqence = {}table.insert (self.m_pseqence," Start ") Table.insert (self.m_pseqence," Stop ") Self.m_pBenzBuilder:SetSequence (self.m_pseqence) return Self.m_pBenzBuilder:GetCarModel () end--model B Mercedes Benz function Director:getbbenzmodel () print ("--------model B Mercedes Benz--------") self.m_pseqence = {}table.insert (self.m_pseqence, " Engineboom ") Table.insert (Self.m_pseqence," Start ") Table.insert (self.m_pseqence," stop ") seLf.m_pBenzBuilder:SetSequence (self.m_pseqence) return Self.m_pBenzBuilder:GetCarModel () end--C model of BMW car function Director:getcbmwmodel () print ("--------C model BMW--------") self.m_pseqence = {}table.insert (self.m_pseqence, "alarm") Table.insert (Self.m_pseqence, "Start") Table.insert (self.m_pseqence, "Stop") self.m_pBMWBuilder:SetSequence (self.m _pseqence) return Self.m_pBMWBuilder:GetCarModel () end--D model BMW function Director:getdbmwmodel () print ("--------D Model BMW--------") self.m_pseqence = {}table.insert (self.m_pseqence," Start ") self.m_pBMWBuilder:SetSequence (self.m_ Pseqence) return Self.m_pBMWBuilder:GetCarModel () End---main---function main () director = Director.new () Director: Getabenzmodel (): Run () Director:getbbenzmodel (): Run () Director:getcbmwmodel (): Run () Director:getdbmwmodel (): Run () Endmain ()

The results of the operation are as follows:




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

Design pattern Instance (Lua) Note IV (Builder mode)

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.