lecture 10: decorator pattern

17
LECTURE 10: DECORATOR PATTERN CSC 313 – Advanced Programming Topics

Upload: carlo

Post on 23-Feb-2016

31 views

Category:

Documents


0 download

DESCRIPTION

CSC 313 – Advanced Programming Topics. Lecture 10: Decorator Pattern. Open-Closed Principle. Classes should be open for extension, but closed to modification So, what does this mean?. Reality Check. Classes should be open for extension… Want to update other’s code - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 10: Decorator Pattern

LECTURE 10:DECORATOR PATTERN

CSC 313 – Advanced Programming Topics

Page 2: Lecture 10: Decorator Pattern

Open-Closed Principle

Classes should be open for extension,but closed to modification

So, what does this mean?

Page 3: Lecture 10: Decorator Pattern

Reality Check

Classes should be open for extension… Want to update other’s code

Other is important emphasis Everyone is other to someone

Easily create subclasses to: Add functionality Include additional fields Specialize behavior via overriding

Page 4: Lecture 10: Decorator Pattern

More Reality Check

…But closed to modification

Page 5: Lecture 10: Decorator Pattern

More Reality Check

…But closed to modification Nobody better mess with your code

YOUR code being beauty incarnate usually

Only add errors & will soil your perfection

Never let anyone: Make changes which violate basic

assumptions Alter tasks it performs perfectly already Anything that might introduce bugs

Page 6: Lecture 10: Decorator Pattern

Strategy Pattern & OCP

Pattern stays true to open-closed principle

Using pattern, context open for extension New functionality created by changing

strategy If more data is needed, subclasses can

add fields Context closed to modification

despite all this Actions limited by data provided to

strategies Strategies focused on task & cannot do

other acts

Page 7: Lecture 10: Decorator Pattern

Observer Pattern & OCP

Observer pattern devotee of principle, too

Using this pattern, subject open for extension Responding to events open to each

Observer In pull model, extend Subject to make

more info Cannot modify Subject using this

pattern Limits of pattern means Observers only

RE-ACT Subject chooses events to expose to

Observers

Page 8: Lecture 10: Decorator Pattern

Warning

Remainder of lecture unsafe for:Beginning programmersUnwilling to consider new viewpointsStubbornly dogmaticStupidLiberal arts majors

Page 9: Lecture 10: Decorator Pattern

Inspiration For Pattern

Page 10: Lecture 10: Decorator Pattern

Making a Cultured Pearl

Pearl made when piece of shell enters oyster Irritant required to start making of pearl

To avoid irritation, shell grown around it Layer after layer of material added by

the oyster As it grows, each layer adds new

affect Color, shine, shape modified by every

layer added So each layer adds something, but

always a pearl

Page 11: Lecture 10: Decorator Pattern

Problem At Hand

Have single key main concept Coffee Paycheck Pizza Characters

But many ways to adjust and extend concept Cream, sugar, soy, mocha, caffeine, whip,

chains Federal & State income tax, FICA, health

insurance Bacon, pineapple, ham, anchovies,

mushrooms Bold, italic, underline, color, SMALL CAPS,

error

Page 12: Lecture 10: Decorator Pattern

Add Fields for properties

Add field for each possible property Is double whip, decaf, soy water

possible? Multiple states need taxes; how to

handle this? Using fields is both hard & inefficient

Must modify class for each new property we need

Creates huge number of rarely used fields

Page 13: Lecture 10: Decorator Pattern

Add Fields for properties

Add field for each possible property Is double whip, decaf, soy water

possible? Multiple states need taxes; how to

handle this? Using fields is both hard & inefficient

Must modify class for each new property we need

Creates huge number of rarely used fields

Class open to everyone to play with & change This very clear violation of open close

principle

Page 14: Lecture 10: Decorator Pattern

Create Subclasses

Define subclass for each possible situation Need both MochaWhipCoffee &

WhipMochaCoffee? Every pizza topping combination must be

written Good news: this follows open-close

principle But code duplicated in many places Who’ll modify classes to update New York

tax rate? Maintaining this code will be pain for

someone

Page 15: Lecture 10: Decorator Pattern

Decorator Pattern Intent

Add, but not destroy, existing class instances Double whip, soy, decaf coffee is just

coffee ThEse are still letters ‘t’, ‘h’, ‘e’, ‘s’, & ‘e’ After taxes, my paycheck still (barely)

paycheck Add functionality using these

additions Wrapping around character changes its

display Add $5.00 to price for calling coffee

“tall” This pattern can also add information

Many attachments included with my e-mail

Page 16: Lecture 10: Decorator Pattern

Decorator Pattern Usage

Flexibly add adjectives to main class’s noun These decorations provide additional

description Not required to use each decoration in

each case Wrappings can be stacked in any order

Enhance class’s actions by adding adverbs Use additional information to improve

result Include additional tasks that must be

performed Lets main instance be invisibly

augmented

Page 17: Lecture 10: Decorator Pattern

For Next Lecture

Lab #2 due before next lab Asks you to implement the Strategy

Pattern Read rubric

Read pages 95 – 105 in the book How do we implement the design

pattern? Can anyone actually use this? What is the main point of this pattern?