/
The Decorator Pattern The Decorator Pattern attaches a The Decorator Pattern The Decorator Pattern attaches a

The Decorator Pattern The Decorator Pattern attaches a - PDF document

faustina-dinatale
faustina-dinatale . @faustina-dinatale
Follow
462 views
Uploaded On 2015-05-25

The Decorator Pattern The Decorator Pattern attaches a - PPT Presentation

Decorators provide a flexible alternativ e to subclassing for extending functionality Toni Sellars Universitat de Girona Welcome to Starbuzz Coffee Because Starbuzz Coffee have grown so quickly they are scrambling to update their ordering system to ID: 74466

Decorators provide flexible

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "The Decorator Pattern The Decorator Patt..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

ties to an object dynamically. e to subclassingfor extending functionality.ToniSellarèsUniversitatde Girona Welcome to StarbuzzCoffee!Because StarbuzzCoffee have grown so quickly, they arescrambling to update their ordering system to match their beverage BeveragedescriptiongetDescription( )cost ( )// other useful methods HouseBlendcost () DarkRoastcost () Decafcost () Expressocost () Beverage is an abstract class, subclassedby all beverages offered in the coffee shop. The description instance variable is set in each subclass and holds a description of the beverage like: abstract; subclasses need to implementations. Each subclass implements cost ( ) to return the cost of the beverage 1)Price changes for condiments will force us to alter the existingcode.2) New condiments will force us to add new methods and alter the cost method in the superclass.3)We may have new beverages. For some of these beverages the condiments may not be appropriate.er wants a double mocha? ”it with the1.Take a 2.Decorate it with a 3.Decorate it with a 4.Call the ( ) method and rely on delegation to add on ”and how does delegation come into this? •Decorators have the same •You can use one or more decorators to wrap an object.•Given that the decorator has the same •The decorator adds its own behavior either before and/or after •Objects can be decorated at any time, so we can decorate objects at runtime with as ma Key point! Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassingfor extending functionality. ComponentmethodA( )methodB( ) ConcreteComponentmethodA( )methodB( )// other methods Decorator methodA( )methodB( ) ConcreteDecoratorAComponent wrappedObjectmethodA( )methodB( )newBehavior( ) ConcreteDecoratorBComponent wrappedObjectmethodA( )methodB( ) Each component can be used on its own or Each decorator HAS_A (wraps) a component, which means the decoracomponent. Decorators implement the same interface or abstract class as the component they are going to decorate. component The ConcreteDecoratorhas an instance variable for the thing it Decorator wraps) Decorators can add new methods; however, new behavior is typically added by doing some computation before or after an existing method in the component The ConcreteComponentis the object we are going to Component. publicclasspublicvoidaddedVariable= "extra"; publicclasspublicvoidaddedBehavior(); privatevoidorB.addedBehavior() publicclasspublicvoid publicinterfacepublicvoid public String getDescription( ) {tDecoratorextends Beverage { getDescription( ); Beverage is an abstract class with two methods getDescription( ) and cost ( ).getDescription( )is already implemented, but we need to implement cost interchangeable with Beverage, so we extend the Beverage class. We are also going to require that the condiment decorators reimplementthe getDescription( ) method. We will see why in a sec… oextends Beverage {public Expresso( ) {ndextends Beverage {public HouseBlend( ) {