PHP Design Pattern Notes

Source: Internet
Author: User

First, Factory mode: Reduce the use of new, define a class, specifically used to create other objects
Cases:

Class a{    static function Create () {        return new DB ();    }} $db = A::create ();

Second, Singleton mode: Ensure that only one instance of a class
Usage Scenario: There is only one instance of cache, log object, database operation.
Disadvantages:
1. Difficult to debug
2. Hidden dependencies
Cases:

Class A {    //static variable holds global instance    private static $_init = null;    Private constructor to prevent external instantiation of object Private function    __construct () {            }    //Private clone function to prevent external clone of object    Private Function __clone () {            }    static method, single instance unified Access portal public    static function Getinit () {        if (!isset (Self::$_init)) {            self::$_init = new self () ;        }        return self::$_init;    }        Public Function test () {        echo ' call method succeeded ';    }} $a = A::getinit (); $a->test ();

III. Strategy Model: encapsulate specific behaviors into classes and adapt to specific environments
Usage Scenario: E-commerce mens womens
Cases:

Abstract policy Interface Interface a{public    function Show (); Classes (Environment role) class IE implements  a {public    function show () {        return ' IE ';    }} for use when the client is IE Class FF implements  A {public    function show () {        return ' Firefox '    } when the client is not in Firefox when it is called (Environment role) Specific policy role class U {public    function index ($obj) {        echo $obj->show ();    }} $user = new U (), $user->index (new FF);

Iv. Observer Pattern: When the state of an object changes, all objects that depend on it are notified and updated automatically
Usage Scenario: Event System
Cons: Reduced performance
Cases:

Defines the Observer interface interface Looker {public    function News ($user),//Defines the method that receives the notification}//defines the interface interface event {    public function add ($obj);//define the method of the observed event public    function notify ($user);//define a method that notifies all observers}//the event class E1 implements events {    protected $obj = Array ();        Public function Add ($obj) {//observed event        $this->obj[] = $obj;    }        Public function Notify ($user) {//Notifies all observers of        foreach ($this->obj as $o) {            $o->news ($user);        }    } Viewer class L1 implements Looker {public    function News ($user) {//Receive notification        echo $user. ' There is a new dynamic <br> ';    }} $event 1 = new E1; $event 1->add (new L1); $event 1->notify (' Xiao Ming '); $event 1->notify (' Little Red '); $event 1->notify (' Xiao Li ') );

Five, adapter mode: The various classes are packaged into the same interface
Usage Scenario: Database mysql,mysqli. Cache memcache,redis,file. Can be uniformly consistent with adapter mode

VI, Decorator mode: Can dynamically add the ability to modify the class
Cases:

Abstract class beverage{public $_name; Abstract public function cost ();}    The Decorator class Coffee extends beverage{public function __construct () {$this->_name = ' Coffee ';    } Public Function cost () {return 1.00; }}//The following three classes are decorator related classes class Condimentdecorator extends beverage{public function __construct () {$this->_name = ' Co    Ndiment ';    } Public Function cost () {return 0.1;    }}//2. Add milk Class Milk extends condimentdecorator{public $_beverage;        Public function __construct ($beverage) {$this->_name = ' Milk ';        if ($beverage instanceof beverage) {$this->_beverage = $beverage;        }else{die (' Failure ');    }} public Function cost () {return $this->_beverage->cost () + 0.2;    }}//3. Add sugar class Sugar extends condimentdecorator{public $_beverage;        Public function __construct ($beverage) {$this->_name = ' Sugar '; if ($beverage instanceof beverage) {$tHis->_beverage = $beverage;        }else{exit (' Failure ');    }} public Function cost () {return $this->_beverage->cost () + 0.2; }}//1. Take a cup OF coffee $coffee = new coffee ();//2. Add Milk $coffee = new Milk ($coffee);//3. Add Sugar $coffee = new Sugar ($coffee);p rintf ("Coffee total:%0.2f yuan \ n ", $coffee->cost ());

PHP Design Pattern Notes

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.