Design mode PHP5 implementation ---- Strategy

xiaoxiao2021-03-06  42

* /

/ *** String Replacement Rule ** This example assumes a replacement of a piece of article to provide two options * / interface replacerule {public function replace ();}

/ *** Solution One * / Class ReplaceRuleone Implements ReplaceRule {Public Function Replace () {$ SUBJECT = STR_REPLACE ("ABC", "Doodoo", "This is to replace the text! ABC"); Return $ Subject;}}}

/ *** Solution Die * / Class ReplaceRuletwo Implements ReplaceRule {public function replace ($ SUBJECT = STR_REPLACE ("abc", "panwei", "This is to replace text! ABC"); Return $ Subject;}}

/ *** Algorithm Solve Class to provide customers choose what solution to use * / Class Solve {Private $ strategy = null; public function __construct (ReplaceUle $ rule) {$ this-> Strategy = $ rule;} / ** * Substance after the replacement * / public function getContext () {RETURN $ this-> strategy-> replace ();}}

// Call the first algorithm $ SOLVE = New Solve (New ReplaceRuleone ()); $ STR = $ SOLVE-> getContext (); ECHO $ ​​Str; Echo "
/ r / n";

// Call the second algorithm $ SOLVE = New Solve (New ReplaceRuletwo ()); $ STR = $ SOLVE-> getContext (); Echo $ Str;>>>>

转载请注明原文地址:https://www.9cbs.com/read-55024.html

New Post(0)