PHP Common Design Patterns Summary

Source: Internet
Author: User

Decoration Mode:
<?PHPAbstract classTile {Abstractfunction Getwealthfactor ();}classPlains extends Tile {Private$wealthfactor =2; function Getwealthfactor () {return$ This-Wealthfactor; }}Abstract classTiledecorator extends Tile {//Decoration  protected$tile; function __construct (Tile $tile) {$ This->tile =$tile; }}classDiamonddecorator extends Tiledecorator {//Diamond Decorationfunction Getwealthfactor () {return$ This->tile->getwealthfactor () +2; }}classPollutiondecorator extends Tiledecorator {//pollution Decorationfunction Getwealthfactor () {return$ This->tile->getwealthfactor ()-4; }} $tile=NewPlains ();p rint $tile->getwealthfactor ();//2$tile =NewDiamonddecorator (NewPlains ());p rint $tile->getwealthfactor ();//4$tile =NewPollutiondecorator (NewDiamonddecorator (NewPlains ())); Print $tile->getwealthfactor ();//0?>
Combination mode:
<?PHPAbstract classUnit {Abstractfunction Bombardstrength ();}classArcher extends Unit {function bombardstrength () {return 4; }}classLasercannonunit extends Unit {function bombardstrength () {return  -; }}classArmy {Private$units =Array (); Private$armies =Array (); function Addunit (Unit $unit) {Array_push ($ This-units, $unit); } function Addarmy (Army $army) {Array_push ($ This-armies, $army); } function Bombardstrength () {$ret=0; foreach( $ This->units as$unit) {$ret+ = $unitbombardstrength (); }    foreach( $ This->armies as$army) {$ret+ = $armybombardstrength (); }    return$ret; }} $unit 1=NewArcher (); $unit 2=Newlasercannonunit (); $army=NewArmy (); $army-Addunit ($unit 1); $army-Addunit ($unit 2);p rint $army-bombardstrength ();p rint"\ n"; $army 2= Clone $army;//Clone Army$armyAddarmy ($army 2);p rint $army-bombardstrength ();p rint"\ n";?>
Factory mode
<?PHP/** * Operation class * because it contains an abstract method, the class must be declared as an abstract class*/     Abstract classoperation{//abstract methods cannot contain function bodies         Abstract  Publicfunction GetValue ($num 1, $num 2);//It is strongly required that the subclass must implement the function function     }     /** * Addition class*/     classOperationadd extends Operation { Publicfunction GetValue ($num 1, $num 2) {return$num 1+$num 2; }     }     /** * Subtraction class*/     classOperationsub extends Operation { Publicfunction GetValue ($num 1, $num 2) {return$num 1-$num 2; }     }     /** * Multiplication class*/     classOperationmul extends Operation { Publicfunction GetValue ($num 1, $num 2) {return$num 1*$num 2; }     }     /** Division class*/     classOperationdiv extends Operation { Publicfunction GetValue ($num 1, $num 2) {Try {                 if($num 2==0){                     Throw NewException ("the divisor cannot be 0"); }Else {                     return$num 1/$num 2; }             }Catch(Exception $e) {echo"error message:". $eGetMessage (); }         }    /** factory class, mainly used to create objects * function: According to the input operation symbol, the factory can instantiate the appropriate object **/    classfactory{ Public Staticfunction Createobj ($operate) {Switch($operate) { Case '+':                    return NewOperationadd ();  Break;  Case '-':                    return Newoperationsub ();  Break;  Case '*':                    return Newoperationsub ();  Break;  Case '/':                    return NewOperationdiv ();  Break; } }} $test=factory::createobj ('/'); $result= $test->getvalue ( at,0); echo $result;?>

PHP Common Design Patterns Summary

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.