Thursday, June 24, 2021

Template Method Pattern - Skeleton is Defined in Base Class

Template Method Design Pattern
Photo Courtesy Unsplash

What is the Template Method Design Pattern?

The Template Method Design Pattern defines a sequence of steps of an algorithm and allows the subclasses to override the steps but do not allow to change the sequence. The Key to the Template Design Pattern is that we put the general logic in the abstract parent class and let the child classes define the specifics.

Template Method pattern falls under the behavioural design pattern, is one of the easiest to understand and implement. This design pattern is used popularly in framework development and also helps to avoid code duplication.

An abstract class contains the templateMethod which should be made final so that it cannot be overridden. This template method makes use of other operations available in order to run the algorithm but is decoupled for the actual implementation of these methods. Concrete class implements all the methods required by the templateMethod that were defined as abstract in the parent class.

Pattern Implementation

As am a coffee person, I will take an example of preparing the coffee using a template method pattern which should allow to understand the pattern easily instead of taking some framework implementations such as AbstractController, RequestProcessor, HttpServlet, InputStream, OutputStream etc.

public abstract class CoffeeMaker {
   protected boolean sugarFree = false;

   public void prepareCoffee() {
      boilWater();
      addMilk();
      if (!isSugarFree()) {
         addSugar();
      } else {
 System.out.println("- No Sugar")
      }
      addCoffeePowder();
      System.out.println("- Coffee is Ready!!!");
   }

   public final void boilWater() {
      System.out.println("- Boiling Water");
   }

   public boolean isSugarFree() {
      return sugarFree;
   }

   public void setSugarFree(boolean sugarFree) {
      this.sugarFree = sugarFree;
   }

   abstract void addMilk();
   abstract void addSugar();
   abstract void addCoffeePowder();
}

Now we will create 2 concrete classes which will provide different implementations.

public class CothasCoffeeMaker extends CoffeeMaker {
   @Override
   public void addMilk() {
      System.out.println("- Adding Milk");
   }

   @Override
   public void addSugar() {
      System.out.println("- Adding Sugar");
   }

   @Override
   public void addCoffeePowder() {
      System.out.println("- Adding Cothas Coffee Powder");
   }
}

public class BruCoffeeMaker extends CoffeeMaker {
   @Override
   public void addMilk() {
      System.out.println("- Adding Milk");
   }

   @Override
   public void addSugar() {
      System.out.println("- Adding Sugar");
   }

   @Override
   public void addCoffeePowder() {
      System.out.println("- Adding Bru Coffee Powder");
   }
}

Now it's time to write the main method to execute the above code and see the output of the two variations of the coffee.

public class CoffeeMakerExample {
   public static void main(String[] args) {
      System.out.println("Cothas Coffee Preparation")
      CoffeeMaker coffeeMaker = new CothasCoffeeMaker();
      coffeeMaker.prepareCoffee();

      System.out.println("Bru Coffee Preparation");
      coffeeMaker = new BruCoffeeMaker();
      coffeeMaker.setSugarFree(true);
      coffeeMaker.prepareCoffee();
   }

}

The CoffeeMaker class is an abstract class containing the algorithm skeleton. The prepareCoffee() is the method that contains the process steps. The boilWater() method is common step for the process of any coffee preparation and no customization is required, hence it has been made as final so the subclasses will not override and provide a different implementation. 

We have two subclasses CothasCoffeeMaker and BruCoffeeMaker which follows the same preparation process but implementation are given at each individual subclass.

If you execute the above program, you should see the following output.

Cothas Coffee Preparation
- Boiling Water
- Adding Milk
- Adding Sugar
- Adding Cothas Coffee Powder
- Coffee is Ready!!!

Bru Coffee Preparation
- Boiling Water
- Adding Milk
- No Sugar
- Adding Bru Coffee Powder
- Coffee is Ready!!!

Conclusion

  • The pattern promotes the code reusability and decoupling, but at the expense of using inheritance.
  • The pattern adhers to the Single Responsibility and Open/Closed principles of S.O.L.I.D - Design Principles.

Reference

Tags: , , , ,
Location: Mysuru, Karnataka, India

0 comments:

Post a Comment

Featured Post

Benefits & Best Practices of Code Review

Photo by Bochelly Code reviews are methodical assessments of code designed to identify bugs, increase code quality, and help developers lear...