The strategy mode of Android design mode

Source: Internet
Author: User

The definition of the policy pattern: Define the algorithm families, encapsulate each other, so that they can be replaced with each other, this pattern allows the algorithm to change independently of the customers using the algorithm. Essence: Separation algorithm, select implementation

Object-Oriented design principles:

Package changes

Multi-use combination, less inheritance

Programming for interfaces, not for implementation

Find out where changes may be needed in your application, separate them, and don't mix with code that doesn't need to change

Class diagram


Application Scenarios:
1, multiple classes only differ in performance behavior, you can use the Strategy mode, at run time to dynamically select the specific behavior to execute.
2. Different strategies (algorithms) need to be used in different situations, or strategies may be implemented in other ways in the future.
3, the customer hides the specific strategy (algorithm) Implementation details, each other completely independent.

Advantages:
1. The strategy mode provides a way to manage related algorithm families. The hierarchy structure of a policy class defines an algorithm or a family of behaviors. Proper use of inheritance can transfer common code to the parent class, thus avoiding duplicate code.
2. The policy mode provides a way to replace the inheritance relationship. Inheritance can handle multiple algorithms or behaviors. If the policy mode is not used, then the environment class using the algorithm or behavior may have some subclasses, each of which provides a different algorithm or behavior. However, the user of the algorithm or behavior is mixed with the algorithm or the 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 an algorithm or behavior.
3. Use the policy mode to avoid using multiple conditional transfer statements. Multiple transfer statements are difficult to maintain, and it mixes the logic of which algorithm or behavior is taken with the logic of the algorithm or the action, all in a multiple transfer statement, more primitive and backward than the method of using inheritance.

Disadvantages:

1. The client must know all of the policy classes and decide which policy class to use at its own discretion. This means that the client must understand the differences between these algorithms in order to select the appropriate algorithm classes at the right time. In other words, the policy pattern applies only to situations where the client knows all the algorithms or behaviors.
2, the policy mode causes a lot of policy classes, each specific policy class will produce a new class. It is sometimes possible to design a policy class to be shareable by saving the environment-dependent state to the client, so that the policy class instance can be used by different clients. In other words, you can use the enjoy meta mode to reduce the number of objects.


Example code: http://download.csdn.net/detail/deng0zhaotai/7842995

Reference book: "Head First design mode"

The strategy mode of Android design 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.