How do you put an elephant in the fridge

First open the fridge door, then herd the elephant in, and finally, Piaji, close the fridge door 🐘

So we’ve got a wild elephant

Is the fixed three steps of the elephant somewhat similar to the template method of design patterns?

In this article, you can find out what kind of coding techniques the template method provides, in which scenarios it can be used, whether it is used in the framework, and how the design pattern is used in real business scenarios.

directory

  • define
  • The sample analysis
  • Business use
  • Framework using

It takes about 6 minutes to read this article ✍️

define

The template approach, as a behavioral design pattern,

The essence is to define the framework of an algorithm and allow subclasses to provide implementations for one or more of the steps

The algorithm steps can be redefined to the maximum extent possible without intruding on the internal logic of the subclasses

The scenario is that you can implement the invariant parts of an algorithm (open refrigerator door, close refrigerator door) at once, leaving the variable behavior to subclasses (🐘 for elephant, 🐒 for monkey, 🐜 for ant).

The advantage of this design pattern is that it can improve the code reuse, scalability, and conform to the open and closed principle. The disadvantage is that it increases the difficulty of understanding the system, and it is realized by inheritance. Once a new abstract method is added, the code of other subclasses must be changed again

Listen to this a lot of definitions, is not feeling in the clouds in the fog

Now, let’s go back to the elephant example, and you’ll understand

The sample analysis

Let’s first define an abstract class, ARefrigeratorHandle

To implement our prescribed on, off, and off steps

(Hook methods will be added here to increase the extensibility of the program, see the comment for details)

Next, implement PutElephant

Next, implement PutMonkey.

You can observe that they all inherit the ARefrigeratorHandle abstract class

Everything is there except testing

Let’s write a crude test class

Ok, the test has passed, we have simply mastered the use of template method, only one step away from promotion and salary increase

Business use

The business scenario looks like this

In the financial consumption scenario, after the user makes repayment, we need to inform the corresponding capital party of the changed repayment plan in time

In our system design, repayment plan is a set of standard fields

The repayment plan field accepted by the capital side of 7788 is different

For example, in our system design, the plan period is called planPeriod, while the fund party A is called plan-period and the fund party B is called period

So before we push, we need to transform the fields

The overall steps are validation plan -> Transform fields -> Unified push

Framework using

AbstractList AbstractList AbstractList AbstractList AbstractList AbstractList AbstractList AbstractList AbstractList

Now, if we look at line 117, we can see that the implementation of GET is all in the hands of subclasses, so let’s look at the subclasses

 abstract public E get(int index);
Copy the code

You can observe that one of the classes we’re familiar with is ArrayList

Take a look at the get method in ArrayList

    public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }
Copy the code

That’s the implementation of GET in ArrayList

Of course, you can also observe the implementation of GET in other subclasses

This is a simple example, and you can explore the use of template methods in HttpServlet and BaseExecutor


End of article 🤣

If this article helped you, give it a thumbs up

More sharing can be found on Skow’s wechat official account

You can get interview questions/technical documents/e-books, etc


Phase to recommend

The interviewer and I were hooked on Maven 11 exceptional best practices from Dachang Factory design patterns, do you know these issues?