Decorator pattern - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Decorator pattern

Description:

What can you criticize about this inheritance architecture? ... If a customer wants a Dark Roast with Mocha and Whip. Take a DarkRoast object ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 38
Provided by: csieNt2
Category:

less

Transcript and Presenter's Notes

Title: Decorator pattern


1
Decorator pattern
2
An example
3
Your first idea of implementation
4
In reality
5
Now a beverage can be mixed from different
condiment to form a new beverage
6
(No Transcript)
7
(No Transcript)
8
Now, your turns. It is a good solution?
9
Try several minutes to complete
10
What can you criticize about this inheritance
architecture?
  • Write down your notes to see if you are right

11
(No Transcript)
12
(No Transcript)
13
(No Transcript)
14
(No Transcript)
15
Decorator Pattern ???
  • The problems of two previous designs
  • we get class explosions, rigid designs,
  • or we add functionality to the base class that
    isnt appropriate for some of the subclasses.

16
Revisit the problem again
  • If a customer wants a Dark Roast with Mocha and
    Whip
  • Take a DarkRoast object
  • Decorate it with a Mocha object
  • Decorate it with a Whip object
  • Call the cost() method and rely on
  • delegation to add on the condiment costs

17
Constructing a drink order with Decorators
18
(No Transcript)
19
(No Transcript)
20
(No Transcript)
21
Decorator Pattern defined
22
The decorator pattern for Starbuzz beverages
23
(No Transcript)
24
(No Transcript)
25
Lets see the code
26
The abstract class of condiments
27
Concrete Base Classes of Beverages
28
A concrete Condiment class
When Mocha price changed, we only need to change
this class
29
Constructing new beverages from decorator classes
dynamically
30
Real world decorator Java I/O
  • Java I/O

31
(No Transcript)
32
Comments
  • You can see that this isnt so different from the
    Starbuzz design. You should now be in a good
    position to look over the java.io API docs and
    compose decorator s on the various input streams.
  • Youll see that the output streams have the same
    design. And youve probably already found that
    the reader/Writer streams (for character-based
    data) closely mirror the design of the streams
    classes (with a few differences and
    inconsistencies, but close enough to figure out
    whats going on).

33
Lets write a new decorator
34
Test out your new Java I/O decorator
35
Dark Side
  • You can usually insert decorators transparently
    and the client never has to know its dealing
    with a decorator
  • However, if you write some code is dependent on
    specific types -gt Bad things happen
  • Java library is notorious to be used badly by
    people who do not know decorator pattern

Beverage beverage2 new DarkRoast() beverage2
new Mocha(beverage2) beverage2 new
Mocha(beverage2) beverage2 new
Whip(beverage2) System.out.println(beverage2.getD
escription() beverage2.cost())
Beverage beverage2 new DarkRoast() beverage2
new Mocha(beverage2) beverage2 new
Mocha(beverage2) Whip beverage3 new
Whip(beverage2) System.out.println(beverage3.getD
escription() beverage2.cost())
The right way
The poor way
36
(No Transcript)
37
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com