Contents of strategy pattern:
Concept; composition; Application Scenario; meaning; implementation; writing steps; advantages and disadvantages; simple implementation.
Concept:
The Strategy Pattern Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Policy patterns define a series of algorithms (algorithm families), encapsulate each algorithm, and make them replaceable. The rule mode allows algorithms to change independently of customers who use it.
The policy mode embodies two very basic object-oriented design principles-the concept of encapsulation changes; interfaces are used in programming, rather than interface implementation (interface-Oriented Programming ).
Composition:
- Abstract policy role (Strategy): A Policy class, usually implemented by an interface or abstract class. Different algorithms implement this interface in different ways. context uses this interface to call different algorithms.
- Concretestrategy: implements the interfaces defined by Strategy and provides specific algorithm implementations (including related algorithms and behaviors ).
- Environment role (context): holds a reference to the policy class and finally calls the client-internally maintains a strategy instance. The algorithm provided by concretestrategy is required. It is responsible for dynamically setting specific implementation algorithms of strategy during runtime, interaction with strategy, and data transmission.
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.
Meaning:
The policy mode enables developers to develop software consisting of many replaceable parts, and each part has a weak connection relationship. The weak connection feature makes the software more
High scalability, easy maintenance, and more importantly, it greatly improves the reusability of software.
Implementation:
1. The purpose of the Policy mode is to encapsulate each algorithm into an independent class with a common interface for a group of algorithms so that they can replace each other.
2. The policy mode allows the algorithm to change without affecting the client. Policy mode can be used to separate behaviors from the environment.
3. The environment class is responsible for maintaining and querying behavior classes, and various algorithms are provided in specific policies. Because the algorithm and environment are independent, the modification of the algorithm does not affect the environment and the client.
Procedure:
1. Define a public interface for the policy object.
2. Write a policy class that implements the above public interface.
3. Save a reference to the policy object in the class using the Policy object.
4. In the class using policy objects, set and get methods (injection) of policy objects or assign values using constructor methods are implemented.
Advantages and disadvantages:
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 avoid repeated code.
2. The policy mode provides a way to replace the inheritance relationship. That is, it retains the advantages of inheritance (code reuse) and is more flexible than inheritance (algorithms are independent and can be expanded at will ).
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
Same algorithm or behavior. However, in this way, the user of an algorithm or behavior is mixed with the algorithm or behavior to determine which algorithm to use or which behavior to take
Logic is mixed with the logic of algorithms or actions, so that it is impossible to evolve independently. Inheritance enables dynamic computation
Methods or actions become impossible.
3. Use Policy mode to avoid using multiple conditional transfer statements. It is not easy to maintain multiple transfer statements. It refers to the logic and algorithm or
The logic of behavior is mixed together and all are listed in a multi-transfer statement, which 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 selection can be made 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
The policy class is designed to be shareable, so that the policy class instances can be used by different clients. In other words, you can use the metadata mode to reduce the number of objects.
A brief example:
/*** Implementation policy mode: * similar to the implementation of treemap, comparator is equivalent to the abstract policy role (strategy), and the class of the comparator interface is equivalent to the specific policy role * (concretestrategy ), treemap is equivalent to the Environment role (Context/Environment). It holds a reference to a policy class and finally calls it to the client ). */Package blogandtest; public class strategytest // The client is used to test the pattern {public static void main (string [] ARGs) {addstrategy = new addstrategy (); environment = new environment (addstrategy); system. out. println ("result of add:" + environment. calculate (3, 4); subtractstrategy = new subtractstrategy (); environment. setstrategy (subtractstrategy); System. Out. println ("result of subtract:" + environment. calculate (3, 4); multiplystrategy = new multiplystrategy (); environment. setstrategy (multiplystrategy); system. out. println ("result of multiply:" + environment. calculate (3, 4); dividestrategy = new dividestrategy (); environment. setstrategy (dividestrategy); system. out. println ("result of Divide:" + environment. calculate (3, 4)) ;}}/*** The classes that implement a concrete strategy shoud implement this interface. * The context class uses this to call the concrete strategy. */interface strategy // analogy interface comparator {public int calculate (int A, int B);}/*** implements the algorithm using the strategy inteface. */class addstrategy implements strategy // analogy comparator's implementing class {public int calculate (I Nt a, int B) {return a + B;} class subtractstrategy implements Strategy {public int calculate (int A, int B) {return a-B ;}} class multiplystrategy implements Strategy {public int calculate (int A, int B) {return a * B;} class dividestrategy implements Strategy {public int calculate (int A, int B) {return a/B; }}/ *** configured with a concretestrategy object and maintains a reference to a strategy object */Class environment // analogy treemap; {private strategy Strategy; Public Environment (strategy Strategy) {This. strategy = strategy;} public strategy getstrategy () {return strategy;} public void setstrategy (strategy Strategy) {This. strategy = strategy;} public int calculate (int A, int B) {return strategy. calculate (a, B); // tips: place the cursor on calculate, hold down the ctrl key, and click the left button to jump to the abstract method calculate in interface strategy; place the cursor, CTRL + T to list all I Nterface Strategy Implementation class .}} /* The output result in eclipse is: Result of Add: 7 result of subtract:-1 result of multiply: 12 result of Divide: 0 */
Strategy Pattern)