Policy pattern of Android Design Pattern

Source: Internet
Author: User

Policy pattern of Android Design Pattern

Policy pattern definition: defines algorithm families, which are encapsulated separately so that they can be replaced with each other. This pattern allows algorithm changes to be independent of customers who use algorithms. Essence: separation algorithm, selection and implementation

Object-Oriented Design Principles:

Encapsulation changes

Multi-Purpose Combination and less inheritance

For interface programming, not for implementation programming

Identify potential changes in the application and separate them from those that do not need to be changed.

Class Diagram

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

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.

 

 

Related Article

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.