In-depth parallel template method mode

xiaoxiao2021-03-05  24

First, the primer

This is a very simple model, but it is very widely used. The reason is simply because in this mode is only used inheritance.

Inheriting relationships have been deducted with "sin" hats due to their own defects. "Use the delegation relationship instead of inheritance", "Try to use the interface to implement rather than abstract class inheritance" and other experts warn, let us take these rookies to inherit "another eye."

In fact, inheritance still has a lot of own advantages. It is only more obvious that it seems to be abused. Reasonable use inheritance relationship, or can play a good role in your system design. The template mode mode is one of the examples.

Second, definition and structure

GOF Defines the skeleton of the algorithm in an operation to the template method (Template Method), and delays some steps into the subclass. Keep a subclass that can redefine certain specific steps of the algorithm without changing the structure of an algorithm. The structure of the algorithm here can be understood as a business process you designed according to requirements. The specific step refers to the links that may have variables on the content.

It can be seen that the template method mode is also designed to suit the impact of the system to the system. Using a template method enhances system scalability and minimizing changes in the system. This is clearly seen in the example below.

Let's see the structure of this simple pattern:

1) AbstractClass: Define a plurality of abstract methods for specific subclasses to implement them; and implement a template method to define an algorithm's skeleton. This template method not only calls the previous abstraction method, but also calls other operations, as long as it can complete its own mission.

2) ConcreteClass: Implement the abstraction method in the parent class to complete the steps associated with a specific subclass in the algorithm.

The following is a structural diagram of a template method mode. Take the picture on the "design pattern" directly:

Third, for example

Still find an example in the JUnit I have just analyzed the source code. TestCase in JUnit and its subclass is an example of a template method mode. The process of the entire test will be set in the abstract class of TestCase. For example, the SETUP method is executed first, and the test method is run before running the test method, and then Teardown will cancel the test settings. But what will you do in Setup, Teardown? Ghosts know! ! Therefore, the specific implementation of these steps is delayed into the subclass, which is in the test class you implement.

Come and see the relevant source code.

This is a template method for performing tests in TestCase. You can see that it is as mentioned in the previous definition, it has set the "algorithm" framework - first execute the setup method to perform the initialization, then execute the test method, and finally perform Teardown to release the resources you get.

Public void runbare () throws throwable {

setup ();

Try {

Runtest ();

}

Finally {

TEARDOWN ();

}

}

This is two methods used above. Unlike definitions, these two methods are not implemented as an abstract method, but two empty anomalities (known as hook methods). This is because we don't have to let the test program use these two ways to initialize and release resources. If it is an abstract method, the subclass must give it an implementation, no matter what it is not used. This is obviously unreasonable. Using a hook method, you can backwritten these methods in the subclass when needed.

Protected void setup () throws exception {

}

protected void teardown () throws exception {

}

Fourth, applicable

According to the above pair defined analysis, as described in the example, it can be seen that the template method is applied to the following cases: 1) The constant portion of an algorithm is realized one-time, and the variable behavior is left to the subclass.

2) The public behavior in each subclass should be extracted and concentrated into a public parent class to avoid code repeat. In fact, this can be said to be a good coding habit.

3) Control subclass extension. The template method is only in a specific point call, so it is only allowed to expand at these points. For example, the Runbare () method is only available in front of RUNTEST. If you don't want a child to modify the framework defined by your template method, you can use two ways: First, you don't reflect your template method in the API; Second, you can set your template method to Final .

It can be seen that using the template method mode can extract the public behavior of the code to achieve the purpose of multiplexing. Moreover, in the template method mode, the specific implementation of the subclass is controlled by the template method of the parent class. So when you implement subclasses, you don't need much understanding of your business process at all.

Five, summary

I've been in a hurry. I hope everyone will correct!

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

New Post(0)