decorator
play

Decorator Linda Marshall and Vreda Pieterse Department of Computer - PowerPoint PPT Presentation

Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket Decorator Linda Marshall and Vreda Pieterse Department of Computer Science University of Pretoria 29 August 2014 Linda


  1. Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket Decorator Linda Marshall and Vreda Pieterse Department of Computer Science University of Pretoria 29 August 2014 Linda Marshall and Vreda Pieterse Decorator

  2. Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket Overview Identification 1 Structure 2 Discussion 3 Participants 4 Related Patterns 5 Example - creating a Pizza 6 The scenario Hawaiian pizza Linda Marshall and Vreda Pieterse Decorator

  3. Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket Name and Classification: Decorator (Object Structural) Intent: “Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.” GoF(175) Linda Marshall and Vreda Pieterse Decorator

  4. Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket Linda Marshall and Vreda Pieterse Decorator

  5. Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket Looks similar to the Composite. A composite may comprise of multiple components, while Decorators comprise of 0 or 1. Composite themselves do not have specilisations, while Decorators do. Linda Marshall and Vreda Pieterse Decorator

  6. Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket Component interface for objects that can have responsibilities dynamically added to them. ConcreteComponent the object to which the additional responsibilities can be attached Linda Marshall and Vreda Pieterse Decorator

  7. Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket Decorator defines a reference to a Component-type object ConcreteDecorator adds the responsibilities to the component Linda Marshall and Vreda Pieterse Decorator

  8. Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket Adapter (139) : Changes the interface to an object while the Decorator only changes responsibilities. Composite (163) : A Decorator can be seen as a Composite with only one component that has added responsibility. Strategy (315) : The Strategy pattern changes the inner workings of an object while the Decorator changes the looks. Linda Marshall and Vreda Pieterse Decorator

  9. Identification Structure The scenario Discussion Hawaiian pizza Participants First attempt Related Patterns Enter the Decorator.... Example - creating a Pizza Example - Sales Ticket Luigi (the restauranteur from the Ashes to Ashes series) has decided to begin a fast pizza outlet called Dumbo’s - he was a Disney fan. Luigi wants his clientele to choose the toppings for the pizza. Each topping has a price associated with it. A Hawaiian pizza for example has a tomato base, mozzarella cheese, ham and pineapple. Linda Marshall and Vreda Pieterse Decorator

  10. Identification Structure The scenario Discussion Hawaiian pizza Participants First attempt Related Patterns Enter the Decorator.... Example - creating a Pizza Example - Sales Ticket Linda Marshall and Vreda Pieterse Decorator

  11. Identification Structure The scenario Discussion Hawaiian pizza Participants First attempt Related Patterns Enter the Decorator.... Example - creating a Pizza Example - Sales Ticket class Pizza { // methods to get a description // and the cost }; class Hawaiian : public Pizza { // description and cost methods // specifically to Hawaiian pizzas }; Linda Marshall and Vreda Pieterse Decorator

  12. Identification Structure The scenario Discussion Hawaiian pizza Participants First attempt Related Patterns Enter the Decorator.... Example - creating a Pizza Example - Sales Ticket What if - the cost of ham changes? Luigi wants to add salami to the pizza? Or maybe even cheddar cheese! Linda Marshall and Vreda Pieterse Decorator

  13. Identification Structure The scenario Discussion Hawaiian pizza Participants First attempt Related Patterns Enter the Decorator.... Example - creating a Pizza Example - Sales Ticket The previous design will require another class to be defined to inherit from Hawaiian which increases the price of the pizza to include salami and updates the description accordingly. You can see where this is going......... How would you use the Decorator pattern to solve your problem? Linda Marshall and Vreda Pieterse Decorator

  14. Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket Linda Marshall and Vreda Pieterse Decorator

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend