Structural Design Patterns: Decorator Implementation - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Structural Design Patterns: Decorator Implementation

Description:

In this example we decorate the Car component with Wings, Side Skirts, Rear ... We add feature dynamically to the car objects as we wish. ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 18
Provided by: pagesCpsc4
Category:

less

Transcript and Presenter's Notes

Title: Structural Design Patterns: Decorator Implementation


1
Structural Design Patterns Decorator
Implementation
  • Ibrahim Jadalowen
  • SENG 609.04, University of Calgary
  • March 16, 2004

2
Introduction
  • Decorators expand the functionality of an
    instance of a class without changing the class
    code
  • More flexibility than static inheritance
  • Adding items such as scroll bars to a window as
    needed provides more flexibility than requiring
    all windows to have scroll bars
  • Decorators work behind the scenes, they are
    transparent to the interface

3
Decorator Structure
4
A Working Example
  • Normal cars can be made to look like sports cars
    by adding extra accessories to the body
  • In this example we decorate the Car component
    with Wings, Side Skirts, Rear Aprons, and the
    Manufacturer Stamp.

5
VehicleDecorator Structure
Vehicle DecorateVehicle()
Car DecorateVehicle()
CarDecorator DecorateVehicle()
_NextCarDecorator-gtDecorateVehicle()
CarWing DecorateVehicle()
CarApron DecorateVehicle()
CarDecoratorDecorateVehicle() AddApron()
6
The Vehicle Component
  • class Vehicle  public   virtual void
    Decorate_Vehicle()  protected   Vehicle()

7
Concrete Component The Car
  • The Car is the concrete component to which we
    will attach features
  • class Car public Vehicle  public   Car()
    Vehicle()   void Decorate_Vehicle()

8
The Decorator Class
  • class CarDecorator public Vehicle
  •                                                  
                    
  • public
  • CarDecorator(Vehicle a) Vehicle()
  • _ACarDecorator a   void
    Decorate_Vehicle()
  • _ACarDecorator-gtDecorate_Vehicle()
  • private Vehicle _ACarDecorator

9
Define the Car Wing
  • class CarWing public CarDecorator
  • public   CarWing(Vehicle a) CarDecorator(a)
      void Decorate_Vehicle()    cout ltlt "\t
    a car wing." ltlt endl        
  •    CarDecoratorDecorate_Vehicle()
  • //     and add extra stuff.

10
Define the Rear Apron
  • class CarApron public CarDecorator  public
      CarApron(Vehicle a) CarDecorator(a)  
    void Decorate_Vehicle()    cout ltlt "\t a car
    apron." ltlt endl    CarDecoratorDecorate_Vehicl
    e()

11
Define the Side Skirt
  • class CarSideSkirt public CarDecorator
     public  CarSideSkirt(Vehicle a)
    CarDecorator(a)  void Decorate_Vehicle()  
    cout ltlt "\t a car side skirt." ltlt endl  
    CarDecoratorDecorate_Vehicle()

12
Manufacturer Stamp
  • class CarStamp public CarDecorator  public
       CarStamp(Vehicle a, int cntry_num 1)
    CarDecorator(a)        _cntry_type
    cntry_num    void Decorate_Vehicle()
           Decorate()          CarDecoratorDecor
    ate_Vehicle()   protected     void
    Decorate() cout ltlt "\t made in" Type()    
    void Type()        if (_cntry_type gt 1)
    cout ltlt " Japan!..."ltlt endl        if
    (_cntry_type lt 1) cout ltlt " USA!..." ltlt endl
           if (_cntry_type 1) cout ltlt "
    Germany!..." ltlt endl  private
  •   int _cntry_type

13
Display Vehicle type
  • void TellMeVehicleType(Vehicle a, char n)
     cout ltltn ltlt " has " ltlt endl  a-gtDecorate_Vehic
    le()  cout ltlt endl

14
Client Main function
  • void main(void) //Client has the
    responsibility to compose desired features
     Vehicle Honda   new CarWing(        new
    CarApron(         new CarSideSkirt(         
    new CarStamp(           new
    Car(),2))))  TellMeVehicleType(Honda, "Honda")

15
Output
  • Honda has
  • a car wing
  • a car apron
  • a car side skirt
  • made in Japan

16
Conclusion
  • The main function, or the client, doesnt have to
    be aware of the decorator presence. It is
    transparent.
  • We add feature dynamically to the car objects as
    we wish.
  • Decorator instances are not defined until we use
    them.

17
References
  • Gamma 1995   Gamma, E., Helm, R., Johnson, R.
    Vlissides, J. (1995). Design Patterns Elements
    of Reusable Object-Oriented Software. Reading
    Mass., Addison Wesley. Rai 2000
  •  Rai, Gurjit, Decorator Pattern, SENG609.04 Fall
    2000 Term Paper. University of Calgary.
    http//sern.ucalgary.ca/grai/seng/609.04/classli
    brarytemplate1.html
Write a Comment
User Comments (0)
About PowerShow.com