Moe's Tech Blog

[Design Pattern] Template Design Pattern 본문

Software Design Pattern/Notes

[Design Pattern] Template Design Pattern

moe12825 2022. 8. 24. 12:43

Behavioral Design Patterns

  • focuses on ways that individuals objects collaborate to achieve a common goal
    • each object you make is a piece of a larger solution
    • in order for each to work effectively, it needs to have a set purpose
      •  e.g. each person working at a company
        • if the people of the company didn't have any predefined roles, there would be no way to make sure that each of their functions was being executed
        • The company would be less effective and not nearly as organized
    • Example of behavioral pattern in real world
      • 1. Directions on making soup or mixing a drink from powdered ingredients
        • Both have similarly described steps to
          • 1. get a container
          • 2. empty the ingredients into the container
          • 3. add water
          • 4. stir
          • 5. prepare
        • The steps are ordered the same way with some identical steps and some different in implementation
      • 2. Say you are an executive chef in a large chain of restaurants that serve pasta
        • you want dishes to be consistent at all the restaurants location in the chain
        • your two most popular dishes are spaghetti with tomato sauce and meatballs and penne noodles with Alfredo sauce and chicken
        • Both dishes require:
          • 1. boiling water
          • 2. cooking the pasta
          • 3. adding the sauce
          • 4. adding the protein
          • 5. garnishing the plate
        • some of the steps are implemented differently depending on what dish you are making
          • each dish has a different protein, sauce and garnish

 

Template Method

  • defines an algorith's steps generally deferring the implementation of some steps to subclasses (for steps that are unique to each)
  • is a behavioral pattern concerned with the assignment of responsibilities
  • The template method name must be the same way for both subclasses 
    • Example
      • 1. Self-driving vehicle for car and motorcycle
        • The template method for the two subclasses are "driveToDestination()"
        • The difference between the two is the way each steer it's wheels
  • is best used when you can generalize between two classes into a new super class
    • Think of it like another technique to use when you notice two separate classes with very similar functionality in order of operations
    • after generalization, the objects can be reused more effectively

 

How to Implement UML for Template Method Pattern

  • here makeRecipe() method calls for other methods for the steps of the recipe
    • some steps are common like
      • 1. boiling water
      • 2. cooking paster
      • 3. drain and plate
      • and these are methods of the PastaDish class
  • Some methods are special for each dish such as:
    • 1. addPasta
    • 2. addSauce
    • 3. addProtein
    • 4. addGarnish
    • So these are abstract methods of PastaDish class
    • In each subclass, they must provide their own versions of addPasta, addSauce, addProtein, and addGarnish
public abstract class PastaDish {
    public final void makeRecipe() {
        boilWater();
        addPaster();
        cookPasta();
        drainAndPlate();
        addSauce();
        addProtein();
        addGarnish();
    }

    protected abstract void addPasta();
    protected abstract void addSauce();
    protected abstract void addProtein();
    protected abstract void addGarnish();

    private void boilWater() {
        System.out.println("Boiling water");
    }
}
public class SpaghettiMeatballs extends PastaDish {
    protected void addPasta() {
        System.out.println("Adding Spaghetti");
    }

    protected void addSauce() {
        System.out.println("Adding tomato sauce");
    }

    protected void addProtein() {
        System.out.println("Adding meatballs");
    }

    protected void addGarnish() {
        System.out.println("Add parmesan cheese");
    }
}

public class PenneAlfredo extends PastaDish {
    protected void addPasta() {
        System.out.println("Adding penne");
    }

    protected void addSauce() {
        System.out.println("Adding alfredo sauce");
    }

    protected void addProtein() {
        System.out.println("Adding chicken");
    }

    protected void addGarnish() {
        System.out.println("Add parsely");
    }
}
  • Here, final keyword means that the method declared cannot be overriden by subclasses
    • this means neither specific subclass can have its own version of the method of same name
  • The addPasta, addProtein, addSauce, and addGarnish methods are called in the makeRecipe() tempalte method found in the PastaDish superclass
    • makeRecipe() is callable in SpaphettiMeatballs and PenneAlfredo classes

 

Summary

  • Template method pattern can be useful if you have two classes with similar functionalities
    • template method pattern can be used to consolidate algorithms into one place
    • The difference in the algorithm would be done through calls to abstract methods whose implementations are provided by the subclasses