Rule mode: 1. rule mode motivation: there are multiple ways to complete a task. Each method is called a policy. We can select different policies based on different environments or conditions to complete the task. This situation is often encountered in software development: there are multiple ways to achieve a certain function. At this time, a design mode can be used to allow the system to flexibly choose a solution, it can also easily add new solutions. 2. The policy mode is the behavior mode of the object, and is intended to encapsulate a group of algorithms. Dynamically select the desired algorithm and use the methods of human travel in the previous example. By encapsulating the travel method, dynamically select the desired algorithm and use it. 3. policy mode role: (1) Abstract policy role: it is usually implemented using an abstract class or interface. It mainly defines the functions completed by this algorithm (2) specific policy role: related Algorithms and behaviors are encapsulated. (3) Environment roles: reference of the holding strategy class: abstract policy mode <---> travel mode specific policy roles <---> cycling, cycling, train, aircraft environment role <---> People (use of policies) Environment class: Context4. the policy mode is simple and easy to use. The policy mode is the encapsulation of algorithms, it separates the algorithm's responsibilities from the algorithm itself and delegates different object management. Policy mode typically encapsulates a series of algorithms into a series of policy classes and serves as a subclass of an abstract policy class. In other words, "prepare a group of algorithms and encapsulate each algorithm, so that they can be exchanged ". 5. advantages and disadvantages: (1) dynamically changing algorithms or behaviors (2) Simplifying unit testing. Because each algorithm has its own class, you can test the disadvantages through its own interface: the client must know all the policy classes and decide which policy classes to use. 6. the combination of policy mode and simple factory mode combines the simple factory mode in the Context Environment corner class, that is, the constructor is designed in the Context, the independent variables are passed in, and the Environment role selects the algorithm used based on the input independent variables, the disadvantage is that the client must be able to identify the Context before use, that is, to understand the meaning of the parameter. The following is the instance code [java] package Strategy;/* abstract policy role */public interface IOperate {public double getResult (double a, double B);} [java] package Strategy; /* Algorithm for adding specific policy roles */public class Addition implements IOperate {public double getResult (double a, double B) {return a + B;} [java] package Strategy; public class Multiplication implements IOperate {public double getResult (double a, double B) {return a * B ;}} [java] package Strategy; import java. util. *;/* Environment role, holding a policy class reference */public class Context {IOperate opr = null; public Context () {}; public Context (int flag) {/* simple factory mode. Context is equivalent to a factory. It determines the product to be produced based on the input flag, but the client must have the ability to identify the Context, that is, the parameter meaning */switch (flag) {case 1: {opr = new Addition (); break;} case 2: {opr = new Multiplication (); break;} default: {break ;}}} public final static Addition add = new Addition ();/* pure policy mode */public final static Multiplication multi = new Multiplication ();} MAIN Function [java] package Strategy; public class Main {public static void main (String [] args) {Context context = new Context ();/* Create an environment role */double ret = context. add. getResult (12.0, 23.99); System. out. println ("context. add. getResult (12.0, 23.99) = "+ ret); ret = context. multi. getResult (12.0, 4); System. out. println ("context. multi. getResult (12.0, 4) = "+ ret);/* combination of policy mode and simple factory mode */Context contFac = new Context (2 ); /* You must know in advance what the input 2 represents. This is a disadvantage */ret = contFac. opr. getResult (2, 23); System. out. println ("conFac. opr. getResult (2, 23) = "+ ret );}}