Implementation Presentation


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • We’ll assume there’s a Component class called Vehicle
  • We define a subclass of Vehicle called CarDecorator, which we’ll subclass to obtain different decorations. CarDecorator decorates the Vehicle referenced by the _ACarDecorator instance variable, which we initialize in the constructor. For each operation in Vehicle’s interface, CarDecorator defines a default implementation that passes the request on to _ACarDecorator.
  • CarWing is a subclass of CarDecorator. It defines a specific decorations. The CarWing class overrides the Decorate_Vehicle operation to add the wing to the car’s body. The subclass inherits all other operation implementations from Decorator.
  • In the main function we create instances of the decorator class to illustrate how we can use decorators to create a car with with all the body accessories we need. We access the the contents of Car through the Vehicle interface, we don’t have to be aware of the decorator’s presence. We, as the client, can still interact with Car directily when we need to invoke operations that aren’t part of the Vehicle interface. Clients that rely on the component’s identity should refer to it directly as well.
  • Implementation Presentation

    1. 1. Structural Design Patterns: Decorator Implementation Ibrahim Jadalowen SENG 609.04, University of Calgary March 16, 2004
    2. 2. Introduction <ul><li>Decorators expand the functionality of an instance of a class without changing the class code </li></ul><ul><li>More flexibility than static inheritance </li></ul><ul><li>Adding items such as scroll bars to a window as needed provides more flexibility than requiring all windows to have scroll bars </li></ul><ul><li>Decorators work behind the scenes, they are transparent to the interface </li></ul>
    3. 3. Decorator Structure
    4. 4. A Working Example <ul><li>Normal cars can be made to look like sports cars by adding extra accessories to the body </li></ul><ul><li>In this example we decorate the Car component with Wings, Side Skirts, Rear Aprons, and the Manufacturer Stamp. </li></ul>
    5. 5. Vehicle Decorator Structure Vehicle DecorateVehicle(); Car DecorateVehicle(); CarDecorator DecorateVehicle(); CarWing DecorateVehicle(); CarApron DecorateVehicle(); _NextCarDecorator->DecorateVehicle() CarDecorator::DecorateVehicle(); AddApron();
    6. 6. The Vehicle Component <ul><li>class Vehicle {  public:   virtual void Decorate_Vehicle() {}  protected:   Vehicle() { } }; </li></ul>
    7. 7. Concrete Component: The Car <ul><li>The Car is the concrete component to which we will attach features </li></ul><ul><li>class Car : public Vehicle {  public:   Car() : Vehicle() {}   void Decorate_Vehicle() {} }; </li></ul>
    8. 8. The Decorator Class <ul><li>class CarDecorator : public Vehicle </li></ul><ul><li>  {                                                                 </li></ul><ul><li>public: </li></ul><ul><li> CarDecorator(Vehicle* a) : Vehicle() </li></ul><ul><li>{ _ACarDecorator = a; }   void Decorate_Vehicle() </li></ul><ul><li>{_ACarDecorator->Decorate_Vehicle(); } </li></ul><ul><li>private: Vehicle* _ACarDecorator; </li></ul><ul><li>}; </li></ul>
    9. 9. Define the Car Wing <ul><li>class CarWing : public CarDecorator { </li></ul><ul><li>public:   CarWing(Vehicle* a) : CarDecorator(a) { }   void Decorate_Vehicle() {    cout << &quot; a car wing.&quot; << endl;         </li></ul><ul><li>   CarDecorator::Decorate_Vehicle(); } </li></ul><ul><li>//     and add extra stuff. }; </li></ul>
    10. 10. Define the Rear Apron <ul><li>class CarApron : public CarDecorator {  public:   CarApron(Vehicle* a) : CarDecorator(a) { }   void Decorate_Vehicle() {    cout << &quot; a car apron.&quot; << endl;    CarDecorator::Decorate_Vehicle();} }; </li></ul>
    11. 11. Define the Side Skirt <ul><li>class CarSideSkirt : public CarDecorator {  public:  CarSideSkirt(Vehicle* a) : CarDecorator(a) {}  void Decorate_Vehicle() {   cout << &quot; a car side skirt.&quot; << endl;   CarDecorator::Decorate_Vehicle(); } }; </li></ul>
    12. 12. Manufacturer Stamp <ul><li>class CarStamp : public CarDecorator {  public:    CarStamp(Vehicle* a, int cntry_num = 1) : CarDecorator(a)        { _cntry_type = cntry_num; }    void Decorate_Vehicle() {        Decorate();          CarDecorator::Decorate_Vehicle(); }   protected:     void Decorate() {cout << &quot; made in&quot;; Type(); }     void Type() {        if (_cntry_type > 1) cout << &quot; Japan!...&quot;<< endl;        if (_cntry_type < 1) cout << &quot; USA!...&quot; << endl;        if (_cntry_type == 1) cout << &quot; Germany!...&quot; << endl; }  private: </li></ul><ul><li>  int _cntry_type; }; </li></ul>
    13. 13. Display Vehicle type <ul><li>void TellMeVehicleType(Vehicle* a, char* n) {  cout <<n << &quot; has &quot; << endl;  a->Decorate_Vehicle();  cout << endl; } </li></ul>
    14. 14. Client: Main function <ul><li>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, &quot;Honda&quot;); </li></ul><ul><li>} </li></ul>
    15. 15. Output <ul><li>Honda has </li></ul><ul><li>a car wing </li></ul><ul><li>a car apron </li></ul><ul><li>a car side skirt </li></ul><ul><li>made in Japan </li></ul>
    16. 16. Conclusion <ul><li>The main function, or the client, doesn’t have to be aware of the decorator presence. It is transparent. </li></ul><ul><li>We add feature dynamically to the car objects as we wish. </li></ul><ul><li>Decorator instances are not defined until we use them. </li></ul>
    17. 17. References <ul><li>[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] </li></ul><ul><li>  Rai, Gurjit, Decorator Pattern, SENG609.04 Fall 2000 Term Paper. University of Calgary. </li></ul>