Thoughts on the template method and Policy mode and the template Policy

Source: Internet
Author: User

Thoughts on the template method and Policy mode and the template Policy

The thought origin of this essay was probably two or three years ago. At that time, I chatted with my former colleagues and had to talk about Http access without knowing how.

I. I remember the first sentence that he and I said, probably: Is there any encapsulated and powerful HttpUtil. It may also be caused by the impact of the business (the interface is internal) and the Http access I was exposed to at the time, most of them are "standardized", at least one interface is bound to something, it will not pass json, return json, and then pass xml, return xml, or even more amazing, upload a file, return 0 or 1. If this happens, HttpUtil can still adapt to various situations conveniently and flexibly. I think this Util is absolutely powerful. Unfortunately, my colleague just needs to meet such an amazing demand (System Interconnection with multiple third parties ).

I remember that in the face of this problem, my first consciousness was: the template method, and it seems that I have seen such a method. Because: The entire Http access process is nothing more:

1. Create a request object based on the Url,

2. Set Request parameters, such as no cache, Header, Cookie, and timeout.

3. Send data, such as the Post method.

4. Create a response object

5. receive data.

6. Get Response parameters

7. In addition to the above process, it is possible to carry out additional processes, such as Util, a powerful framework, exception handling, and logging.

Define an algorithm trunk and delay implementation of steps to sub-classes. Reuse code in the base class to form an inheritance system. In the end, you only need to process three things in the bottom class and rewrite the function. One is to provide the request address, the second is to provide the request data, and the third is to process the response data. Of course, the request method is encapsulated by default. In this case, this solution seems good?

However, what will happen if there are hundreds of interfaces to be connected in the system? Hundreds of classes? Does it seem acceptable?

A little more complicated. If the requested data may be forms, Json, Xml, and files, and the response data may be these or even more data, then face such a requirement, what if I continue to use the template method? Think over it. inheritance can achieve code reuse, but in the face of the above requirements, if only inheritance is used, can it achieve efficient reuse (especially in the case of single inheritance )? Please note that processing request data and processing response data here are two change points, and two change points are independent changes, that is, the request data may be a simulated form, the response data may be a Json, Xml, an Image (Verification Code), or even a file or a compressed package (multiple files ). If the request data is in Json, Xml, and other formats, the response data may still be in the above situation. That is to say, once Multiple change points change independently, the single inheritance will be instantly restored.

It is a waste of energy. If you put a large amount of reusable code into the base class (the access permission is protected), and then you can choose to call the code in the subclass, it seems that it can also solve the problem of "independent changes of Multiple change points" mentioned above, but as a result, the base class will be extremely bloated and the flexibility will be greatly reduced, in addition, the coupling between multiple data processing methods increases exponentially [This is a more failed design ].

  

Looking back, at the beginning, the template method is good. Even if an interface corresponds to a class, it is acceptable to some extent. After all, each interface is independent and corresponds to a single class, complying with the single responsibility principle. If multiple interfaces have similarity (Business similarity), you can easily use the inheritance system of the template Method and create a new Method, A simple factory mode can be built to hide more implementation classes and simplify the use. It was only because of the "independent changes of multiple changes" that caused some problems. Otherwise, the design of the former can still work well. So how can we solve the problem of "Multiple change points change independently? A: Policy. Use the policy mode for each change point. So far, this powerful HttpUtil has come to an end. At least, in actual application, the Code constructed in the form of template method + policy mode has already met the existing requirements. If you have any other questions, please leave a message.

 

2. After I proposed the template method, I asked my colleagues what they thought? His answer is: It seems that after the policy mode is used, the template method is not needed, or the template method can be completely replaced by the Policy mode. At that time, I also had a misconception that the template method = a set of policy modes. In terms of the Code effect, the template method and a group of policy modes can indeed be classified, but are they "equal? If they are equal, then the template method does not seem necessary? Let's take a look at the meanings of the two:

The intent of the template method: Define an algorithm flow to implement and delay specific steps to subclass. This allows you to customize specific steps in the process through different subclasses without changing the algorithm flow.

Policy pattern intent: enable different algorithms to be replaced with each other without affecting the use of clients.

As shown in the figure above, the template method emphasizes the following:

1) define a line (algorithm flow). Multiple online points can be changed (the specific implementation is completed in the subclass). Multiple online points must be executed, and must be executed according to the specific process.

2) algorithm flows have only one entry, and access to points is restricted. [protected virtual functions are usually used to define variable points ].

The policy mode pays more attention to: a "policy" is an overall (complete) algorithm, and the algorithm can be replaced as a whole. The template method can only be replaced with specific points, and the algorithm flow is fixed and immutable.

  

In such details, the template method and a group of policy modes cannot be equal.

 

3. In the face of this "old" model-the template method, it seems that the "preferential use of object combination, rather than inheritance" policy mode is very arrogant? I personally think that there are various advantages and disadvantages, but they have different adaptation scenarios. When "Multiple change points change independently", the policy mode is needed to save the problem. Otherwise, the original architecture can be well maintained and expanded, do I have to find the perfect answer for the moment? No.

 

Iv. Appendix references: design patterns:

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.