Java record-66-Rule Mode
Strategy: the Strategy model embodies two basic object-oriented design principles: l encapsulation of the concept of change; l Programming Using Interfaces rather than interface implementation; interface-oriented programming policy mode definition: l defines a group of algorithms, encapsulates each algorithm, and makes them interchangeable; l The Policy mode enables these algorithms to change without affecting each other when the client calls them. The significance of the Policy mode is as follows: l The Policy mode enables developers to develop software consisting of many replaceable parts, and each part has a weak connection relationship; l The weak connection feature makes the software more scalable and easy to maintain; more importantly, it greatly improves the reusability of the software; composition of the policy model: l abstract policy role: strategy class, usually implemented by an interface or abstract class; l specific policy role: encapsulates related algorithms and behaviors; l environment role: holds a reference of the Policy class and finally calls it to the client. Implementation of policy mode: l the purpose of policy mode is to encapsulate each algorithm into an independent class with a common interface for mutual replacement; l The Policy mode allows the algorithm to change without affecting the client. Policy mode can be used to separate behaviors from the environment. l 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, modifications to the algorithm do not affect the environment and the client. Write the policy Mode Step: 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. disadvantages of policy pattern: 1. the client must specify all the policy classes and decide which policy class to use. 2. causes many strategies. (The Factory method can solve this problem)