PHP design mode series (i)

Source: Internet
Author: User

Reference article: http://duchengjiu.iteye.com/blog/2227452

Polymorphic Code

Polymorphic, in Java is so used, in fact, in PHP can be naturally eliminated, because the parameters are dynamic, you can pass anything, no restriction type, directly call the method of the class

Polymorphic, in Java is so used, in fact, in PHP can be naturally eliminated, because the parameters are dynamic, you can pass anything, do not restrict the type, direct call class method abstract class Tiger {public    abstract functio n Climb ();} Class Xtiger extends Tiger {public    function  Climb () {        echo ' falls down ';    }} Class Mtiger extends Tiger {public    function climb () {        echo ' climbs to tree top ';    }} Class Client {public    static function call (Tiger $animal) {        $animal->climb ();}    } Client::call (New Xtiger ()); Client::call (New Mtiger ());

  

interface-oriented development code

Object oriented inside there is an interface development, is a common specification, you produce sockets, I produce plugs//Common Interface interface DB {    function conn ();} Server-side development (do not know who will be called) class Dbmysql implements db{public    function conn () {        echo ' connected to MySQL ';    }} Class Dbsqlite implements db{public    function conn () {        echo ' connected to SQLite ';    }} The client, without seeing the internal details of the Dbmysql, Dbsqlite, only knows that the above two classes implement the DB interface. $db = new Dbmysql (); $db->conn ();          Knowing that this class implements the DB interface, know that there is this conn class $sqlite = new Dbsqlite (); $sqlite->conn ();//I don't even want to tell the client what I have, so how do I further encapsulate// The fewer parties that happen to connect know the better, and you know I have two classes

  Simple Factory mode Code

Simple factory//object-oriented inside there is an interface development, is a common specification, you produce sockets, I produce plugs//Common Interface interface DB {function conn ();}    Server-side development (do not know who will be called) class Dbmysql implements db{Public Function conn () {echo ' connected to MySQL ';    }}class Dbsqlite implements db{public Function conn () {echo ' connected to SQLite '; }}//Simple Factory class Factory {public static function Createdb ($type) {if ($type = = ' MySQL ') {return new D        Bmysql ();        } elseif ($type = = ' SQLite ') {return new dbsqlite ();        } else {throw new Exception ("Error db Type", 1); The}}//client now does not know what class name the other party has.//only know that the other party has opened a Factory::createdb method//method allows the database name to be passed $mysql = factory::createdb (' mysql '); $mysql- >conn (); $sqlite = Factory::createdb (' SQLite '), $sqlite->conn ();//Originally you know the server side of the two class name, think you know too much, and then package up, only to a channel, Better adapt to change//If the Oracle type is added later, what should I do?//We can easily change PHP, like Java packaging a very troublesome, but also modify the server side of the content//server to modify the content of factory (in Java, C + +, after the change will be compiled)// How to improve at this time//in the Ood (object-oriented design) Law, there are important open and close principles--for the modification is closed, for the extension is opened.//You can add a source code, do not modify the old code//now use the second method, calledFactory method 

Factory method code

//Common Interface interface DB {function conn ();} Interface Factory {function createdb ();}    Server-side development (do not know who will be called) class Dbmysql implements db{Public Function conn () {echo ' connected to MySQL ';    }}class Dbsqlite implements db{public Function conn () {echo ' connected to SQLite ';    }}class Mysqlifactory implements factory{Public Function Createdb () {return new Dbmysql ();    }}class Sqlitefactory implements Factory {public Function createdb () {return new dbsqlite ();         }}//Server Side Add Oracle class//The preceding code does not change//I will add a database driver class and a factory, thus avoiding the modification of the source code class Dboracle implements DB {public Function conn () {    Echo ' connected to Oracle ';    }}class Oraclefactory implements Factory {public Function createdb () {return new dboracle (); }}//client Start, the other side gave two APIs, one DB API, one is to create the database Api$fact = new Mysqlifactory (); $db = $fact->createdb (); $db->conn (); $ Fact = new Sqlitefactory (); $db = $fact->createdb (); $db->conn (); 

  Single example design pattern code

Single case//generally a small and medium-sized web site a db to connect to the database on the line//think, can only one DB class, a upload class, a cookie class//class must have only one, how to ensure that the class instance also only one//2 objects is the same time only congruent// Singleton Mode class Single {    protected static $ins = null;    Control permissions, block new operation, close the gate, need to leave a small window    //method before the final, the method cannot be overwritten, the class is final, then the class cannot be inherited    final protected function __construct () {}    //anti-clone    final protected function __clone () {}        //Leave an interface to the new object public    static function Getins () {        if (self:: $ins = = null) {Self            :: $ins = new self ();        }        Return self:: $ins;    }} $s 1 = single::getins (), $s 2 = single::getins (), if ($s 1 = = = $s 2) {    echo ' is an object ';} else {    echo ' is not an object ';}

  

PHP design mode series (i)

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.