Chapter 7 Structural Patterns: Decorator - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Chapter 7 Structural Patterns: Decorator

Description:

What Are Structural Patterns? Structural Patterns ... 'Head First Design Patterns' by Bert Bates, Elisabeth Freeman, Eric Freeman, Kathy Sierra ... – PowerPoint PPT presentation

Number of Views:488
Avg rating:3.0/5.0
Slides: 32
Provided by: brahimm
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 Structural Patterns: Decorator


1
Chapter 7 -Structural Patterns Decorator
CIS 476/566 Software Architecture and Design
Patterns
  • Dr. Brahim Medjahed
  • brahim_at_umich.edu

2
Design Patterns in GoF The Big Picture
3
What Are Structural Patterns?
  • Structural Patterns
  • Deal with the composition of classes or objects
  • Two categories
  • Object structural patterns
  • Describe ways to assemble objects (e.g.,
    Decorator, Proxy)
  • Class structural patterns
  • Use inheritance to compose classes (e.g.,
    Adapter)

4
Decorator Pattern - Intent
  • Attach additional responsibilities to an object
    dynamically.
  • Provide a flexible alternative to sub-classing
    for extending functionality.
  • Also Known As (AKA)
  • Wrapper

5
Motivating Example BestCafé
  • BestCafé coffee shop with a large beverage
    offering
  • Example inspired from Head First Design
    Patterns by Bert Bates, Elisabeth Freeman, Eric
    Freeman, Kathy Sierra
  • Offers four (4) beverages
  • House Blend
  • Dark Roast
  • Decaf
  • Espresso

6
BestCafé Initial Design
7
Additional Requirements
  • Customers can also select condiments
  • Milk
  • Soy
  • Mocha
  • Topped with
  • Whipped cream
  • Each one has its own charge

8
Solution 1 Sub-classing
9
Solution 1 - Problems
  • Explosion of classes!
  • Maintenance nightmare
  • What happens when price of milk goes up?
  • Change code in all relevant subclasses
  • What happens when new topping called caramel is
    added?
  • Much more classes

10
Solution 2 Instance Variables
11
Solution 2 Class Diagram
12
Solution 2 - Problems
  • What if the price of condiment changes?
  • Change the code?
  • Adding new condiments
  • Change the super class
  • We may have new beverages for which set of
    condiments are not good.
  • What if customer wants double mocha?

13
Solution 3 Decorate Objects
  • Take a DarkRoast object
  • Decorate it with Mocha object
  • Decorate it with Whip object
  • Call the cost()
  • Rely on delegation to add condiments cost.

14
Solution 3 Decorate Objects (contd)
  • We start with DarkRoast object
  • The customer wants Mocha.
  • Create Mocha and wrap it with DarkRoast

15
Solution 3 Decorate Objects (contd)
  • The customer wants Whipped Cream
  • Create a WhippedCream object and wrap it with
    Mocha

16
Solution 3 Decorate Objects (contd)
Total Cost 1.29
0.10
.99
0.20
17
Decorator Pattern - Structure
Each component can be used on its own or wrapped
by a decorator
Each decorator HAS-A (wraps) a component. It has
an instance variable that refers to that component
Objects we are going to dynamically add behavior
to.
18
Decorator Pattern - Participants
  • Component
  • Defines the interface for objects that can have
    responsibilities added to them dynamically
  • ConcreteComponent
  • Defines an object to which additional
    responsibilities can be attached
  • Decorator
  • Maintains a reference to a component object and
    defines an interface that conforms to Components
    interface
  • ConcreteDecorator
  • Adds responsibilities to the component

19
BestCafé UML Class Diagram
20
Decorator Pattern - Applicability
  • Add responsibilities to objects dynamically and
    transparently
  • Responsibilities that can be withdrawn
  • When extension by sub-classing is impractical
  • Explosion of subclasses
  • Class definition hidden or unavailable

21
Decorator Pattern - Collaborations
  • Decorator forwards requests to its Component
    object.
  • It may optionally perform additional operations
    before and after forwarding the request

22
Implementation - Example
  • Implement UML class diagram in slide 17
  • Operation() in ConcreteComponent displays Im
    concrete component
  • Operation() in ConcreteDecoratorA displays Im
    concrete Decorator A
  • Operation() in ConcreteDecoratorB displays Im
    concrete Decorator B

23
Implementation Example (contd)
  •   // MainApp test application   class
    MainApp      static void Main()          //
    Create ConcreteComponent and two Decorators
          ConcreteComponent c new
    ConcreteComponent()      ConcreteDecoratorA d1
    new ConcreteDecoratorA()      ConcreteDecorato
    rB d2 new ConcreteDecoratorB()      // Link
    decorators       d1.SetComponent(c)      d2.Set
    Component(d1)      d2.Operation()       

24
Implementation Example (contd)
  •   // "Component"   abstract class
    Component      public abstract void
    Operation()    // "ConcreteComponent"
      class ConcreteComponent Component      pu
    blic override void Operation()          Console
    .WriteLine(Im Concrete Component()")      

25
Implementation Example (contd)
  • // "Decorator"   abstract class Decorator
    Component      protected Component
    comp    public void SetComponent(Component
    comp)          this.comp comp        pub
    lic override void Operation()          componen
    t.Operation()      

26
Implementation Example (contd)
  •   // "ConcreteDecoratorA"   class
    ConcreteDecoratorA Decorator      public
    override void Operation()          base.Operati
    on()      Console.WriteLine(Im Concrete
    Decorator A")      
  • // "ConcreteDecoratorB"   class
    ConcreteDecoratorB Decorator      public
    override void Operation()          base.Operati
    on()      Console.WriteLine(Im Concrete
    Decorator B")      

27
Exercise 1
  • Implementation of BestCafé
  • Cost() returns the price of the corresponding
    beverage/condiment
  • Prices hard-coded in the methods
  • Main application
  • Create a Dark Roast decorated with double mocha
    then Whipped Cream

28
Exercise 1 - Implementation
  • To be done in class

29
Exercise 2
  • Window abstract class
  • Has one abstract method draw()
  • SimpleWindow is a subclass of Window
  • draw() displays Simple Window
  • Two possible concrete decorators
  • VerticalScrollBarDecorator
  • draw() displays with vertical scroll bar
  • HorizontalScrollBarDecorator
  • draw() displays with horizontal scroll bar
  • Main Application
  • simple window with horizontal scroll bar with
    vertical scroll bar

30
Exercise 2 UML Class Diagram
  • To be done in class

31
Exercise 2 Implementation
  • To be done in class
Write a Comment
User Comments (0)
About PowerShow.com