PHP design mode-policy mode

Source: Internet
Author: User
PHP design pattern-rule pattern statement: This series of blog reference material "big talk design pattern", author Cheng Jie.

Policy patterns define a series of algorithms, encapsulate each algorithm, and make them replaceable. The policy mode allows algorithms to change independently of customers who use them, that is, encapsulate the changed algorithms.


Applicable scenarios:

1. multiple classes only differ in performance behavior. you can use the Strategy mode to dynamically select the behavior to be executed during running.

2. different policies (algorithms) need to be used in different situations, or they may be implemented in other ways in the future.

3. Hiding implementation details of specific policies (algorithms) from customers is completely independent of each other.

4. the client must know all the policy classes and decide which policy classes to use. the policy mode is only applicable when the client knows all the algorithms or actions.

5. the policy mode creates many policy classes. each specific policy class produces a new class.

Sometimes you can save the environment-dependent status to the client, and you can use the enjoy mode to reduce the number of objects.


UML class diagram:


Role Analysis:

Abstract policy role (RotateItem): a policy class, usually implemented by an interface or abstract class.

Specific policy role (ItemX): encapsulates related algorithms and behaviors.

Environment role (ItemContext): Holds a reference to the policy class and calls it to the client.


Code implementation:

 ";} Function unInertisRotate () {echo" I'm an X product and I'm not inertial.
";}}/** Specific policy role ?? Y product * Class YItem */class YItem implements RotateItem {function inertiaRotate () {echo "I am a Y product and I cannot rotate inertia.
";} Function unInertisRotate () {echo" I'm a Y product and I'm not inertial.
";}}/** Specific policy role ?? XY product * Class XYItem */class XYItem implements RotateItem {function inertiaRotate () {echo "I am a XY product and I rotate inertia.
";} Function unInertisRotate () {echo" I am an XY product and I am not rotating inertia.
";}} Class contextStrategy {private $ item; function getItem ($ item_name) {try {$ class = new ReflectionClass ($ item_name ); $ this-> item = $ class-> newInstance ();} catch (ReflectionException $ e) {$ this-> item = "" ;}} function inertiaRotate () {$ this-> item-> inertiaRotate ();} function unInertisRotate () {$ this-> item-> unInertisRotate ();}}

Client call code:

 "; $ Strategy-> getItem ('xitem'); $ strategy-> inertiaRotate (); $ strategy-> unInertisRotate (); echo" Y product
 "; $ Strategy-> getItem ('yitem'); $ strategy-> inertiaRotate (); $ strategy-> unInertisRotate (); echo" XY product
 "; $ Strategy-> getItem ('xyitem'); $ strategy-> inertiaRotate (); $ strategy-> unInertisRotate ();

Advantages:

1. the policy mode provides methods for managing related algorithm families.

The hierarchical structure of a policy class defines an algorithm or behavior family.

Proper use of inheritance can transfer public code to the parent class to avoid repeated code.

2. the policy mode provides a way to replace the inheritance relationship.

Inheritance can process multiple algorithms or actions.

If the policy mode is not used, the environment class that uses algorithms or behaviors may have some subclasses. each subclass provides a different algorithm or behavior. However, in this way, the user of an algorithm or behavior is mixed with the algorithm or behavior itself. The logic that determines which algorithm to use or which behavior to take is mixed with the logic of the algorithm or behavior, so that it is impossible to evolve independently. Inheritance makes it impossible to dynamically change algorithms or behaviors.

3. use policy mode to avoid using multiple conditional transfer statements.

Multiple transfer statements are not easy to maintain. they mix the logic of which algorithm or behavior is adopted with the logic of the algorithm or behavior and are all listed in a multiple transfer statement, it is more primitive and backward than the inheritance method.


Disadvantages:

1. 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.

2. the policy mode creates many policy classes. each specific policy class produces a new class.

Sometimes you can save the environment-dependent status to the client and design the policy class to be shared so that the policy class instance can be used by different clients. In other words, you can use the metadata mode to reduce the number of objects.

PHP object-oriented design model

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.