The template method behavioral design pattern is to define an algorithmic program as a skeleton of operations and leave the details to be implemented in subclasses. These steps can be abstract methods that don’t require implementation while declaration time. That’s why they will be implemented in subclasses. Subclasses will override only the steps defined in the base class without changing the overall algorithm’s structure.
Uses of Template Method:
- To reduce the duplication of code by splitting the code into similar ‘steps ‘ or ‘sections’.
- To extend the code easily with controlling of core parts of it, this is useful.
- When you have a set of steps (algorithm), that might have more than one implementation. The base set of steps will be defined in the base class, but subclasses can create their own implementation of each step by replacing the base class steps.
Let us take an example of manufacturing cars and motorbikes. There are common four steps to creating vehicles:
- addEngine()
- addWheels()
- paintVechicle()
- sellVechicle()
As we know, every vehicle has different engines and wheels. So, the code to implement these properties will be different. We need to just declare addEngine() and addWheels() methods as abstract methods in the base class. The actual implementation of these methods will be performed in subclasses.
The process of painting/selling all types of vehicles is the same. So, we don’t need to rewrite the code again and again for every vehicle. We will declare and define paintVechicle() and sellVechicle() methods in parent class.
Parent class:
<?php
abstract class VehicleManufacturing
{
abstract protected function addWheels();
abstract protected function addEngine();
protected function paintVehicle()
{
echo " Painting of vehicle\n";
}
final protected function sellVehicle()
{
echo "Vehicle Selling\n";
}
final public function template()
{ // this is the template method
$this->addEngine();
$this->addWheels();
$this->paintVehicle();
$this->sellVehicle();
}
}
Subclass1:
class Car extends VehicleManufacturing
{
protected function addWheels()
{
echo " Four Car wheels added \n";
}
protected function addEngine()
{
echo " Car engine added\n";
}
}
Subclass2:
class Motorbike extends VehicleManufacturing
{
protected function addWheels()
{
echo “Two Motorbike wheels added\n";
}
protected function addEngine()
{
echo "Motorbike engine added\n";
}
}
Both subclasses are extending the main abstract template class “VehicleManufacturing”. Only two methods “addWheels()” and “addEngine()” need to be implemented in child classes.
We always only have the same steps, in the same order, that are defined in the template() method. So, we will call only the template() method for creating both types of vehicles:
<?php
$motorbike = new Motorbike;
$motorbike->template();
$car = new Car();
$car->template();
?>
StudySection gives an opportunity to beginners and experts in .NET framework to go through StudySection’s .NET Certification Exam and get a .NET certification for enhancement of career in programming. If you have knowledge of the .NET framework then you can get a certificate through an online exam at StudySection.