23 design patterns (11) Java policy pattern

The 23 design patterns of learning videos have been prepared for everyone, and the need to reply to the "design mode" in the official account for download links.

Definition: define a set of algorithms, encapsulate each algorithm and make them interchangeable.

Type: behavior class pattern

Class diagram:

Policy pattern is the encapsulation of algorithms, which encapsulates a series of algorithms into corresponding classes, and these classes implement the same interface and can be replaced with each other. Among the behavior patterns mentioned above, one pattern also focuses on the encapsulation of algorithms - template method pattern. The control class diagram can see that the difference between the policy mode and the template method mode is just a single encapsulation class Context, which differs from the template method mode. In the template method mode, the main body of the algorithm is called in the abstract parent class, while in the policy mode, the calling algorithm is encapsulated in the encapsulation class Context. Abstract strategy is generally an interface for the purpose of defining specifications, which generally does not contain logic. In fact, this is only a general implementation. In actual programming, because there is inevitably some same logic between specific policy implementation classes, in order to avoid repeated code, we often use abstract classes to play the role of strategy and encapsulate public code. Therefore, in many application scenarios, In the policy mode, you can generally see the shadow of the template method mode.

Structure of policy pattern

Encapsulation class: also known as context, it encapsulates the policy twice in order to avoid the direct call of the high-level module to the policy. Abstract strategy: it is usually an interface. When there is duplicate logic in each implementation class, the abstract class is used to encapsulate this part of public code. At this time, the policy pattern looks more like the template method pattern. Specific policy: the specific policy role is usually played by a group of classes that encapsulate the algorithm, and these classes can be freely replaced as needed.

Policy pattern code implementation

Advantages and disadvantages of policy mode

The main advantages of the strategy mode are:

Policy classes can switch freely. Since policy classes are implemented from the same abstraction, they can switch freely. It is easy to expand. Adding a new policy is very easy for the policy mode. Basically, it can be extended without changing the original code. Avoid using multiple conditions. If you do not use the policy mode, you must use conditional statements to connect all algorithms, and determine which algorithm to use through conditional judgment. As we mentioned in the previous article, using multiple conditional judgment is very difficult to maintain.

There are two main disadvantages of the policy mode:

Maintaining various policy classes will bring additional expenses to development. Maybe everyone has experience in this aspect: Generally speaking, it is a headache if the number of policy classes exceeds 5. The client must be (caller) exposes all policy classes, because which policy to use is determined by the client. Therefore, the client should know what policies there are and understand the differences between various policies. Otherwise, the consequences will be serious. For example, there is a policy model of sorting algorithm, which provides three algorithms: quick sorting, bubble sorting and selective sorting, which are used by the client Before these algorithms, do you need to understand the application of these three algorithms? For another example, the client should use a container, which is implemented by linked list and array. Should the client also understand the difference between linked list and array? At this point, it is contrary to Dimitri's law.

Applicable scenario

For object-oriented design, you must be familiar with the strategy pattern, because it is essentially inheritance and polymorphism in object-oriented. After reading the general code of the strategy pattern, I think that even if you have never heard of the strategy pattern before, you must have used it in the development process? You can consider using the policy mode in at least the following two cases:

Policy mode is a simple and commonly used mode. We often use it intentionally or unintentionally during development. Generally speaking, policy mode will not be used alone. It is often mixed with template method mode and factory mode.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>