A little thought about the template method and the Strategy model

Source: Internet
Author: User

The original idea of the essay should be considered two or three years ago. At that time chatting with a former colleague, I did not know how to chat to the HTTP access.

One, I remember he and I said the first sentence, probably: there is no already packaged, relatively strong httputil. may also be affected by the business (interface internally), I was exposed to the HTTP access, mostly compared to the "specification", at least one interface constraints in the Convention some things, not one will pass JSON, return JSON, one will also pass XML, return XML, even more wonderful is, upload a file, returns 0 or 1. If this is the case, Httputil still be able to adapt to various situations conveniently and flexibly, I think this util is absolutely powerful. Unfortunately, my colleague is precisely to meet the needs of such wonderful (and a number of third-party system docking).

Remember when, in the face of this problem, my first consciousness: the template method, and seems to have seen how to use this way. Because: the entire HTTP access process is not the same:

1. Create the Request object according to the URL,

2, set the request parameters, such as: Do not use the cache, set the header, cookies, time-out and so on.

3, send data, such as: Post method.

4. Create a Response object

5, receive data.

6. Get Response parameters

7, at the beginning of the above process, whether as a util, or as a strong framework, it is possible to do exception handling, logging and other additional processes.

Define an algorithm backbone, defer the detail implementation of the step to the subclass, make. Reuse code in the base class to form an inheritance system, in the end, only need to be in the lowest class, overriding the function to handle three things, one is to provide the request address, the second is to provide the request data, the third is to process the response data, of course, the default is that the request method has been encapsulated. This idea seems to be a good idea, huh?

But what happens if there are hundreds or thousands of interfaces in the system that need docking? Hundreds of classes? Seems to be acceptable too?

More complicated, if the requested data may be forms, Json, Xml, files, and the response of the data may be these, or even more, so in the face of such a requirement, if you continue to use the template method? Think about it, inheritance can implement code reuse, but in the face of the above requirements, if only using inheritance, can achieve efficient reuse (especially in the case of single inheritance)? Note that processing the request data here and processing the response data is two points of change, and is a separate change of two change points, that is, the request data may be a form of impersonation, and the response data may be a JSON, a section of XML, an image (verification code), or even a file or a compressed package (multiple files )。 If the request data is in the form of JSON, XML, and so on, the response data may still be the case. It is also said that once a plurality of changes in the independent change, the single-inheritance moment is returned to the prototype.

Waste a little more thought, if you put a lot of reusable code into the base class (access rights are protected), and then selectively make calls in subclasses, it seems to solve the above mentioned "multiple change point independent change" problem, but then the base class will be extremely bloated, flexibility will be greatly reduced, And the coupling between various data processing methods will rise exponentially "This is a more failed design."

  

Look back at the beginning, template method is also good ah, even if a docking interface corresponding to a class, in a way is acceptable ah. After all, each interface has an independent, individual class to correspond to, in line with a single responsibility principle. If more than one interface is similar (referring to business similarity), it is convenient to use the inheritance system of the template method, just create a new method, you can build a simple Factory mode, so as to hide more implementation classes, to achieve the purpose of simplifying. Only later due to "multiple change point independent change", only to cause a number of problems, if not, the former design can still work well. So how to solve the problem of "multiple change point independent change"? A: strategy, for each change point, use the policy mode alone. At this point, this relatively powerful httputil, is a temporary. At least, in the actual application, the template method + policy mode such organization, the code has been built to meet the existing needs, if there are other questions, welcome message.

Second, when I put forward the template method, I asked colleagues, what is the idea? His answer is that it seems that the template method is not needed after using the strategy mode, or that the template method can be completely replaced by the policy mode. And I also had a mistaken cognition, template method = A set of policy patterns. In terms of code effects, a template method and a set of policy patterns are really equal, but are they really "equal"? If they are equal, then the template method does not seem necessary? Let's take a look at the two intentions first:

The intent of the template approach: Define an algorithmic flow that will implement and defer specific steps to subclasses. Makes it possible to implement specific steps in a "custom" process with different subclasses without changing the algorithm flow.

The intent of the policy pattern: to make different algorithms interchangeable with each other without affecting the client's use.

In intent, the template approach emphasizes:

1) define a line (algorithmic flow), multiple points on the line can be changed (the implementation is done in the subclass), many points on the line must be executed, and must be executed according to a specific process.

2) The algorithm flow has only a unique entry, and the access to the point is limited to "usually define a variable point with a protected virtual function".

The strategy mode is more focused on: a "strategy" is a whole (complete) algorithm, the algorithm can be replaced by the whole. The template method can only be replaced by a specific point, and the algorithm flow is fixed and immutable.

  

In this detail, the template method and a set of policy patterns cannot be equated.

Third, in this "old" mode--template method, it seems that "priority to use the object combination, rather than inherit" strategy mode is very high-toed? I personally still feel, each has the excellent lack, just each adapts the scene to be different. When encountering "Multiple change point independent change", then need to rescue the strategy mode, if not, the original architecture can be well maintained and extended, it is necessary to go to the trouble, to find the temporary most perfect answer? It doesn't seem to be right.

Iv. attached reference to "design pattern fine Solution":

A little thought about the template method and the Strategy model

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.