PHP implementation Design Pattern Builder mode

Source: Internet
Author: User

<?php/** * Builder mode *-------------* definition: Separates the construction of a complex object from its representation so that the same build process can create different representations. * Type: Create class mode * Four elements: * 1: Product class: Generally a more complex object * 2: Abstract Builder: Extracts the structure of complex product classes. There are generally at least two abstract methods, one for building products and one for returning products. * 3: Builder: Implement all the non-implemented methods of an abstract class, typically two tasks: build a product, and return to a well-formed product. * 4: Director class: Responsible for invoking the appropriate builder to assemble the product *///complex product class Car{private $name;p rivate $type;p rivate $price;p rivate $color;p ublic function SetName ($name) {$this->name = $name;} Public Function SetType ($type) {$this->type = $type;} Public Function Setprice ($price) {$this->price = $price;} Public Function SetColor ($color) {$this->color = $color;}  Public function Show () {echo $this->name. ': ' $this->type. '  '. $this->color. ' '. $this->price;echo "<br/>";}} Abstract its creation process, which can be used for different objects to create interface Bulider{public function CreateType ($type);p ublic function Createname ($name);p ublic function Createcolor ($color);p ublic function Createprice ($price);p ublic function Createcar (); Class Concretebulider implements bulider{//contains a complex object public $car;p ublic function __construct () {$this->car = new CAR ();} Public Function CreateType ($type) {$this->car->settype ($type);} Public Function Createcolor ($color) {$this->car->setcolor ($color);} Public Function Createname ($name) {$this->car->setname ($name);} Public Function Createprice ($price) {$this->car->setprice ($price);} Public Function Createcar () {return $this->car;}} Package variable parts, such as order, attribute class Derictor{public function __construct (Concretebulider $bulider) {$bulider->createcolor (' Red $bulider->createname (' BMW '); $bulider->createprice (' 1.5 million '); $bulider->createtype (' SUV ');}} Class Client{public static function main () {//Create a builder, the Director class uses it to create objects $bulider = new Concretebulider (); $derictor = new Derictor ($bulider); $car = $bulider->createcar (); $car->show ();}} Client::main ();? >

The UML class diagram is as follows:

PHP implementation Design Pattern 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.