Php implementation design mode builder Mode

Source: Internet
Author: User
? Php *** builder mode ** ------------- * Definition: separates the construction of a complex object from its representation, so that different representations can be created during the same build process. * Type: Create class mode * four elements: * 1: product class: generally a complex object * 2: Abstract builder: extracts the structure of complex product classes. Generally

? 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 complex object * 2: Abstract builder: extracts the structure of complex product classes. Generally

 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"
";}}// Abstract the creation process. It can be used to create interfaces Bulider {public function createType ($ type) and public function createName ($ name) for different objects ); public function createColor ($ color); public function createPrice ($ price); public function createCar ();} class ConcreteBulider implements Bulider {// contains a complex object public $ car; public function _ construct () {$ this-> car = new Car ();} public function createType ($ type) {$ this-> car-> setType ($ type);} public funct Ion 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 ;}// encapsulate the variable-prone part, such as the sequence, attribute class Derictor {public function _ construct (ConcreteBulider $ bulider) {$ bulider-> createColor ('red'); $ bulider-> createName ('bmw '); $ bulider-> createPrice ('20140901 '); $ Bulider-> createType ('suv ');} class Client {public static function main () {// create a builder, the director class uses it to create an object $ bulider = new ConcreteBulider (); $ derictor = new Derictor ($ bulider); $ car = $ bulider-> createCar (); $ car-> show () ;}} Client: main () ;?>

The UML class diagram is as follows:

The builder mode is a creation mode used to create objects that are more complex than the product family. The first premise is that there is a complex object that abstracts the process of creating a complex object and is used to create other objects.

Statement: the builder contains a complex object that utilizes the component process of the complex object .. The director class is used to create an object that contains a builder object.

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.