Design Patterns in PHP--strategy mode

Source: Internet
Author: User

<?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

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.