Crab learn PHP Design Model Builder mode

Source: Internet
Author: User
Tags learn php php class

2. Builder mode

Crab today to learn the builder model, yesterday's adapter remember is very clear. For the builder model, the crab understands that you have 10 iphone phones to charge, assuming the charger is rated at 220v, but the external voltage is not stable, may be 220v, or 240v, or 300v, what to do, the feasible way is to buy a household transformer, The charger is connected to the transformer, do not need to directly connected to the external network, no matter how the external voltage changes, only need to operate the transformer. This analogy may be a bit farfetched, but it can also be used to illustrate the purpose of the builder pattern, which is that the original object may be mutable, including the parameters or methods received, but if we have already instantiated the original object in a large number of programs, then it is very bad for maintenance. At this time it is better to create a builder object to complete the instantiation of the original object, to avoid direct manipulation of the original object, so that the subsequent changes in the original object, only need to change the Builder object class.

Reference Project module: Member System

Member system frequency is still very high, before also encountered the project, member of the original object to define the comparison of the introduction, the crab also do quickly, then the lovely customer said to add attributes, but the project is used everywhere the original object, change it is also very troublesome, then if the use of builder mode can be more convenient to make changes.

Original User object: User.class.php

<?php class User{private $username = Null;private $password = null;/** * @return The $username */public function getuse Rname () {return $this->username;} /** * @return The $password */public function GetPassword () {return $this->password;} /** * @param NULL $username */public function Setusername ($username) {$this->username = $username;} /** * @param NULL $password */public function SetPassword ($password) {$this->password = $password;}}? >

That the direct instantiation of the original object to operate the crab doesn't show, here crabs create an intermediate class that is the builder class to instantiate the original object's work: UserBuilder.class.php

<?php require (' User.class.php '); class userbuilder{protected $_user = null;protected $_datas = array (); function __ Construct ($datas) {$this->_user = new User (); $this->_datas = $datas;} Public Function Build () {$this->_user->setusername ($this->_datas[' username '); $this->_user-> SetPassword ($this->_datas[' password ');} Public Function GetUser () {return $this->_user;}}? >

Then write the test file: testbuilder.php

<?php require (' UserBuilder.class.php ');//analog user Data $user = Array (' username ' = ' crab ', ' password ' = ' px123456 ');// Create objects from the Builder class $build = new Userbuilder ($user); $build->build ();//Get User Object $userobj = $build->getuser ();//Print User Object echo ' Username: '. $userobj->getusername (); Echo ' <br>password: '. $userobj->getpassword ();? >

Output test Results:

Username: Crab password:px123456

Crab found that the builder mode and adapter mode is similar, the adapter mode is the original object can not be changed, but also need to add or modify properties or methods, and the builder is the original object is not sure, can not directly use the original object, need to use the builder to build, so two design patterns are roughly sorted out, It's here today, the crab is going to cook, and it's getting hungry.

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.