PHP design mode-builder mode, PHP design mode _php Tutorial

Source: Internet
Author: User

PHP design mode-builder mode, PHP design mode


Requirements Analysis:

We received an order from BMW and Mercedes-Benz, who are responsible for defining the parts and models of the product, and we are responsible for the production and the simple description of the requirement. We need to design a design pattern for this requirement to better adapt to their needs.

First of all we need a car model class to define all the components needed, which is called abstract class, because we are also likely to receive orders from more companies, such as Rolls-Royce, Bentley.

Then by the respective car to inherit this abstract class, implement the method inside.

Next, a builder abstract class is needed to define how the respective vehicle needs to be built.

This abstract class is then inherited by the respective vehicle builders.

We'll think of a construction model, yes, the builder mode. It's just too appropriate to use it. Take a look at the builder's use case diagram

Note: The example method section of this diagram is not the same as my example.

Directly on the code:

1 
 Php2 3 Abstract classcarmodel{4 5     //It stores all the parts needed for the assembled vehicle.6      Public $spareParts=Array();7 8     //the name of the car9      Public $carName= "";Ten  One     //Adding wheel parts A      Public Abstract functionAddlunzi ($xinghao); -  -     //Adding shell parts the      Public Abstract functionAddwaike ($xinghao); -  -     //Increased engine parts -      Public Abstract functionAddfadongji ($xinghao); +  -     //get the car and take the name of the car +     Final  Public functionGetcar ($carName){ A         if($this-spareparts) { at             $this->carname =$carName; -             //$k represents the part name - //$V representative model -             foreach($this->spareparts as $k=$v){ -                 $actionName= "Add".$k; -                 $this-$actionName($v);  in             } -}Else{ to             Throw New Exception("No Car Parts"); +              -         } the     } * } $ Panax Notoginseng  - //define a specific product the classBmwcarmodelextendscarmodel{ +  A      Public $spareParts=Array(); the      Public $carName= ""; +  -      Public functionAddlunzi ($xinghao){ $         Echo"BMW".$this->carname. " The wheel, the model is ".$xinghao. "\ n"; $     } -  -      Public functionAddwaike ($xinghao){ the         Echo"BMW".$this->carname. " The casing, the model is ".$xinghao. "\ n"; -     }Wuyi  the      Public functionAddfadongji ($xinghao){ -         Echo"BMW".$this->carname. " The engine, the model is ".$xinghao. "\ n"; Wu     } - } About  $  - //define a specific product - classBenzicarmodelextendscarmodel{ -  A      Public $spareParts=Array(); +      Public $carName= ""; the  -      Public functionAddlunzi ($xinghao){ $         Echo"Mercedes-Benz".$this->carname. " The wheel, the model is ".$xinghao. "\ n"; the     } the  the      Public functionAddwaike ($xinghao){ the         Echo"Mercedes-Benz".$this->carname. " The casing, the model is ".$xinghao. "\ n"; -     } in  the      Public functionAddfadongji ($xinghao){ the         Echo"Mercedes-Benz".$this->carname. " The engine, the model is ".$xinghao. "\ n"; About     } the } the  the  +  - //Define Builders the Abstract classcarbuilder{Bayi      Public Abstract functionSetspareparts ($partsName,$xinghao); the  the      Public Abstract functionGetcarmodel ($name); - } -  the  the classBmwbuilderextendscarbuilder{ the     Private $bmwModel; the  -      Public function__construct () { the         $this->bmwmodel =NewBmwcarmodel (); the     } the 94      Public functionSetspareparts ($partsName,$xinghao){ the         $this->bmwmodel->spareparts[$partsName] =$xinghao; the     } the 98      Public functionGetcarmodel ($name){ About         $this->bmwmodel->getcar ($name); -     }101 }102 103 104 classBenzibuilderextendscarbuilder{ the     Private $benziModel;106 107      Public function__construct () {108         $this->benzimodel =NewBenzicarmodel ();109     } the 111      Public functionSetspareparts ($partsName,$xinghao){ the         $this->bmwmodel->spareparts[$partsName] =$xinghao;113     } the  the      Public functionGetcarmodel ($name){ the         $this->bmwmodel->getcar ($name);117     }118 }119  - 121 122 //impersonate a client call123 124 //Create a BMW car and take the name BMW X1 the 126 $bmwBuilder=NewBmwbuilder ();127 $bmwBuilder->setspareparts (' Lunzi ', ' Awesome Wheels # 1th ')); - $bmwBuilder->setspareparts (' waike ', ' t-Shell No. 1th ');129 $bmwBuilder->setspareparts (' Fadongji ', ' t-engine # 1th '); the $bmwBuilder->getcarmodel ("BMW X1"); 131 $bmwBuilder->getcarmodel ("BMW X1");//Create two BMW X1 in a row the 133 //re-create a BMW without a shell named BMW S5134 $bmwBuilder=NewBmwbuilder ();135 $bmwBuilder->setspareparts (' Lunzi ', ' Awesome Wheels # 2nd '));136 $bmwBuilder->setspareparts (' Fadongji ', ' t-engine # 2nd ');137 $bmwBuilder->getcarmodel ("BMW S5"); 138 $bmwBuilder->getcarmodel ("BMW S5");//Create two BMW X1 in a row

Code can run directly, you can try to produce a good Mercedes Benz Oh.

Definition of builder pattern

The builder pattern, also called the builder pattern, is defined as follows:

separate the construction of a complex object from it representation so, the same construction process can create Different representations. separating the construction of a complex object from its representation allows the same build process to create different representations.

A generic class diagram of the builder pattern.

In the builder mode, there are four characters:

    • Product class

The template method pattern is usually implemented, that is, the template method and the basic method, which refer to the template method pattern of the previous chapter. In the example, Benzmodel and Bmwmodel belong to the product class.

    • Builder Abstract Builder

The establishment of normative products is generally implemented by subclasses. In the example, Carbuilder belongs to the abstract builder.

    • ConcreteBuilder Concrete Builders

Implements all methods of the abstract class definition, and returns a component good object. In the example, Benzbuilder and Bmwbuilder belong to the concrete builders.

    • Director

Be responsible for arranging the order of the existing modules, and then tell Builder to start building, in the example above is our boss, the cattle Fork Company to find the boss, said I want this, this, that type of vehicle model, and then the boss of the command passed to me, my team and I began desperately building, so a project was completed.


Introduction to PHP Design Patterns

You don't have to look for ' php design Patterns ', you can look for ' design patterns ' or ' Java Design Patterns ' and more. Because design patterns are not specific to a language but a thought, you can see that the design patterns are the same in either the PHP design pattern or the Java design pattern ' or ' design pattern.

What are the benefits of PHP design patterns?

If you use Userfactory, you do not have to know the existence of the user class, the abstract class to help you hide it, later if the class more, the mode of maintenance is more convenient, it is recommended that you understand the abstract Factory mode, Factory mode, factory method mode, these are for reusable programming means.

http://www.bkjia.com/PHPjc/878815.html www.bkjia.com true http://www.bkjia.com/PHPjc/878815.html techarticle PHP design mode-builder mode, PHP design pattern Requirements Analysis: We received an order from BMW and Mercedes, who are responsible for defining the parts and models of the product ...

  • 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.