Learn Pattern Template

The Template method template defines a framework of an algorithm in a function, transferring its implementation to subclasses. The Template Method template allows the subclass to redefine the behavior of an algorithm, without changing the algorithm structure, which is suitable for creating different types of robots.

Real problem

You get a contract about auto robots assembling cars. No problem, the programmers said and started to work right away.

Create the first robot

After a few days, programmers created a software, it was simple enough to meet the requirements. The robot class starts with an initialization function as follows:

And there are some functions that the robot can do, for example to start the robot, you use the start function, let the robot work, you use asemble assembly function, …

And all you need is a method, named go () , which will make the robot work by calling other functions in the robot class.

And the rest is just running the program by calling go () function as follows:

And your robot will operate as follows:

Great, the project still works well, and programmers are satisfied with that.

Create Robot with Template Method design

Next, you get a new contract, a software that makes a robot make baked goods. And programmers begin to hesitate, will have to rewrite all software from the beginning. This is the right time to talk about Template Method design.

There is a problem that programmers face, they have a robot that assembles cars, but now they need a baking robot. And they have to rewrite the entire source code.

The baking robot has a number of functions similar to a car assembly robot such as the start () , stop () function , but it also has some differences as the assemble () function will not display as Getting a carburetor … instead, flour and sugar … is Getting.

Template Method design templates can solve this problem. This pattern says, you can write a method, used to define a series of algorithms, like the go function you saw before, to run a variety of functions for robots.

Then you put this function in a bwangf template set to allow subclasses to redefine the algorithm steps in the required way. In this case, to be a baking robot, you'll rewrite the functions geParts () and asemble () . You should use the Template Method template when you have an algorithm created by many steps, and you want to customize some steps in it. Note that if you want to rewrite everything from scratch, you don't need to use a template.

Create robots with a Template set

By calling the go () function , the set of algorithms will be executed. To customize in the legacy class, you just need to rewrite some steps you want.

That's the idea behind the Template Method design – A multi-step cell function will be customized by the subclass. In case you need 2 robots, 1 robot assembles a car and 1 robot bake, what will it be like?

You start by creating a set of Template templates in an abstract abstract class, called RobotTemplate

If a robot uses these methods correctly, such as the start () and stop () functions , we don't need to rewrite them and just change the ways in which the robot changes.

For example, you can use the Abstract RobotTemplate to create a car assembly robot. You have inherited from abstract RobotTemplate in a new class, this class is set to AutoMotiveRobot .

This car assembly robot needs to redefine some functions like getParts () and assemble () as follows:

You can customize the source code based on the template by adding some functions, such as the initialization function that takes the name of the robot, and the getName function returns this name.

You can also customize the go function inherited from the template for baking robots. You create a new class named CookieRobot that inherits from the abstract RobotTemplate . You can write CookieRobot class by rewriting the getParts () and assemble () functions as follows:

You used the go () function from the template set to create two new classes AutomotiveRobot and CookieRobot , and you rewrote some steps in the algorithm to fit each robot without having to rewrite these two new classes. from the beginning.

Check robot creation

Create 2 objects of AutomotiveRobot and CookieRobot class and call go () function as follows:

And you will see the program running as you wish: D.

You use the Template Method design template when you have an algorithm with multiple steps and you want to allow them to be customized in the subclass. So easy. By rewriting the declared functions in the abstract class, you will change the way you want.

Template Method design is a great idea when you have a multi-step algorithm that you can customize it yourself.

So we have just learned about Template Method (Template Pattern) design pattern. Hopefully the article will help you in programming. ?

Share the news now