lecture 11: implementing decorators

24
LECTURE 11: IMPLEMENTING DECORATORS CSC 313 – Advanced Programming Topics

Upload: cleta

Post on 23-Feb-2016

60 views

Category:

Documents


0 download

DESCRIPTION

CSC 313 – Advanced Programming Topics. Lecture 11: Implementing Decorators. Decorator Pattern Intent. Invisibly augment main concept instances Turn coffee into a double mocha with whip Make $2 sandwich cost $2.17 Decorators add functionality by wrapping - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 11: Implementing Decorators

LECTURE 11:IMPLEMENTING DECORATORS

CSC 313 – Advanced Programming Topics

Page 2: Lecture 11: Implementing Decorators

Decorator Pattern Intent

Invisibly augment main concept instances Turn coffee into a double mocha with

whip Make $2 sandwich cost $2.17

Decorators add functionality by wrapping Original instance wrapped by new

decorator Can also have decorator wrap another

decorator

Page 3: Lecture 11: Implementing Decorators

Decorator Pattern Intent

Invisibly augment main concept instances Turn coffee into a double mocha with

whip Make $2 sandwich cost $2.17

Decorators add functionality by wrapping Original instance wrapped by new

decorator Can also have decorator wrap another

decorator

Violates all rules of good design (Not an actual intent, just good fun)

Page 4: Lecture 11: Implementing Decorators

Pizza

Decorator Pattern Visual

Page 5: Lecture 11: Implementing Decorators

Decorator Pattern Visual

Anchovy

GarlicOlives

Pizza

Page 6: Lecture 11: Implementing Decorators

Decorator Pattern CreationBeverage joe = new HouseBlend();joe = new Mocha(joe);joe = new Mocha(joe);joe = new Whip(joe);joe = new Tall(joe);int mortgage = joe.cost();

Page 7: Lecture 11: Implementing Decorators

Decorators’ Dirty Secrets

Decorators are subclasses of main class

Page 8: Lecture 11: Implementing Decorators

Decorators’ Dirty Secrets

Decorators are subclasses of main class

Page 9: Lecture 11: Implementing Decorators

Decorators’ Dirty Secrets

Decorators are subclasses of main class

Almost recursive

Page 10: Lecture 11: Implementing Decorators

Meet the Decorator Classes AbstractComponent

Component & decoratorsuperclass

Only type used outsidepattern

Interface or abstract class

Defines methods calledby code outside pattern

Page 11: Lecture 11: Implementing Decorators

Meet the Decorator Classes ConcreteComponent(s)

Base concept defined here Only role always created Instance at root

of decoration Holds vital fields Basic methods defined

in this class

Page 12: Lecture 11: Implementing Decorators

Meet the Decorator Classes AbstractDecorator

Decorator superclass Declares component field Abstract class only Re-declares all

inherited methodsto make abstract

Page 13: Lecture 11: Implementing Decorators

Meet the Decorator Classes ConcreteDecorator(s)

Add fields or functionalityto ConcreteComponent

All methods definedso not abstract Calls method

in component field May instantiate 0 to ∞

Page 14: Lecture 11: Implementing Decorators

Decorator Pattern Usage

Drink martini = new Gin();martini = new Vermouth(martini);martini = new Ice(martini);martini = martini.shake();

Page 15: Lecture 11: Implementing Decorators

Decorator Pattern Usage

Drink martini = new Gin();martini = new Vermouth(martini);martini = new Ice(martini);martini = martini.shake();

= martini.pour();

Page 16: Lecture 11: Implementing Decorators

Decorator Pattern Usage

Beverage joe = new HouseBlend();

HBlendjoe

Page 17: Lecture 11: Implementing Decorators

Mocha

Decorator Pattern Usage

Beverage joe = new HouseBlend();joe = new Mocha(joe);

HBlendjoe

bev

Page 18: Lecture 11: Implementing Decorators

Mocha

Decorator Pattern Usage

Beverage joe = new HouseBlend();joe = new Mocha(joe);

HBlendjoe

bev

Page 19: Lecture 11: Implementing Decorators

WhipMocha

Decorator Pattern Usage

Beverage joe = new HouseBlend();joe = new Mocha(joe);joe = new Whip(joe);

HBlendjoe

bevbev

Page 20: Lecture 11: Implementing Decorators

MochaWhip

Mocha

Decorator Pattern Usage

Beverage joe = new HouseBlend();joe = new Mocha(joe);joe = new Whip(joe);joe = new Mocha(joe);

HBlendjoe

bevbevbe

v

Page 21: Lecture 11: Implementing Decorators

MochaWhip

Mocha

Decorator Pattern Usage

Beverage joe = new HouseBlend();joe = new Mocha(joe);joe = new Whip(joe);joe = new Mocha(joe);int mortgage = joe.cost();

HBlendjoe

bevbevbe

v

Page 22: Lecture 11: Implementing Decorators

Decorator Example

Page 23: Lecture 11: Implementing Decorators

Decorators: Good or Bad

Pros: Invisibly add to

classes Enable code reuse Limit code written Creates classes

that are closed to modification

Cons: No reality in

hierarchy Use mangled

recursion Slow, polymorphic

calls used everywhere

Page 24: Lecture 11: Implementing Decorators

For Next Lecture

Lab #3 available on web/Angel Asks you to implement the Observer

Pattern Short -- due before lab next Friday

Think about recursion What are its benefits? What are the costs? Can we do anything to limit these costs?