Table of Contents
php design pattern - builder pattern, php design pattern
Definition of Builder Pattern
Introduction to PHP design patterns
What are the benefits of PHP design patterns
Home Backend Development PHP Tutorial php design pattern - builder pattern, php design pattern_PHP tutorial

php design pattern - builder pattern, php design pattern_PHP tutorial

Jul 13, 2016 am 10:19 AM
Design Patterns

php design pattern - builder pattern, php design pattern

Requirements analysis:

We received an order from BMW and Mercedes-Benz. They are responsible for defining the parts and models of the products, and we are responsible for production. The simple description of the requirements is like this. We need to design a design pattern 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. The reason for this is because we may receive orders from more companies, such as Rolls-Royce and Bentley. .

Then each car inherits this abstract class and implements the methods inside.

Next, you need a builder abstract class to define the methods needed to build your own car

Then each car builder inherits this abstract class.

We will think of a construction mode, yes, it is the builder mode. It's perfect to use. Take a look at the builder’s use case diagram

Please note: The method part of the example in this picture does not match my example.

Directly upload the code:

<span>  1</span> <?<span>php
</span><span>  2</span> 
<span>  3</span> <span>abstract</span> <span>class</span><span> carModel{
</span><span>  4</span> 
<span>  5</span>     <span>//</span><span>这里存储所有组装车需要的零件</span>
<span>  6</span>     <span>public</span> <span>$spareParts</span> = <span>array</span><span>();
</span><span>  7</span> 
<span>  8</span>     <span>//</span><span>车的名字</span>
<span>  9</span>     <span>public</span> <span>$carName</span> = ""<span>;
</span><span> 10</span> 
<span> 11</span>     <span>//</span><span>增加轮子部件</span>
<span> 12</span>     <span>public</span> <span>abstract</span> <span>function</span> addLunzi(<span>$xinghao</span><span>);
</span><span> 13</span> 
<span> 14</span>     <span>//</span><span>增加外壳部件</span>
<span> 15</span>     <span>public</span> <span>abstract</span> <span>function</span> addWaike(<span>$xinghao</span><span>);
</span><span> 16</span> 
<span> 17</span>     <span>//</span><span>增加发动机部件</span>
<span> 18</span>     <span>public</span> <span>abstract</span> <span>function</span> addFadongji(<span>$xinghao</span><span>);
</span><span> 19</span> 
<span> 20</span>     <span>//</span><span>获取车,并给车取名字</span>
<span> 21</span>     <span>final</span> <span>public</span> <span>function</span> getCar(<span>$carName</span><span>){
</span><span> 22</span>         <span>if</span>(<span>$this</span>-><span>spareParts){
</span><span> 23</span>             <span>$this</span>->carName = <span>$carName</span><span>;
</span><span> 24</span>             <span>//</span><span>$k 代表部件名字
</span><span> 25</span> <span>            //$v 代表型号</span>
<span> 26</span>             <span>foreach</span>(<span>$this</span>->spareParts <span>as</span> <span>$k</span>=><span>$v</span><span>){
</span><span> 27</span>                 <span>$actionName</span> = "add" . <span>$k</span><span>;
</span><span> 28</span>                 <span>$this</span>-><span>$actionName</span>(<span>$v</span><span>); 
</span><span> 29</span> <span>            }
</span><span> 30</span>         }<span>else</span><span>{
</span><span> 31</span>             <span>throw</span> <span>new</span> <span>Exception</span>("没有汽车部件"<span>);
</span><span> 32</span>             
<span> 33</span> <span>        }
</span><span> 34</span> <span>    }
</span><span> 35</span> <span>}
</span><span> 36</span> 
<span> 37</span> 
<span> 38</span> <span>//</span><span>定义具体的产品</span>
<span> 39</span> <span>class</span> bmwCarModel <span>extends</span><span> carModel{
</span><span> 40</span> 
<span> 41</span>     <span>public</span> <span>$spareParts</span> = <span>array</span><span>();
</span><span> 42</span>     <span>public</span> <span>$carName</span> = ""<span>;
</span><span> 43</span> 
<span> 44</span>     <span>public</span> <span>function</span> addLunzi(<span>$xinghao</span><span>){
</span><span> 45</span>         <span>echo</span> "宝马".<span>$this</span>->carName."的轮子,型号是" . <span>$xinghao</span> . "\n"<span>;
</span><span> 46</span> <span>    }
</span><span> 47</span> 
<span> 48</span>     <span>public</span> <span>function</span> addWaike(<span>$xinghao</span><span>){
</span><span> 49</span>         <span>echo</span> "宝马".<span>$this</span>->carName."的外壳,型号是" . <span>$xinghao</span> . "\n"<span>;
</span><span> 50</span> <span>    }
</span><span> 51</span> 
<span> 52</span>     <span>public</span> <span>function</span> addFadongji(<span>$xinghao</span><span>){
</span><span> 53</span>         <span>echo</span> "宝马".<span>$this</span>->carName."的发动机,型号是 " . <span>$xinghao</span> . "\n"<span>;
</span><span> 54</span> <span>    }
</span><span> 55</span> <span>}
</span><span> 56</span> 
<span> 57</span> 
<span> 58</span> <span>//</span><span>定义具体的产品</span>
<span> 59</span> <span>class</span> benziCarModel <span>extends</span><span> carModel{
</span><span> 60</span> 
<span> 61</span>     <span>public</span> <span>$spareParts</span> = <span>array</span><span>();
</span><span> 62</span>     <span>public</span> <span>$carName</span> = ""<span>;
</span><span> 63</span> 
<span> 64</span>     <span>public</span> <span>function</span> addLunzi(<span>$xinghao</span><span>){
</span><span> 65</span>         <span>echo</span> "奔驰".<span>$this</span>->carName."的轮子,型号是" . <span>$xinghao</span> . "\n"<span>;
</span><span> 66</span> <span>    }
</span><span> 67</span> 
<span> 68</span>     <span>public</span> <span>function</span> addWaike(<span>$xinghao</span><span>){
</span><span> 69</span>         <span>echo</span> "奔驰".<span>$this</span>->carName."的外壳,型号是" . <span>$xinghao</span> . "\n"<span>;
</span><span> 70</span> <span>    }
</span><span> 71</span> 
<span> 72</span>     <span>public</span> <span>function</span> addFadongji(<span>$xinghao</span><span>){
</span><span> 73</span>         <span>echo</span> "奔驰".<span>$this</span>->carName."的发动机,型号是 " . <span>$xinghao</span> . "\n"<span>;
</span><span> 74</span> <span>    }
</span><span> 75</span> <span>}
</span><span> 76</span> 
<span> 77</span> 
<span> 78</span> 
<span> 79</span> <span>//</span><span>定义建造者</span>
<span> 80</span> <span>abstract</span> <span>class</span><span> carBuilder{
</span><span> 81</span>     <span>public</span> <span>abstract</span> <span>function</span> setSpareParts(<span>$partsName</span> , <span>$xinghao</span><span>);
</span><span> 82</span> 
<span> 83</span>     <span>public</span> <span>abstract</span> <span>function</span> getCarModel(<span>$name</span><span>);
</span><span> 84</span> <span>}
</span><span> 85</span> 
<span> 86</span> 
<span> 87</span> <span>class</span> bmwBuilder <span>extends</span><span> carBuilder{
</span><span> 88</span>     <span>private</span> <span>$bmwModel</span><span>;
</span><span> 89</span> 
<span> 90</span>     <span>public</span> <span>function</span><span> __construct(){
</span><span> 91</span>         <span>$this</span>->bmwModel = <span>new</span><span> bmwCarModel();
</span><span> 92</span> <span>    }
</span><span> 93</span> 
<span> 94</span>     <span>public</span> <span>function</span> setSpareParts(<span>$partsName</span> , <span>$xinghao</span><span>){
</span><span> 95</span>         <span>$this</span>->bmwModel->spareParts[<span>$partsName</span>] = <span>$xinghao</span><span>;
</span><span> 96</span> <span>    }
</span><span> 97</span> 
<span> 98</span>     <span>public</span> <span>function</span> getCarModel(<span>$name</span><span>){
</span><span> 99</span>         <span>$this</span>->bmwModel->getCar(<span>$name</span><span>);
</span><span>100</span> <span>    }
</span><span>101</span> <span>}
</span><span>102</span> 
<span>103</span> 
<span>104</span> <span>class</span> benziBuilder <span>extends</span><span> carBuilder{
</span><span>105</span>     <span>private</span> <span>$benziModel</span><span>;
</span><span>106</span> 
<span>107</span>     <span>public</span> <span>function</span><span> __construct(){
</span><span>108</span>         <span>$this</span>->benziModel = <span>new</span><span> benziCarModel();
</span><span>109</span> <span>    }
</span><span>110</span> 
<span>111</span>     <span>public</span> <span>function</span> setSpareParts(<span>$partsName</span> , <span>$xinghao</span><span>){
</span><span>112</span>         <span>$this</span>->bmwModel->spareParts[<span>$partsName</span>] = <span>$xinghao</span><span>;
</span><span>113</span> <span>    }
</span><span>114</span> 
<span>115</span>     <span>public</span> <span>function</span> getCarModel(<span>$name</span><span>){
</span><span>116</span>         <span>$this</span>->bmwModel->getCar(<span>$name</span><span>);
</span><span>117</span> <span>    }
</span><span>118</span> <span>}
</span><span>119</span> 
<span>120</span> 
<span>121</span> 
<span>122</span> <span>//</span><span>模拟客户端调用
</span><span>123</span> 
<span>124</span> <span>//创建一辆宝马车,取名字为宝马x1</span>
<span>125</span> 
<span>126</span> <span>$bmwBuilder</span> = <span>new</span><span> bmwBuilder();
</span><span>127</span> <span>$bmwBuilder</span>->setSpareParts('Lunzi' , '牛逼轮子1号'<span>);
</span><span>128</span> <span>$bmwBuilder</span>->setSpareParts('Waike' , '牛逼外壳1号'<span>);
</span><span>129</span> <span>$bmwBuilder</span>->setSpareParts('Fadongji' , '牛逼发动机1号'<span>);
</span><span>130</span> <span>$bmwBuilder</span>->getCarModel("宝马x1"<span>); 
</span><span>131</span> <span>$bmwBuilder</span>->getCarModel("宝马x1");  <span>//</span><span>连续创建两个宝马x1
</span><span>132</span> 
<span>133</span> <span>//再创建一个宝马 没有外壳 取名为 宝马s5</span>
<span>134</span> <span>$bmwBuilder</span> = <span>new</span><span> bmwBuilder();
</span><span>135</span> <span>$bmwBuilder</span>->setSpareParts('Lunzi' , '牛逼轮子2号'<span>);
</span><span>136</span> <span>$bmwBuilder</span>->setSpareParts('Fadongji' , '牛逼发动机2号'<span>);
</span><span>137</span> <span>$bmwBuilder</span>->getCarModel("宝马s5"<span>); 
</span><span>138</span> <span>$bmwBuilder</span>->getCarModel("宝马s5");  <span>//</span><span>连续创建两个宝马x1</span>
Copy after login

The code can be run directly, and you can try to produce an awesome Mercedes-Benz car.

Definition of Builder Pattern

Builder Pattern is also called Generator Pattern, which is defined as follows:

 Separate the construction of a complex object from its representation so that the same construction process can create different representations. Separate the construction of a complex object from its representation so that the same construction process can create different representations. representation.

The general class diagram of the builder pattern is shown in the figure.

php design pattern - builder pattern, php design pattern_PHP tutorial

In builder mode, there are four characters as follows:

  • Product product category

Usually the template method pattern is implemented, that is, there are template methods and basic methods. Please refer to the template method pattern in the previous chapter. In the example, BenzModel and BMWModel belong to the product category.

  • Builder abstract builder

The construction of standardized products is generally implemented by subclasses. In the example, CarBuilder is an abstract builder.

  • ConcreteBuilder concrete builder

Implement all methods defined by the abstract class and return a component object. In the example, BenzBuilder and BMWBuilder are concrete builders.

  • Director Director

Responsible for arranging the order of existing modules, and then telling the Builder to start building. In the above example, it is our boss. Niucha Company found the boss and said I want this, this, and that type of vehicle model, and then the boss will The order was passed to me, and my team and I started working hard to build, and a project was completed.

Introduction to PHP design patterns

You don’t have to specifically look for ‘php design pattern’, you can look for ‘design pattern’ or ‘java design pattern’ which are easier to find. Because design patterns are not specific to a certain language but an idea, the design ideas you get are the same whether you look at 'php design pattern' or 'java design pattern' or 'design pattern'

What are the benefits of PHP design patterns

If you use UserFactory, you don’t need to know the existence of the User class. The abstract class will help you hide it. If there are more classes in the future, this pattern will be more convenient to maintain. It is recommended that you learn about the abstract factory pattern, factory pattern, and factory. Method patterns, these are all means for reusable programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/878815.htmlTechArticlephp design pattern - builder pattern, php design pattern demand analysis: We received an order from BMW and From Mercedes-Benz, they are responsible for defining product parts and models...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between design patterns and architectural patterns in Java framework The difference between design patterns and architectural patterns in Java framework Jun 02, 2024 pm 12:59 PM

In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

Analysis of the Decorator Pattern in Java Design Patterns Analysis of the Decorator Pattern in Java Design Patterns May 09, 2024 pm 03:12 PM

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

PHP design pattern practical case analysis PHP design pattern practical case analysis May 08, 2024 am 08:09 AM

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

How design patterns deal with code maintenance challenges How design patterns deal with code maintenance challenges May 09, 2024 pm 12:45 PM

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

The wonderful use of the adapter pattern in Java design patterns The wonderful use of the adapter pattern in Java design patterns May 09, 2024 pm 12:54 PM

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

PHP Design Patterns: Test Driven Development in Practice PHP Design Patterns: Test Driven Development in Practice Jun 03, 2024 pm 02:14 PM

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

Application of design patterns in Guice framework Application of design patterns in Guice framework Jun 02, 2024 pm 10:49 PM

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.

What are the advantages and disadvantages of using design patterns in java framework? What are the advantages and disadvantages of using design patterns in java framework? Jun 01, 2024 pm 02:13 PM

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.

See all articles