Php design mode-builder mode, php design mode

Source: Internet
Author: User

Php design mode-builder mode, php design mode

Requirement Analysis:

We received an order from BMW and Mercedes Benz, which defines the parts and models of the product. We are responsible for production, and the requirement is simply described as follows. We need to design a design model for this requirement to better adapt to their needs.

First, we need a car model class to define all the required parts. This is called an abstract class because we may still receive orders from more companies, such as Rolls-Royce and Bentley.

Then the abstract class is inherited by the respective vehicles to implement the methods in the class.

Next, we need a builder abstract class to define the methods required to build their respective vehicles.

Then the respective car builders inherit this abstract class.

We will think of a construction model. Good, it is the builder model. It is quite appropriate to use it. Let's take a look at the builder's use case diagram.

Note: The method section in this figure is not correct.

Directly run the Code:

1 <? Php 2 3 abstract class carModel {4 5 // store all parts required for group loading 6 public $ spareParts = array (); 7 8 // car name 9 public $ carName = ""; 10 11 // Add wheel Part 12 public abstract function addLunzi ($ xinghao ); 13 14 // Add Shell Part 15 public abstract function addWaike ($ xinghao); 16 17 // Add engine part 18 public abstract function addFadongji ($ xinghao ); 19 20 // get the car and get the car name 21 final public function getCar ($ carName) {22 if ($ this-> spareParts) {23 $ this-> carName = $ carName; 24 // $ k represents part name 25 // $ v represents model 26 foreach ($ this-> spareParts as $ k =>$ v) {27 $ actionName = "add ". $ k; 28 $ this-> $ actionName ($ v); 29} 30} else {31 throw new Exception ("no car parts "); 32 33} 34} 35} 36 37 38 // define a specific product 39 class bmwCarModel extends carModel {40 41 public $ spareParts = array (); 42 public $ carName = ""; 43 44 public function addLunzi ($ xinghao) {45 echo "BMW ". $ this-> carName. "Wheel, model is ". $ xinghao. "\ n"; 46} 47 48 public function addWaike ($ xinghao) {49 echo "BMW ". $ this-> carName. "shell, model is ". $ xinghao. "\ n"; 50} 51 52 public function addFadongji ($ xinghao) {53 echo "BMW ". $ this-> carName. "The engine type is ". $ xinghao. "\ n"; 54} 55} 56 57 58 // define the specific product 59 class benziCarModel extends carModel {60 61 public $ spareParts = array (); 62 public $ carName = ""; 63 64 public function addLunzi ($ xinghao) {65 echo "Benz ". $ this-> carName. "Wheel, model is ". $ xinghao. "\ n"; 66} 67 68 public function addWaike ($ xinghao) {69 echo "Benz ". $ this-> carName. "shell, model is ". $ xinghao. "\ n"; 70} 71 72 public function addFadongji ($ xinghao) {73 echo "Benz ". $ this-> carName. "The engine type is ". $ xinghao. "\ n"; 74} 75} 76 77 78 79 // defines builder 80 abstract class carBuilder {81 public abstract function setSpareParts ($ partsName, $ xinghao ); 82 83 public abstract function getCarModel ($ name); 84} 85 86 87 class bmwBuilder extends carBuilder {88 private $ bmwModel; 89 90 public function _ construct () {91 $ this-> bmwModel = new bmwCarModel (); 92} 93 94 public function setSpareParts ($ partsName, $ xinghao) {95 $ this-> bmwModel-> spareParts [$ partsName] = $ xinghao; 96} 97 98 public function getCarModel ($ name) {99 $ this-> bmwModel-> getCar ($ name); 100} 101} 102 103 104 class benziBuilder extends carBuilder {105 private $ benziModel; 106 107 public function _ construct () {108 $ this-> benziModel = new benziCarModel (); 109} 110 111 public function setSpareParts ($ partsName, $ xinghao) {112 $ this-> bmwModel-> spareParts [$ partsName] = $ xinghao; 113} 114 115 public function getCarModel ($ name) {116 $ this-> bmwModel-> getCar ($ name); 117} 118 119 120 121 122 // simulate client call 123 124 // create a BMW car, name: BMW x1125 126 $ bmwBuilder = new bmwBuilder (); 127 $ bmwBuilder-> setSpareParts ('lunzi', 'niuqiang wheel 1 '); 128 $ bmwBuilder-> setSpareParts ('waike', 'noble Shell 1 '); 129 $ bmwBuilder-> setSpareParts ('fadongji', 'noble Engine 1 '); 130 $ bmwBuilder-> getCarModel ("BMW x1"); 131 $ bmwBuilder-> getCarModel ("BMW x1 "); // create two BMW x1132 133 in a row // create another BMW without a shell named BMW s5134 $ bmwBuilder = new bmwBuilder (); 135 $ bmwBuilder-> setSpareParts ('lunzi ', 'Awesome wheels 2'); 136 $ bmwBuilder-> setSpareParts ('fadongji ', 'awesome engine 2'); 137 $ bmwBuilder-> getCarModel ("BMW s5 "); 138 $ bmwBuilder-> getCarModel ("BMW s5"); // create two BMW x1

Code can be run directly, and you can try to make a great Mercedes-Benz.

Builder mode definition

Builder Pattern is also called Builder Pattern. Its definition is as follows:

Separate the construction of a complex object from its representation so that the same construction process can create different representations.Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

The general class graph of the builder mode.

 

In builder mode, there are four roles:

  • Product

The template method mode is usually implemented, that is, the template method and basic method. For details, refer to the template method mode in the previous chapter. In this example, BenzModel and BMWModel belong to the product class.

  • Builder abstract Builder

The establishment of standardized products is generally implemented by sub-classes. In this example, CarBuilder belongs to the abstract builder.

  • ConcreteBuilder

Implements all methods defined by abstract classes and returns an object of good components. In this example, BenzBuilder and BMWBuilder belong to the specific builder.

  • Director

Responsible for arranging the order of the existing modules, and then telling Builder to start construction. In the above example, we are the boss. niucha found the boss and said I want this, this, that type of vehicle model, then the boss passed the command to me, and my team and I started to work hard, so the construction of a project was complete.


Introduction to PHP Design Patterns

You don't have to look for 'php design mode'. You can look for 'Design mode' or 'java design mode. Because the design pattern is not an idea for a language, therefore, whether you look at 'php design mode' or 'java design mode' or 'Design mode', you get the same design philosophy.

What are the benefits of the php design model?

If UserFactory is used, you do not need to know the existence of the User class. The abstract class helps you hide it. If there are more classes in the future, this mode will be easier to maintain, we recommend that you understand the abstract factory mode, factory mode, and factory method mode, all of which are for reusable programming.

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.