Behavioural PAttern
- Anand Nerurkar
- May 2, 2024
- 2 min read
Behavioural PAttern
===
Template Method pattern
===
The template method pattern is a behavioral class pattern. A behavioral class pattern uses inheritance for distribution of behavior.
In the template method pattern, a method (the 'template method') defines the steps of an algorithm. The implementation of these steps (ie, methods) can be deferred to subclasses. Thus, a particular algorithm is defined in the template method, but the exact steps of this algorithm can be defined in subclasses.
The template method is implemented in an abstract class. The steps (methods) of the algorithm are declared in the abstract class, and the methods whose implementations are to be delegated to subclasses are declared abstract.
Here is an example of the template method pattern. Meal is an abstract class with a template method called doMeal() that defines the steps involved in a meal. We declare the method as final so that it can't be overridden. The algorithm defined by doMeal() consists of four steps: prepareIngredients(), cook(), eat(), and cleanUp(). The eat() method is implemented although subclasses can override the implementation. The prepareIngredients(), cook(), and cleanUp() methods are are declared abstract so that subclasses need to implement them.
package com.cakes;
public abstract class Meal {
// template method
public final void doMeal() {
prepareIngredients();
cook();
eat();
cleanUp();
}
public abstract void prepareIngredients();
public abstract void cook();
public void eat() {
System.out.println("Mmm, that's good");
}
public abstract void cleanUp();
}
The HamburgerMeal class extends Meal and implements Meal's three abstract methods.
package com.cakes;
public class HamburgerMeal extends Meal {
@Override
public void prepareIngredients() {
System.out.println("Getting burgers, buns, and french fries");
}
@Override
public void cook() {
System.out.println("Cooking burgers on grill and fries in oven");
}
@Override
public void cleanUp() {
System.out.println("Throwing away paper plates");
}
}
The TacoMeal class implements Meal's three abstract methods and also overrides the eat() method.
package com.cakes;
public class TacoMeal extends Meal {
@Override
public void prepareIngredients() {
System.out.println("Getting ground beef and shells");
}
@Override
public void cook() {
System.out.println("Cooking ground beef in pan");
}
@Override
public void eat() {
System.out.println("The tacos are tasty");
}
@Override
public void cleanUp() {
System.out.println("Doing the dishes");
}
}
The Demo class creates a HamburgerMeal object and calls its doMeal() method. It creates a TacoMeal object and calls doMeal() on the TacoMeal object.
package com.cakes;
public class Demo {
public static void main(String[] args) {
Meal meal1 = new HamburgerMeal();
meal1.doMeal();
System.out.println();
Meal meal2 = new TacoMeal();
meal2.doMeal();
}
}
The console output of the execution of Demo is shown here.
Getting burgers, buns, and french fries
Cooking burgers on grill and fries in oven
Mmm, that's good
Throwing away paper plates
Getting ground beef and shells
Cooking ground beef in pan
The tacos are tasty
Doing the dishes
As you can see, the template method design pattern allows us to define the steps in an algorithm and pass the implementation of these steps to subclasses.
Mediator Pattern
Iterator Pattern
chain of responsibility
observer pattern
command pattern
strategy pattern
Comments