PHP policy mode and php policy Mode

Source: Internet
Author: User

PHP policy mode and php policy Mode

 

Policy mode: defines a series of algorithms, encapsulates each algorithm, and enables them to replace each other. This mode allows algorithms to change independently of customers who use it. The rule mode separates objects from operation rules and has powerful functions, because the core idea of this design mode is the concept of object-oriented programming.

That is, we plan to travel. We can consider several strategies, such as cycling, cars, trains, and airplanes.

 

 

[Primary role in policy mode]

Context: A ConcreteStrategy object is used for configuration. Maintain a reference to the Strategy object. You can define an interface to allow Strategy to access its data. (For outgoing travel (interface class or abstract class), transport must consider the time (method to be implemented by the interface class or abstract class) and the cost)
Abstract Strategy: defines the public interfaces of all supported algorithms. Context uses this interface to call a ConcreteStrategy-defined algorithm. (For environmental implementation, select the transportation scope, train, airplane, bicycle ...)
ConcreteStrategy: a specific algorithm is implemented using the Strategy interface. (The information about the selected transportation mode is provided)

 

 

[Policy mode PHP example]

# Context interface TravelTool {public function useTime (); public function money () ;}# Strategy class Plane implements TravelTool {# airplane public function useTime () {return '1 H';} public function money () {return '$ 10';} class Train implements TravelTool {# Train public function useTime () {return '3 H';} public function money () {return '$ 5' ;}# a specific policy class (ConcreteStrategy) class doTravel {public $ obj; public function _ construct ($ method) {$ temp = new ReflectionClass ($ method ); # I saw other blogs with reflection and learned the http://www.php.net/manual/zh/book.reflection.php $ this-> obj = $ temp-> newInstance ();} public function money () {echo $ this-> obj-> money ();} public function useTime () {echo $ this-> obj-> useTime ();}} $ obj = new doTravel ('train'); $ obj-> money (); echo "<br/>"; $ obj-> useTime ();

For more detailed content (advantages, disadvantages, applicability) See here reprinted on http://www.howzhi.com/group/php/discuss/3456

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.