Php implementation design pattern policy pattern

Source: Internet
Author: User
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. Is A behavior pattern. Rule mode includes three types of role 1 abstract policy roles: Policy class, which is usually implemented by an interface or abstract class. 2. Specific policy roles: Related calculation is encapsulated.

Policy mode: defines a series of algorithms, encapsulates each algorithm, and makes them replaceable. This mode allows algorithms to change independently of customers who use it. Is A behavior pattern. Rule mode includes three types of role 1 abstract policy roles: Policy class, which is usually implemented by an interface or abstract class. 2. Specific policy roles: Related calculation is encapsulated.

Rule mode:SetA series of algorithms, encapsulate each algorithm, and make them replace each other. This mode allows algorithms to change independently of customers who use it. Is A behavior pattern.

Rule mode includes three roles

1. abstract policy role: a policy class, usually implemented by an interface or abstract class.

2. Specific policy roles: encapsulate related algorithms and behaviors.

3. Environment role: Hold a reference to the policy class and call it to the client.

For example, there are multiple sorting methods. I can write a sorting class, and each Sorting Algorithm writes a method. When the client calls it, you can know each method. However, when you add an algorithm or rewrite an algorithm, you must modify this algorithm class. When this algorithm class is large, it becomes difficult to maintain it.

The policy mode separates the object (configuration class) from the algorithm class (specific algorithm class. In this way, the modification of the algorithm class is added. It does not affect the modification of other classes, but you can replace the algorithm by yourself.

The UML class diagram is as follows:

 Cg = $ a;} public function dodo () {return $ this-> cg-> dosomething (); // The same method acts on objects of different classes and produces different results, this is a polymorphism} // client call in php. The customer decides which policy to use, that is, the customer instantiates the algorithm class. Unlike the simple factory mode, the simple factory mode is the object creation mode. The client does not create an object, but only provides parameters. The factory method determines which instance to create. // That is to say, in simple factory mode, the client only transmits parameters. In policy mode, the client transfers Algorithm Instances $ m = new context (new category_ B (); $ m-> dodo ();?>

The preceding rule mode is implemented.

Now I want to add an algorithm, do D; I just need to write a new class

class category_d implements category{public function dosomething(){echo 'do D';}}

Client call. Replace it with d.

$m = new context(new category_b());

Different from the simple factory model (see simple factory model ).

Disadvantages of Rule mode:

The client must know all the policy classes and decide which one to use. This means that the client must understand the differences between these algorithms so that appropriate algorithm classes can be selected in a timely manner. In other words, the policy mode is only applicable when the client knows all the algorithms or actions.

Use the Strategy mode when the following conditions exist:
1) • many related classes only have different behaviors. "Policy" provides a method to configure a class with one of multiple actions. That is, a system must dynamically select one of several algorithms.
2) • different variants of an algorithm are required. For example, you may define algorithms that reflect different space/time trade-offs. When these variants are implemented as class layers of an algorithm, the policy mode can be used.
3) • algorithms use data that customers should not know. Policy modes can be used to avoid exposing complex algorithm-related data structures.
4) • a class defines multiple behaviors, and these behaviors appear in the form of multiple condition statements in the operations of this class. Move related condition branches into their respective Strategy classes to replace these condition statements.

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.