Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Behavioral Patterns (3)

Similar presentations


Presentation on theme: "Introduction to Behavioral Patterns (3)"— Presentation transcript:

1 Introduction to Behavioral Patterns (3)
11/17/2018 Programming Design Patterns

2 Programming Design Patterns
Making Coffee and Tea public class Coffee { void prepareRecipe() { boilWater (); brewCoffeeGrinds(); pourInCup(); addSugarAndMilk(); } // … public class Tea { void prepareRecipe() { boilWater (); steepTeaBag(); pourInCup(); addLemon(); } // … Programming Design Patterns 11/17/2018

3 Abstract the commonality (1)
Is there code duplication in the two classes? Which two methods are the same? Need to abstract the commonality into a base class CLASSWORK Define a base class CaffeineBeverage force subclasses to implement the prepare() method let subclasses reuse the base class implementation for boilWater() and pourInCup() Programming Design Patterns 11/17/2018

4 Abstract the commonality (2)
CaffeineBeverage: prepareRecipe() abstract prepare() boilWater() // shared pourInCup() // shared Coffee prepare() brewCoffee() addSugar() Tea preprare() steepTeaBags() addLemon() Programming Design Patterns 11/17/2018

5 Abstract the commonality (3)
Coffee brewCoffee() addSugar() Tea steepTeaBags() addLemon() Beverage (after further abstraction) brew addCondiments Now design for the class for CaffeineBeverage Programming Design Patterns 11/17/2018

6 Programming Design Patterns
Template Method Defines the steps of an algorithm and allows subclasses to provide the implementation for one or more steps Programming Design Patterns 11/17/2018

7 Programming Design Patterns
CaffeineBeverage Should the class be abstract? Should the method prepare() be final? Should some of the methods of the class be abstract? Which pattern does this remind you of? Homework: difference between this pattern and “factory method” pattern Hint: one is a specialized case of the other This is similar to the factory method pattern Programming Design Patterns 11/17/2018

8 CaffeineBeverage class
public abstract class CaffeineBeverage { public final void prepareRecipe() { boilWater(); brew(); pourCup(); addCondiments(); } protected abstract void brew(); protected abstract void addCondiments(); Programming Design Patterns 11/17/2018

9 Programming Design Patterns
Classwork Write code for a “Tea” class with placeholder implementation (…) for the two methods brew addCondiments Write driver code to call prepareRecipe() on a “Tea” instance Programming Design Patterns 11/17/2018

10 Programming Design Patterns
Tea class public class Tea extends CaffeineBeverage { public void brew() { …} public void addCondiments() { …} } Programming Design Patterns 11/17/2018

11 Template Method Pattern: usage
Convert similar operations to a template. Convert from many specialized operations to a generalized operation. Refactor common behavior to simplify the code. Programming Design Patterns 11/17/2018

12 Template Method Pattern: definition
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. Programming Design Patterns 11/17/2018

13 Template Method Pattern
What if a customer wants tea or coffee without condiments? The pattern provides a hook Programming Design Patterns 11/17/2018

14 Template Method Pattern: hook
public abstract class TemplateClass { public final void templateMethod() { primitiveOperation1(); primitiveOperation2(); if (hook()) { concreteOperation(); } protected abstract void primitiveOperation1(); protected abstract void primitiveOperation2(); final void concreteOperation(); protected boolean hook() { …} Programming Design Patterns 11/17/2018

15 Template Method Pattern: hook (2)
public abstract class Beverage { public final void templateMethod() { primitiveOperation1(); primitiveOperation2(); if (customerWantsCondiments()) addCondiments(); } protected abstract void addCondiments(); // subclasses can override this hook, if they want protected boolean customerWantsCondiments() { return true; Programming Design Patterns 11/17/2018

16 Programming Design Patterns
Hollywood Principle "Hollywood principle" "Don't call us, we'll call you” Clients of Tea/Coffee use the superclass abstraction. reduces dependency between driver/client code and the subclasses Homework How is the “Hollywood Principle” different from “Dependency Inversion Principle” Note that the parent calls the operations of the subclass, which is less common than child code referring to the parent's code Programming Design Patterns 11/17/2018

17 Programming Design Patterns
Design Problem A mortgage loan application consists of: check bank balance, check credit score, check stocks, check client’s potential future earnings An equity loan consists of: check values of assets, check credit score, check stocks, check increments in salary in the next 5 years. If the client is a bank employee then stocks need not be checked Present your design for this problem Programming Design Patterns 11/17/2018

18 Template Method Pattern (1)
You can vary behavior using a simple kind of inheritance The idea is that most of an algorithm or procedure is fixed; the detailed behavior depends on calls to specific operations at certain locations Template methods are a fundamental technique for code reuse They are particularly important in class libraries, although its use is often hidden in the implementation and may not be seen in the API Programming Design Patterns 11/17/2018


Download ppt "Introduction to Behavioral Patterns (3)"

Similar presentations


Ads by Google