/* * The policy pattern defines a series of algorithms, encapsulates each algorithm, and allows them to be replaced with each other. * The strategy mode makes the algorithm independent of the customers who use it. * Abstract policy roles: policy classes, usually implemented by an interface or abstract class. * Specific policy role: wrapping the relevant algorithms and behaviors. * Environment role: holds a reference to a policy class that is eventually called to the client. * * * Pros and Cons: * Advantages: * 1, the Strategy model provides a way to manage the associated algorithm family. 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 Strategy 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, which makes it 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 not easy to maintain, * it mixes the logic of which algorithm or behavior is taken with the logic of the algorithm or action, and is listed in a multiple transfer statement, more primitive and backward than the method of using inheritance. * * Disadvantage: * 1, the client must know all the policy classes and decide for themselves which policy class to use. * 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 mode only applies if 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. * Sometimes the policy class can be used by different clients by saving the environment-dependent state to the client and designing the strategy class to be shareable. In other words, you can use the enjoy meta mode to reduce the number of objects. * * Applicable scenario: * 1, multiple classes only difference in performance behavior, you can use the strategy mode, at runtime to select the specificThe behavior to perform. * 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. * *http://www.diyibk.com/post/115.html* * Design principle: * 1. Package changes: Find out where the application may need to change, separate them out, and do not mix with the code that does not need to change; * 2. Programming for interfaces, not for implementation; * 3. Multi-use combination, less inheritance;*/
Design pattern--Strategy mode