How do you fit an elephant in a refrigerator

First open the refrigerator door, then drive the elephant in, and finally, Piaji, close the refrigerator door 🐘

So we have a wild elephant

Does the elephant’s three steps resemble the template approach to design patterns?

In this article, you will find out what coding techniques the template approach provides, what scenarios you can use it in, whether the template approach is used in the framework, and how the design pattern is used in real business scenarios.


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

It takes about 6 minutes to read this article ✍️


As a behavioral design pattern, the templating approach

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

Algorithmic steps can be redefined to the greatest extent possible without intruding into the internal logic of subclasses

The applicable scenario is that the invariant part of an algorithm can be implemented once (open the refrigerator door, close the refrigerator door), and the variable behavior is left to subclasses to achieve (with elephants 🐘, with monkeys, with ants).

The advantage of this design pattern is that it can improve the code’s reusability, extensibility, and conforms to the Open Closed Principle. The disadvantage is that it increases the difficulty of understanding the system, and the design pattern is realized through inheritance. Once a new abstract method is added, other subclasses of code have to be changed again

Listen to this a lot of definitions, do not feel in the clouds and fog

Now, let’s go back to the elephant example so you can understand

The sample analysis

Let’s start by defining an abstract class ArefrigeratorHandle

To implement the open, open, and close steps that we specify

(There will be a hook method added to increase the extensibility of the program, details can be seen in the comments)

Next, go to implement putting the PutElephant

Next, implement putMonkey (here’s how the hook method works).

You can observe that they all inherit the abstract class ArefrigeratorHandle

Everything is ready, but the test

Let’s write a simple test class

OK, the test has passed, we have simply mastered the application of the template method, and we are 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 has repaid the loan, we need to inform the corresponding financier of the changed repayment plan in time

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

The reimbursement plan field that 78 78 capital square place accepts is different again

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

So before we push, we need to convert the fields

The overall step is to check the plan -> conversion field -> unified push

Framework using

If we search for abstractList in Idea, and many classes implement it, we can see that it is used to extend the List

Next, when we look at line 117, we can see that the concrete implementation of get is given to subclasses. Let’s look at the subclasses

 abstract public E get(int index);

You can observe a familiar class called ArrayList

Take a look at the get method in the ArrayList

    public E get(int index) {

        return elementData(index);

That’s the implementation of GET in an ArrayList

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

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

End of article 🤣

If this article has helped you, then give it a like

More sharing can be found on SKOW’s WeChat official account

Public number reply “information” can obtain big factory interview questions/technical documents/e-books and so on

Phase to recommend

The interviewer was Maven to me