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

decorator
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 2

Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket

Overview

1

Identification

2

Structure

3

Discussion

4

Participants

5

Related Patterns

6

Example - creating a Pizza The scenario Hawaiian pizza

Linda Marshall and Vreda Pieterse Decorator

slide-3
SLIDE 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

slide-4
SLIDE 4

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

slide-5
SLIDE 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

  • f 0 or 1.

Composite themselves do not have specilisations, while Decorators do.

Linda Marshall and Vreda Pieterse Decorator

slide-6
SLIDE 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

slide-7
SLIDE 7

Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket

Decorator defines a reference to a Component-type

  • bject

ConcreteDecorator adds the responsibilities to the component

Linda Marshall and Vreda Pieterse Decorator

slide-8
SLIDE 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

slide-9
SLIDE 9

Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket The scenario Hawaiian pizza First attempt Enter the Decorator....

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket The scenario Hawaiian pizza First attempt Enter the Decorator....

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

slide-12
SLIDE 12

Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket The scenario Hawaiian pizza First attempt Enter the Decorator....

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

slide-13
SLIDE 13

Identification Structure Discussion Participants Related Patterns Example - creating a Pizza Example - Sales Ticket The scenario Hawaiian pizza First attempt Enter the Decorator....

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

slide-14
SLIDE 14

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