definition: the policy mode belongs to the behavior pattern of the object. It is intended for a set of algorithms that encapsulate each algorithm in a separate class with a common interface, so that they can be replaced with each other. The policy pattern allows the algorithm to change without affecting the client.
Class Diagram:
Advantages:
(1) The Strategy mode provides a way to manage the associated algorithm families. The hierarchy structure of a policy class defines an algorithm or a family of behaviors. Proper use of inheritance can move common code into the parent class, thus avoiding code duplication.
(2) Use the policy mode to avoid using multiple conditional (IF-ELSE) statements. Multiple conditional 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 listed in a multi-conditional statement, more primitive and backward than the method of using inheritance.
Disadvantages:
(1) The client must know all of the policy classes and decide for itself 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 pattern applies only to situations where the client knows the algorithm or behavior.
(2) Since the policy model encapsulates each specific policy implementation as a class, the number of objects can be significant if there are many alternative strategies.
Strategy Mode of design pattern