Csc 313 advanced programming topics
This presentation is the property of its rightful owner.
Sponsored Links
1 / 24

Lecture 12: Implementing Decorators PowerPoint PPT Presentation


  • 107 Views
  • Uploaded on
  • Presentation posted in: General

CSC 313 – Advanced Programming Topics. Lecture 12: 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

Download Presentation

Lecture 12: Implementing Decorators

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Csc 313 advanced programming topics

CSC 313 – Advanced Programming Topics

Lecture 12:Implementing Decorators


Decorator pattern intent

Decorator Pattern Intent

  • Invisiblyaugment 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


Decorator pattern intent1

Decorator Pattern Intent

  • Invisiblyaugment 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)


Decorator pattern visual

Decorator Pattern Visual

Pizza


Decorator pattern visual1

Decorator Pattern Visual

Anchovy

Garlic

Olives

Pizza


Decorator pattern creation

Decorator Pattern Creation

Beverage joe = new HouseBlend();

joe = new Mocha(joe);

joe = new Mocha(joe);

joe = new Whip(joe);

joe = new Tall(joe);

int mortgage = joe.cost();


Decorators dirty secrets

Decorators’ Dirty Secrets

  • Decorators are subclasses of main class


Decorators dirty secrets1

Decorators’ Dirty Secrets

  • Decorators are subclasses of main class


Decorators dirty secrets2

Decorators’ Dirty Secrets

  • Decorators are subclasses of main class

  • Almost recursive


Meet the decorator classes

Meet the Decorator Classes

  • AbstractComponent

    • Component & decoratorsuperclass

    • Only type used outsidepattern

    • Interface or abstract class

    • Defines methods calledby code outside pattern


Meet the decorator classes1

Meet the Decorator Classes

  • ConcreteComponent(s)

    • Base concept defined here

    • Only role always created

    • Instance at rootof decoration

    • Holds vital fields

    • Basic methods definedin this class


Meet the decorator classes2

Meet the Decorator Classes

  • AbstractDecorator

    • Decorator superclass

    • Declares component field

    • Abstract class only

    • Re-declares allinherited methodsto make abstract


Meet the decorator classes3

Meet the Decorator Classes

  • ConcreteDecorator(s)

    • Add fields or functionalityto ConcreteComponent

    • All methods definedso not abstract

      • Calls methodin component field

    • May instantiate 0 to ∞


Decorator pattern usage

Decorator Pattern Usage

Drink martini = new Gin();

martini = new Vermouth(martini);

martini = new Ice(martini);

martini = martini.shake();


Decorator pattern usage1

Decorator Pattern Usage

Drink martini = new Gin();

martini = new Vermouth(martini);

martini = new Ice(martini);

martini = martini.shake();

= martini.pour();


Decorator pattern usage2

Decorator Pattern Usage

Beverage joe = new HouseBlend();

HBlend

joe


Decorator pattern usage3

Decorator Pattern Usage

Beverage joe = new HouseBlend();

joe = new Mocha(joe);

Mocha

bev

HBlend

joe


Decorator pattern usage4

Decorator Pattern Usage

Beverage joe = new HouseBlend();

joe = new Mocha(joe);

Mocha

bev

HBlend

joe


Decorator pattern usage5

Decorator Pattern Usage

Beverage joe = new HouseBlend();

joe = new Mocha(joe);

joe = new Whip(joe);

Whip

Mocha

bev

HBlend

bev

joe


Decorator pattern usage6

Decorator Pattern Usage

Beverage joe = new HouseBlend();

joe = new Mocha(joe);

joe = new Whip(joe);

joe = new Mocha(joe);

Mocha

Whip

Mocha

bev

HBlend

bev

bev

joe


Decorator pattern usage7

Decorator Pattern Usage

Beverage joe = new HouseBlend();

joe = new Mocha(joe);

joe = new Whip(joe);

joe = new Mocha(joe);

int mortgage = joe.cost();

Mocha

Whip

Mocha

bev

HBlend

bev

bev

joe


Decorator example

Decorator Example


Decorators good or bad

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


For next lecture

For Next Lecture

  • Lab #3 available on Angel

    • Asks you to implement Decorator Pattern

    • Have time Friday, butmay want help profiling

  • Two (short) readings available on web

    • Is this method hot or uglier than ____ Mom?

    • What rules of thumb exist for where to optimize?

    • How to express improvement so it is meaningful?

    • Could we compute maximum benefit from opts?


  • Login