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

Lecture 12: Implementing Decorators PowerPoint PPT Presentation


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

Lecture 12:Implementing Decorators


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

Pizza


Decorator Pattern Visual

Anchovy

Garlic

Olives

Pizza


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 are subclasses of main class


Decorators’ Dirty Secrets

  • Decorators are subclasses of main class


Decorators’ Dirty Secrets

  • Decorators are subclasses of main class

  • Almost recursive


Meet the Decorator Classes

  • AbstractComponent

    • Component & decoratorsuperclass

    • Only type used outsidepattern

    • Interface or abstract class

    • Defines methods calledby code outside pattern


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 Classes

  • AbstractDecorator

    • Decorator superclass

    • Declares component field

    • Abstract class only

    • Re-declares allinherited methodsto make abstract


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

Drink martini = new Gin();

martini = new Vermouth(martini);

martini = new Ice(martini);

martini = martini.shake();


Decorator Pattern Usage

Drink martini = new Gin();

martini = new Vermouth(martini);

martini = new Ice(martini);

martini = martini.shake();

= martini.pour();


Decorator Pattern Usage

Beverage joe = new HouseBlend();

HBlend

joe


Decorator Pattern Usage

Beverage joe = new HouseBlend();

joe = new Mocha(joe);

Mocha

bev

HBlend

joe


Decorator Pattern Usage

Beverage joe = new HouseBlend();

joe = new Mocha(joe);

Mocha

bev

HBlend

joe


Decorator Pattern Usage

Beverage joe = new HouseBlend();

joe = new Mocha(joe);

joe = new Whip(joe);

Whip

Mocha

bev

HBlend

bev

joe


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


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

  • 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