<?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