PHP design mode

Source: Internet
Author: User

1. Single-case mode
Class danli{//Privatization class object store variable private static $_instance;//privatization constructor Private function __construct () {}//privatization clone function Private FU Nction __clone () {}//class instantiation interface public static function getinstance () {if (!     Self::$_instance instanceof self) {self::$_instance = new Self;} return self::$_instance;} Gets the user name according to the User ID public function getusername ($id) {//Get Data Operation Return "returns the last data: {$id}";}} $Danli = Danli::getinstance (); Echo $Danli->getusername (2);2. Abstract Factory mode  //defines an abstract class. Abstract method Abstraction class createobj{    abstract static function GetValue ($num 1, $num 2);}  //definition addition calculation Class class Calculationadd extends createobj{    public static function GetValue ($num 1, $num 2) {         return $num 1 + $num 2;   }}//define multiplication classes class Calculationsub extends createobj{  &n Bsp public static function GetValue ($num 1, $num 2) {        return $num 1 * $num 2;   }}//Define Total Control class-Select Select the specified object class calculation{    public static function Getclassobj ($type) {        switch ($typ e)         {            case ' + ':          &NBSP ;         return new Calculationadd ();                  &NB Sp break;            case ' * ':                return NE W calculationsub ();    &NBSP;           break;       }   }}  $Calculation = Calculation::ge Tclassobj (' + '); Echo $Calculation->getvalue (1,3); 3. Simple Factory mode Simple Factory mode: Static Factory mode. Simple engineering mode is the creation of objects through a static method interface createobj{function GetValue ($num 1, $num 2);}//Add Operation class Calculationadd implements    createobj{function GetValue ($num 1, $num 2) {return $num 1 + $num 2;     }}//Multiplication classes class Calculationsub implements createobj{function GetValue ($num 1, $num 2) {return $num 1 * $num 2;    }}//Operation control class simplefactory{public static function Createadd () {return new Calculationadd ();    } public static function Createsub () {return new calculationsub (); }} $ADD = Simplefactory::createadd (); Echo $Add->getvalue (1,4). " <br/> "; $Sub = Simplefactory::createsub (); Echo $Sub->getvalue (2,3);

PHP design mode

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.