1 / 20

CS 210

CS 210. Template Method Pattern October 24 th , 2006. Look at the simple barista example. Eclipse sample code. Abstracted Recipe method. Abstract base class. public abstract class CaffeineBeverage { final void prepareRecipe() { boilWater(); brew(); pourInCup();

oni
Download Presentation

CS 210

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 210 Template Method Pattern October 24th, 2006

  2. Look at the simple barista example • Eclipse sample code

  3. Abstracted Recipe method

  4. Abstract base class public abstract class CaffeineBeverage { final void prepareRecipe() { boilWater(); brew(); pourInCup(); addCondiments(); } abstract void brew(); abstract void addCondiments(); void boilWater() { System.out.println("Boiling water"); } void pourInCup() { System.out.println("Pouring into cup"); } }

  5. Coffee and Tea in terms of the abstract caffeine class 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"); } } public class Tea extends CaffeineBeverage { public void brew() { System.out.println("Steeping the tea"); } public void addCondiments() { System.out.println("Adding Lemon"); } }

  6. Template Method Pattern defined The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

  7. Caffeine beverage example with hook • Eclipse code

  8. Design Principle The Hollywood Principle Don’t call us, we’ll call you.

More Related