php-Common Design Patterns

Source: Internet
Author: User

Factory mode
/** * Factory method or class to generate the object, rather than in the code directly new *  * Modify the class name, do not need to modify each instantiation statement * Only need to modify the corresponding factory method *  * Class Factory * @package Regina */cla SS factory{    static function CreateDatabase () {        $db = database::getinstance ();        return $db;    }}
Single-Case mode
/** * Singleton mode, save resources, a class can only instantiate an object *  * Make an object of a class only allowed to create one *  * in order to implement Singleton mode, to privatize constructors and clone Magic method * Class Database * @package Regina */class database{        protected static $db;    Private Function __construct () {    }    Private Function __clone () {    }    static function getinstance () {        if (self:: $db) {            return to self:: $db;        }        Self:: $db = new Database ();        Return self:: $db;    }}
Registration mode
/** * Save the new object in a static value for global sharing * global share and Swap objects * Class Register * @package regina */class register{    protected static $arra y;    static function set ($name, $OBJ) {        echo ' Register set run ... ';        Self:: $array [$name] = $OBJ;    }    static function Get ($name) {        echo ' Register get run ... ';        Return self:: $array [$name];    }    function _unset ($name) {        echo ' Register unset run ... ';        Unset (self:: $array [$name]);}    }
Adapter mode
/** * Encapsulates a variety of distinct function interfaces into a unified API * Interface Idatabase * @package regina */interface idatabase{    function Connect ($host, $US Ername, $password, $dbname);    function query ();    function close ();} Class Mysqli implements database{    protected $conn;    Function Connect ($host, $username, $password, $dbname) {        echo ' mysqli connect run ... ';    }    function query () {        echo ' mysqli query run ... ';    }    function Close () {        echo ' mysqli close run ... ';    }} Class Mysql implements database{    function connect ($host, $username, $password, $dbname) {        echo ' MySQL Connect Run ... ';    }    function query () {        echo ' Mysql query run ... ';    }    function Close () {        echo ' Mysql close run ... ';    }}
Policy mode

Encapsulates a specific set of behaviors and algorithms into classes to accommodate the definition of a dictation context.

Dig pits to fill~~~~~

php-Common Design Patterns

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.