Home > Backend Development > PHP Tutorial > A brief analysis of the PHP creator mode, a brief analysis of PHP creation_PHP tutorial

A brief analysis of the PHP creator mode, a brief analysis of PHP creation_PHP tutorial

WBOY
Release: 2016-07-13 10:13:17
Original
952 people have browsed it

A brief analysis of php creator mode, a brief analysis of php creation

Creator Mode:

In the creator mode, the client is no longer responsible for the creation and assembly of the object. Instead, the responsibility for creating the object is handed over to its specific creator class, and the responsibility for assembly is handed over to the assembly class. The client pays the right Object calls, thus clarifying the responsibilities of each class.
Application scenario: The creation is very complex and is assembled in steps.

Copy code The code is as follows:

/**
*Creator Mode
​*/
//Shopping Cart
class ShoppingCart {
//Selected items
Private $_goods = array();
//Coupon used
Private $_tickets = array();
Public function addGoods($goods) {
                   $this->_goods[] = $goods;
}
Public function addTicket($ticket) {
                $this->_tickets[] = $ticket;
}
Public function printInfo() {
                    printf("goods:%s, tickets:%sn", implode(',', $this->_goods), implode(',', $this->_tickets));
}
}
//If we want to restore the contents of the shopping cart, for example, when the user closes the browser and opens it again, it will be restored based on cookies
$data = array(
        'goods' => array('clothes', 'shoes'),
        'tickets' => array('minus 10'),
);
//If you do not use the creator mode, you need to restore the shopping cart step by step in the business class
// $cart = new ShoppingCart();
// foreach ($data['goods'] as $goods) {
// $cart->addGoods($goods);
// }
// foreach ($data['tickets'] as $ticket) {
// $cart->addTicket($ticket);
// }
// $cart->printInfo();
// exit;
//We provide a creator class to encapsulate the data assembly of the shopping cart
class CardBuilder {
       private $_card;
Function __construct($card) {
                   $this->_card = $card;
}
function build($data) {
foreach ($data['goods'] as $goods) {
$this->_card->addGoods($goods);
              }
foreach ($data['tickets'] as $ticket) {
$this->_card->addTicket($ticket);
              }
}
function getCrad() {
                  return $this->_card;
}
}
$cart = new ShoppingCart();
$builder = new CardBuilder($cart);
$builder->build($data);
echo "after builder:n";
$cart->printInfo();
?>

It can be seen that after using the creator pattern to encapsulate the data assembly process for objects with complex internal data, the external interface will be very simple and standardized, and adding and modifying new data items will not have any external impact.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/917028.htmlTechArticleA brief analysis of php creator mode, a brief analysis of php creation creator mode: In creator mode, the client does not It is no longer responsible for the creation and assembly of objects, but gives the responsibility of creating this object to its specific...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template