Design Pattern in PHP

Source: Internet
Author: User
Design Patterns in PHP Abstract factory patterns

For subclass modes that do not need to be merged, factory output can be completed through multiple methods in a class.

Abstract class messageFactor {abstract function getHeader (); abstract function getMail (); abstract function getMobile (); abstract function getFooter ();} class smudgeMo extends messageFactor {public function getHeader () {return 'header';} public function getMail () {return new Mail ();} public function getMobile () {return new Mobile ();} public function getFooter () {return 'footer ';} class julylovinMo extends messageFactor {// similar to the previous subclass}

Although subclass inheritance is reduced, coupling is too serious. if you add a product type, abstract functions, and subclass inheritance bodies, you must add corresponding methods.

abstract class factory{    const APPT = 1;    const TTD = 1;    const CONTACT = 1;    abstract function make($flag_int);}class mailModel extends factory{    public function make($flag_int)    {        switch ($flag_int){            case self::APPT:                return new apptEncoder();                break;            case self::TTD:                return new ttdEncoder();                break;            case self::CONTACT:                return new contactEncoder();                break;        }    }}

Compact factory mode, but high coupling, is not conducive to maintenance

Singleton mode

Global variables bind classes to a specific environment and destroy encapsulation.
Features:
Preference can be called by any object without passing the object as a parameter
Preference should not be stored in global variables
A preference object must not exceed one in the system, that is, it can be instantiated only once.

Class Preference {static $ instance; public $ name; private function _ construct () {} public function setName ($ name) {$ this-> name = $ name ;} public function getName () {return $ this-> name;} public static function getInstance () {if (empty (self: $ instance )) // only one Preference instance object is controlled. {self: $ instance = new Preference ();} return self: $ instance ;}}$ prf = Preference :: getInstance (); // you can obtain the Preference instance object $ prf-> setName ('julylovin '); echo $ prf-> getName (); // Julylovin
Prototype

Replace inheritance with combinations. Abstract factory models have parallel hierarchy inheritance, which causes coupling problems.
Using the clone keyword to copy an existing product, the specific product itself becomes the basis for its own generation

Class sea {private $ navigability = 0; // public function _ construct ($ navigability) {$ this-> navigability = $ navigability ;}} class earthsea extends sea {} class plain {} class earthplain extends plain {} class forest {} class earthforest extends forest {} class factory {private $ sea; private $ plain; private $ forest; public function _ construct (sea $ sea, plain $ plain, forest $ forest) {$ this-> sea = $ sea; $ this-> plain = $ plain; $ this-> forest = $ forest;} public function getSea () {return clone $ this-> sea; // only references objects, is the same object, not two objects} public function getPlain () {return clone $ this-> plain;} public function getForest () {return clone $ this-> forest ;}} $ earthInstance = new factory (new earthsea (1), new earthplain (), new earthforest (); // The sea area can sail $ earthInstance-> getForest (); // new earthforest () instance
Combination mode

The combination mode helps you establish a model between a set and a component.
The gunmen (Archer) are combined into an army (Arm). Multiple gunmen can increase the combat capability (bombardStrength) of the Army)

Abstract class unit {public function addunit (Unit $ unit) {// prevents the independent unit from adding the object throw new unitException (get_class ($ unit) again ). 'is a leaf');} public function removeunit (Unit $ unit) {// prevents the independent unit from deleting the added object throw new unitException (get_class ($ Unit ). 'is a leaf');} abstract function bombardStrength ();} class unitException extends Exception {} class Archer extends unit {public function bombardStrength () {return 4 ;}} Ss Army extends unit {// combination mode private $ units = array (); public function addunit (Unit $ unit) {if (in_array ($ unit, $ this-> units, true) {return ;}$ this-> units [] = $ unit;} public function removeunit (Unit $ unit) {$ units = array (); foreach ($ this-> units as $ item) {if ($ item! ==$ Unit) {$ units [] = $ item; }}$ this-> units = $ units;} public function bombardStrength () {$ ret = 0; foreach ($ this-> units as $ unit) {$ ret + = $ unit-> bombardStrength ();} return $ ret ;}$ SubArmy = new Army (); $ SubArmy-> addunit (new Archer (); echo $ SubArmy-> bombardStrength (); // 8

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.