Connect two template method mode?

zhaozj2021-02-08  447

Yesterday, in the customer site, and the technical staff of a company in Hong Kong were discussed a relatively strange technical solution. Carefully think about it at night, discovering this program to abstract the following problems with the concept of design patterns:

There are two template modes that have a Template method that implements a specific algorithm. Each step of the algorithm can be defined by themselves, but the algorithm has been fixed. Then, in the case where the template method is not allowed, only the specific step is allowed, how to connect these two algorithms to complete a new functional process?

For example, the algorithm A on the left of the figure below contains three steps executed, and the algorithm B contains four steps. How to implement the primitiveOperation in Concreteclassa and ConcreteClassB ... How to achieve the flow of A1, B1, A2, A3, B2, B3, B4? Or another process (but the processes of A and B remain unchanged)? Even the flow of some steps, such as A1, A2, B1, A3, B4?

Obviously, some synchronization means is required between the two sets of algorithms. Our solution is to define new processes with a set of Agent classes while making them an intermediary of both A and B algorithms. This set of Agent class is substantially equivalent to a strategy mode. The abstract Agent class contains a Process method, the specific Agent class defines and perform specific processes in the Process method. At the same time, the progress status information of the algorithm A and algorithm B is saved in the Agent class (if the algorithm has progressed which step, etc.).

In the specific implementation level, the Process method is a main thread of a service program that is running in the background. Its basic idea is based on a specific process definition, the status of the algorithm A and algorithm B are determined whether the algorithm A or algorithm can be made B Enter the next step. The Agent itself is a bit like an algorithm gate, when the gate is opened, the algorithm can continue, and it must be blocked.

In order to implement gate function, the Agent class provides a CANCONTINUE method, and the specific algorithm step can be queried by this method to continue execution.

With an Agent class, when you implement specific algorithm steps, we need to add the following features for PrimitiveOperation ... You can do this before execution, if you can do this, if you can execute, if you can block it ( Depending on the implementation of the template method, the blocking operation is also different: or use the SLEEP mode to return to the current step. If you try again later, each step is executed, after each step is executed, it will pass the Agent class. Complete the status update operation.

The storage algorithm state is the core of this idea. In order to save the unique status information of the system, the Agent should also be a single piece. In addition, if the algorithm A, algorithm B, Agent are not deployed on a computer, or the execution of the algorithm A, B requires a long time, the middle or even the user's participation, we must save the status of the algorithm in the database, file, etc. Permanently stored to ensure the integrity of the algorithm. Therefore, class PersistentState used to save algorithm is quite similar to the persistence layer of an operational database. - This method of using a persistent layer, probably talking in the textbook, ensuring the algorithm of distributed transaction integrity is similar.

The entire idea is shown below:

Is it too abstract, I have not said specific applications. What kind of system is "not allowing to change the template method"? In fact, we encountered problems that system integration projects often encounter. We must use two ripe software platforms to build a system, a set of platforms responsible for background service, achieving customizable workflow management functions; a set of platforms are responsible for foreground data processing, users participate, process, but can customize data Output format. Both platforms provide a secondary development interface with a plug-in, we can implement a specific operation of the plugin to complete the specific link, but define and drive the functional process is the platform itself instead of the plugin. In this case, the platform controls the entire process, which is similar to the template method, we can only develop specific operations, similar to implementing PrimitiveOperation ... if you don't know who, no one knows each other, can not be compatible with each other The platform itself is a large company's mature software product, which cannot be modified. We have to complete the entire project, and to achieve a specific process according to the user's requirements - Isn't this a problem with the two template mode mode?

I don't know that our solution is not a good choice. In any way, we successfully connect two systems before and background, and can always modify the business process at any time according to the needs of users, this result is always good. To talk about the cause of this, it is quite angry. It is clear that you can choose to be compatible with each other, the background system, the client in order to settle the interests between the relationship between the relationships, and bias two uncomprocized software (old beauty software, price) Still very expensive), but also give us such a problem. However, the most attractive place for system integration project is to solve smoothing problems between different systems. The harder the problem, the more powerful programmers naturally.

转载请注明原文地址:https://www.9cbs.com/read-160.html

New Post(0)