Policy mode or template method mode?

Source: Internet
Author: User

Before doing a product, there is an important scene-generate inquiry, the source of the website, message, PM, manual, etc., considering the current code is a bit messy, ready to do the next refactoring, in the end is the use of strategic mode or template mode. Here's a simple analysis. The process of inquiry generation includes the following steps: Construct Inquiry object, calculate inquiry intention degree, create buyer, get intention goods, create inquiry, the difference is to construct inquiry object according to the parameters passed in.

The inquiry generation of each source is considered to be a large algorithm, which can be applied to the policy pattern, but the problem is that there will be a lot of duplicated code, such as the calculation of the inquiry intention, the creation of the buyer and so on, violating the code reuse principle.

From the scene, it seems more in line with the template model, the base class defines the algorithm framework, first constructs the inquiry object, then calculates the inquiry intention degree, then creates the buyer ... The construction of the inquiry object as a template method is implemented by each subclass. But the parameters passed when the client goes to call are not consistent, which makes the parameters of the interface difficult to cure.

According to the definition of the two modes, can summarize its applicable scenarios, the strategy model for each algorithm is very different, there is not much common place, so as to ensure that there is no duplicate code between the different algorithms, so in the use of this mode before the service refinement, the large granularity of the service is broken into fine-grained While the template method pattern is suitable for each algorithm's overall process is basically consistent, the difference is only in the specific sub-process implementation, before applying this mode to ensure that the total process is relatively stable, in the specific sub-process if there is the same part, you can also combine the strategy model, the implementation of the sub-process is defined as a strategy.

Think about it, the above inquiry generated can not adopt a policy or template mode, not designed for the pattern, because the different sources of inquiry generation are from different requests, you can receive the request when the object is constructed, and then passed to a service layer implementation class to handle, so the code is clear, the structure is simple.

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.