An analysis of PHP creator mode _php instance

Source: Internet
Author: User
Creator mode:

In the creator mode, the client is no longer responsible for the creation and assembly of objects, but rather the responsibility of creating the object to its specific creator class, the Assembly of responsibility to the Assembly class, the client pays the call to the object, thus clarifying the responsibilities of each class.
Scenario: Creation is very complex and is assembled in steps.

Copy CodeThe code is as follows:
<?php
/**
* Creator mode
*/
Shopping Cart
Class ShoppingCart {
The selected item
Private $_goods = Array ();
Coupon for use
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 something from the shopping cart, such as when the user closes the browser and then opens it, it will be restored according to the cookie
$data = Array (
' Goods ' = = Array (' clothes ', ' shoes '),
' Tickets ' = = Array (' minus 10 '),
);
If you do not use Creator mode, you will need to restore your shopping cart in the business class step-by-step
$cart = new ShoppingCart ();
foreach ($data [' goods '] as $goods) {
$cart->addgoods ($goods);
// }
foreach ($data [' tickets '] as $ticket) {
$cart->addticket ($ticket);
// }
$cart->printinfo ();
Exit
We provide the 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 ();
?>

As you can see, using the creator pattern to encapsulate the data assembly process for objects with complex internal data, the external interface is very simple and prescriptive, and adding new data items without any external impact.

  • 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.