Template Method design pattern (a)

Learning in front of friends all know that, so far, our issues are encapsulated turn around; we have already encapsulated object creation, method invocation, complex interfaces, ducks, pizzas ... that now what ?

We will be thorough encapsulation algorithm block, so that subclasses can be hooked into their operations in at any time. We even learn here and a design principle inspired by Hollywood influences.

Drink coffee or tea

Some people like to drink coffee, no coffee feels life is uninteresting; some people like to drink tea. Well, the same is tea, the two have nothing in common or what? In fact, both the brewing way very similar. such as

  1. Coffee brewing method
  • (1) The boiling water
  • (2) with boiling coffee
  • (3) the coffee in the cup
  • (4) with sugar and milk
  1. Tea brewing method
  • (1) The boiling water
  • (2) tea with boiling water immersion
  • (3) the tea poured into the cup
  • (4) with lemon

Coffee and tea converted code is as follows:

public class Coffee {
    void prepareRecipe() {
        boilWater();
        brewCoffeeGrinds();
        pourInCup();
        addSugarAndMilk();
    }

    public void boilWater() {
        System.out.println("Boling water");
    }

    public void brewCoffeeGrinds() {
        System.out.println("Dripping Coffee through filter");
    }

    public void pourInCup() {
        System.out.print("Pouring into cup");
    }

    public void addSugarAndMilk() {
        System.out.println("Adding Sugar and Milk");
    }
}

public class Tea {
    void prepareRecipe() {
        boilWater();
        steepTeaBag();
        pourInCup();
        addLemon();
    }

    public void boilWater() {
        System.out.println("Boiling water");
    }

    public void steepTeaBag() {
        System.out.println("Steeping the tea");
    }

    public void addLemon() {
        System.out.println("Adding Lemon");
    }

    public void pourInCup() {
        System.out.println("Pouring into cup");
    }
}

Tea is similar, so there will be places in the code and shared, and give us hints that, like the first edition of the design can be in the following manner:

Further design

So, look at the above code and class diagrams, coffee and tea as well as what other common? Let's start to start brewing method.

  1. Boil the water
  2. With hot coffee or tea
  3. The drinks poured into a cup
  4. Add the appropriate spices in the beverage

It can be seen, and 1 to 4 have been extracted, into the base class. 2 and 3 did not come out of, but they are essentially the same, but used in different beverages nothing.

So, we have the potential prepareRecipe () also abstracted it? Yes, we can, oh .

Abstract prepareRecipe ()

  1. The first problem we encounter is that coffee use brewCoffeeGrinds () and addSuagrAndMilk () method, while the tea use steepTeaBag () and addLemon ().

We found that, steep and brew bubbles are movements; addSugarAndMilk and addLemon are add spices, so they were unified into the brew () and addCondiments () be

  1. Now we have a new prepareRecipe () method, but it needs to be able to comply with the code. To do so, we start CaffeineBeveage (caffeine) superclass start
public abstract  class CaffeineBeverage {
    final void prepareRecipe() {

    }

    abstract  void brew();

    abstract  void addCondiments();

    void boilWater() {
        System.out.println("Boiling water");
    }

    void pourInCup() {
        System.out.println("Pouring into cup");
    }
}
  1. Finally, we need to deal with the coffee and tea. Both classes are now dependent on the superclass (caffeine) to handle the brewing method, we only need to deal with the brew and add spices to their own part:
public class Tea extends CaffeineBeverage {
    public void brew() {
        System.out.println("Steeping the tea");
    }
    public void addCondiments() {
        System.out.println("Adding Lemon");
    }
}

public class Coffee extends CaffeineBeverage {
    public void brew() {
        System.out.println("Dripping Coffee through filter");
    }
    public void addCondiments() {
        System.out.println("Adding Sugar and Milk");
    }
}

So what do we do in this process? FIG figurative book gives a small series to explain the:

Understanding Template Method

In fact, we have used in the process template method we have to learn, prepareRecipe () method is our template. why?

  • After all, it is a method
  • It is used as a template algorithm, in this case, the algorithm is used to make the coffee and tea

The method steps a template defines the algorithms and allow subclasses provide an implementation for one or more steps .

Let us drink tea

Let us step by step to make tea, how to track this template method works. You will learn some methods in the algorithm, the template method controls the algorithm. It allows subclasses to provide realization of certain steps

  1. First, we need a tea objects
Tea myTea = new Tea();
  1. Then we call the template method
myTea.prepareRecipe();
  1. Boil the water
boilWater();

这件事情是在超类中进行的
  1. Next we need to make tea, this thing that only subclasses can know how to do
brew();
  1. Now pour the tea cup; all drinks practices are the same, so this happened in the superclass
pourInCup();
  1. Finally, we add spices, as seasoning is unique to each drink, so by subclasses to implement it
addCondiments();

After the above process, we initial template method pattern to learn. In today's article tail, we define this template method pattern:
Template Method : Define the skeleton of an algorithm in a method, while deferring some steps to subclasses. Template methods such subclasses without changing the structure of the algorithm, some of the steps of the algorithm are redefined.

This mode is never to create a module algorithm. What is a module? As you can see, it is a template method. More specifically, the method of the algorithm is defined as a set of steps, any step may be abstract, it is responsible for the subclass implementation. This algorithm can ensure that the structure remains unchanged, while providing a partially achieved by subclasses.

Well, today's study will first come here. Today only a preliminary study of the template method pattern, then there will be more material way, the next time there or be square.

Love life, love of learning and perception, kicked love

Guess you like

Origin www.cnblogs.com/dimple91/p/10973159.html