PHP 3 Basic Design Patterns combined use

Source: Internet
Author: User

1.1 工厂模式, the factory method or class generates the object instead of the code directly in the new
    class Factory{              staticfunction getDatabase(){            returnnew Mysql($host$user$pass);        }    }    #使用    Factory::getDatabase();
1.2 单例模式To make an object of a class run only to create a
  • 1. There is a private static object variable, specifically for the object of this class
  • 2. There is a static method to create the object
  • 3 a private constructor to prevent the external new object
  • 4. There is a clone method to prevent clone return False
    Reference Article Singleton mode
 class Database {      //Single Object Properties    Private Static $instance;//define some global variables to store properties    Private $props=Array();//Private method of construction    Private  function __construct(){          Echo ' into construct! the class does not allow external creation of objects '; }//Return Single Instance     Public Static  function getinstance () {          //To determine if an instantiated object is already in existence        if(Empty( Self::$instance)) {//Can be override (dynamic parsing)             Self::$instance=New Static();//cannot be override (static parsing)            //self:: $instance = new self (); }return  Self::$instance; } Public  function __clone(){        return ' This class prohibits clone '; }//Set Properties     Public  function setProperty ( $key, $value) {          $this->props[$key] =$value; }//Get Properties     Public  function getpeoperty ( $key ) {          return $this->props[$key]; }  }//Use$DBOBJ= Database::getinstance ();$DBOBJ->setproperty (' Root_path ','/www ');$DBOBJ->setproperty (' Tmp_path ','/tmp ');///Next Delete the singleton, if you can also get the property you just added, it means that the same object is usedunset($DBOBJ);$DBOBJ= Database::getinstance ();Echo $DBOBJ->getpeoperty (' Root_path ');Echo $DBOBJ->getpeoperty (' Tmp_path ');
1.3 Registration mode, global share and Swap objects
  • 1. The same need to use the unified registration of the object multiple aliases, unified call to use, (such as customers buy a machine must go to the factory identified institutions to buy, not everyone to the factory to buy)
  • 2. The next time you want to use an object, do not need to use the factory, nor need to use a singleton mode, directly on the registrar to get it
     class Register () {        protected Static $objects; function set($alias, $object){             Self::$objects[$alias] =$objects; } function get($alias){            return  Self::$objects[$alias]; } function _unset($alias){            unset( Self::$objects[$alias]); }    }
2. Summary use
 class  factory  { static  function  getdatabase   ()  { //Singleton mode Get data object   $DBOBJ  = Database::getinstance ();            //registered to the global tree         Register::set ( ' db1 ' ,  $DBOBJ );     }}  #使用  //the first time the main file inside  factory::getdatabase ();    //use database objects for direct access to  Register::get ( ' db1 ' );  

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

PHP 3 Basic Design Patterns combined use

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.