<?php/* The so-called strategy mode is in different event strategy mode is for the same behavior, in different scenarios have different algorithms, these algorithms are encapsulated, and these algorithms are interchangeable, so that the customer hides the corresponding algorithm implementation details, can be very convenient to select the specific behavior algorithm (i.e. strategy) at run time. Simple strategy mode: Security for students and teachers into the examination room this event, can differentiate different strategies, students to check the test evidence, teacher release */interface strategy{public function handle (); Execution of the strategy (function)}/** to the student's strategy */class Studentstrategy implements Strategy{public function handle () {echo ' Check ';}} /** 's strategy to the teacher */class Teacherstrategy implements Strategy{public function handle () {echo ' GoGoGo ';}} Case class Testcheck{private $strategy; Set policy public Function setstrategy (strategy $strategy) {$this->strategy = $strategy;} Start policy public Function Startstrategy () {$this->strategy->handle ();}} $people = ' teacher '; switch ($people) {case ' student ': $strategy = new Studentstrategy (); Break;case ' teacher ': $strategy = new Teacherstrategy (); break;} $safePeople = new Testcheck (); $safePeople->setstrategy ($strategy); $safePeople->startstrategy ();
Design Patterns in PHP--strategy mode