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.