Docsity

Design and Java: Patterns and Principles: Template Method, Iterator and | CMPSCI 220, Study notes of Computer Science

Partial preview of the text

Download Design and Java: Patterns and Principles: Template Method, Iterator and | CMPSCI 220 and more Study notes Computer Science in PDF only on Docsity!

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

9 - Design & Java: Patterns and Principles:Template Method, Iterator and Composite

CMPSCI 220 (291A)

Programming Methodology

Spring 2009

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

More Design Patterns^ „^ Motivating examples^ „^ Design patterns^ „^ Design principles

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

Stand Alone Coffee public

class

Coffee { void

prepareRecipe()

boilWater();brewCoffeeGrinds();pourInCup();addSugarAndMilk(); } public

void

boilWater()

{^.

.^.

} public

void

brewCoffeeGrinds()

{^.

} public

void

pourInCup()

{^.

.^.

} public

void

addSugarAndMilk()

{^.

.^.

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

Stand Alone Tea public

class

Tea

void

prepareRecipe()

boilWater();steepTeaBag();pourInCup();addLemon(); } public

void

boilWater()

{^.

.^.

} public

void

steepTeaBag()

{^.

.^.

} public

void

pourInCup()

{^.

.^.

} public

void

addLemon()

{^.

.^.

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

The Template Method Pattern^ „^ Defines the skeleton of an algorithm in amethod, deferring some steps to subclasses^ „

Lets subclasses redefine certain steps „ Without changing the algorithm’s structure

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

The Template Method Pattern

AbstractClass templateMethod()primitiveOperation1()primitiveOperation2()^ ConcreteClass^ primitiveOperation1()primitiveOperation2()

primitiveOperation1();primitiveOperation2();

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

Abstracting Recipe Preparation^ public abstract class CaffeineBeverage {

final void prepareRecipe() {

boilWater();brew();pourInCup();addCondiments();

} abstract void brew();abstract void addCondiments();void boilWater() {.. .} void pourInCup() {.. .}

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

Specializing for Coffee and Tea

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");

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

Coffee Using the Hook public class CoffeeWithHook extends CaffeineBeverageWithHook

{

public void

brew() {... } public void

addCondiments() {... } public boolean customerWantsCondiments() {

String answer =

getUserInput();

if (answer.toLowerCase().startsWith("y")) {

return true; }^ else {

return false; } } private

String getUserInput() {... } }

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

About Template Method Pattern^ „^ Reduces code duplication; maximizes reuse^ „^ Algorithm changes only need be made in oneplace^ „^ Framework that can be used by relatedclasses (e.g., beverages)^ „

Only need to add a few methods

„^ Concentrates knowledge about algorithm^ „

Relies on subclasses to completeimplementations

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

Template Method Pattern in Java^ „^ Sorting arrays^ „

Comparable

and

compareTo()

„^ Swing^ „

paint()

in^ JFrame

is a

hook

„^ Applets provide several hooks^ „

start() „ stop() „ destroy() „ paint()

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

Two Related Design Patterns^ „^ Iterator: Provides a way to access theelements of an aggregate object sequentially^ „

Without exposing its underlying representation

„^ Composite: Allows composing objects intotree structures to represent part-wholehierarchies^ „

Lets clients treat individual objects andcompositions of objects uniformly

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

The Composite Pattern

Component operation()add(Component)remove(Component)getChild(int)

Leaf operation()

Composite add(Component)remove(Component)getChild(int)operation()

Client

U^ NIVERSITY OFUNIVERSITY OF

M^ ASSACHUSETTSMASSACHUSETTS

, A^ MHERST •, AMHERST

-^ Department of Computer ScienceDepartment of Computer Science

Another Design Principle „^ A class should have only one reason to change^ „^

E.g., separating aggregate from iterators allows thetwo to change independently

„^ More generally: Strive for

high cohesion

„^ Module or class designed around a set of^ related

functions has

high

cohesion

„^ Module or class designed around a set of^ unrelated

functions has

low

cohesion

„^ Compare with

low coupling

between modules