Decorator Pattern 1. 10 ¢ Design: Which classes must be included?  Story As a user I want a weekend planner so that I can have more fun  Scenario Given.

Slides:



Advertisements
Similar presentations
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
Chapter 3: The Decorator Pattern
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Unified Modeling Language
Object Oriented Software Development
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.
Computer Science 313 – Advanced Programming Topics.
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
Lecture 16 Composition vs Inheritance: The Final Chapter.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
OO as a language for acm l OO phrase l Mental model of key concepts.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Object Oriented Software Development
More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Advanced OOD and the Strategy Pattern 1. In our last episode… (It was all about Object-Oriented Design) 2.
Object Oriented Programming
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
OOD teaches a complicated method, best for large systems. Here we teach the ten cent version. OO Design for the Rest of Us 1.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CMSC 345 Fall 2000 OO Design. Characteristics of OOD Objects are abstractions of real-world or system entities and manage themselves Objects are independent.
Refactoring1 Improving the structure of existing code.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
More Patterns CS 124. More Basic Patterns Patterns you’ve already seen (without knowing it) Observer / Listener Wrapper Composite Decorator / Filter Patterns.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
Making the Concrete Abstract Adapter, Factory, and More 1.
Builder Introduction. Intent Separate the construction of a complex object from its representation so that the same construction process can create different.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Façade and the DP Wrap-Up 1. Which pattern does this class diagram from the Factory chapter call out for? A.Strategy B.Decorator C.Adapter D.Factory 2.
Observer Pattern Context:
Sections Inheritance and Abstract Classes
Design Patterns: Brief Examples
Inheritance and Polymorphism
Factory Method, Abstract Factory, and More
Decorator Pattern.
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Improving the structure of existing code
Introduction to Design Patterns
Fundaments of Game Design
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Object Oriented Design & Analysis
Presentation transcript:

Decorator Pattern 1

10 ¢ Design: Which classes must be included?  Story As a user I want a weekend planner so that I can have more fun  Scenario Given that I have made a plan When Saturday arrives Then Send me my Saturday reminders When Sunday arrives Then Send me my Sunday reminders 2 A.plan reminder B.plan reminder weekend planner C.plan reminder reminders Saturday Sunday

Discussion  Not B : “weekend planner” is the app itself; don’t need a class for that. Design comes from scenarios, not Story. “Weekend” is not a stable concept (3-day weekends), or it’s possibly two things (Saturday and Sunday), if used to extract plans on the two separate days.  Not C : Saturday and Sunday are instances of type/class Day, not classes themselves  Best answer: A (note the question “which classes must be included ; we also need a Day class) 3

Objects versus Classes  Object represents a “ thing ”  person, car, date, …  It’s “alive”: responds to messages  If two objects generally behave the same, then are same type  Type = (abstract) class or interface  Recall class is a template or blueprint for making objects  The car template is not a car, it’s a “blueprint” for a car  Make as many as I need on the fly  The “Sunday” class is not a Sunday, and also lousy as a blueprint  Make one Sunday, but not Monday  And what about 18 th day of month? 4 In OOD, group your object instances into classes.

Decorator Pattern Can you say “composition” and “delegation”? Again??? 5

Decorator Pattern - Problem  Want to allow adding new behaviors to an object without modifying class  Coffee condiments example is great  Soy, Mocha, Whip, …  Class for each combination?  2 n is too many!  And would be static – cannot change on the fly  Flags in Beverage?  Many combos are nonsense  Have to modify Beverage to add new options  Clothing another example  Take off jacket?  Put on hat?  2 flags or 4 classes? No!  How do w/o mod’ing existing classes? 6 Tea: Options make sense?

Decorator - Solution 7 By composing and delegating, Condiments are “decorating” the cost and description

UML for Coffee Decorator 8 Composes (HAS-A) Extends

UML for Decorator 9 In family of patterns called “Wrapper”

Code a Capitalization Decorator Example: hello earthlings  Hello Earthlings class WordReader extends Reader { public WordReader (File file); public int read (char[] cbuf); // cbuf holds word } You are decorating the WordReader class (or rather, it’s objects) with the capitalization capability. 10 Useful resource: static char Character.toUpperCase(char) Useful resource: static char Character.toUpperCase(char) Nesting (composing) a WordReader inside CapitalizationDecorator

Capitalization Decorator class WordReader extends Reader { public WordReader (File file); public int read (char[] cbuf); // cbuf holds word } class UpperReaderDecorator extends ReaderDecorator { Reader innerReader; public UpperReaderDecorator(Reader iR) { innerReader = iR; } public int read (char[] cbuf) { int length = innerReader.read(cbuf ); cbuf[0] = Character.toUpperCase(cbuf[0]); return length; } } char[100] mybuf; Reader myreader = new WordReader(System.in); myreader = new UpperReaderDecorator(myreader); int length = myreader.read(mybuf); 11 abstract class ReaderDecorator extends Reader { abstract public int read (char[] cbuf); }

Open/Closed Principle Yes, you can have it both ways 12 Brain surgery is not necessary when putting on a hat

Open/Closed Principle (OCP) Classes should be open to extension, but closed to modification  Extension  Composition of abstract types, not concrete classes  Constructors and setters also take those abstract types  Composer delegates to the composed  Inheritance from those abstract classes and interfaces  Modification  Editing – allowed for fixing bugs, but not adding behavior  Danger: propagation of changes to numerous dependents (subclasses, aggregators)  Costly invalidation of test cases (have to rewrite)  “Closure” is really creating openings for extension  There is no “close” operation, but can use version control 13

OCP in Strategy  Also, a constructor that initially sets tires field.  Note that KiaSoul delegates to Tire  Open to infinity of tires without modification 14 > _____ Tire_____ int size() bool perf() String season() _MichelinPilot_ … _DunlopSP_ … … _____KiaSoul_____ Tire tires_________ int tireSize() bool tirePerf() String tireSeason()

OCP for Observer  Also, Display constructor that takes Subject, and Subject register method that takes Observer  Subject delegates to the Observer  Open to adding observers without modification 15 composes abstract type <− inherits abs. type

OCP for Decorator  Open to addition of new drinks and condiments without modification to support all combinations 16 Composition of abstract types Decorator delegates to abstract superclass Inheritance from abstract types

Does this diagram indicate OCP? A.Yes (and why?) B.No (why not?) 17

Discussion  Best answer: A. Because Client composes an abstract interface, it can accept any implementation of Target. Thus, it is possible to add new Adapter/Adaptee pairs to the design without modifying any existing classes.  Not B. Some people noted that Adapter, a concrete class refers to a concrete class. But that is not the part of the design we need “open”. I can always code new Adapter/Adaptee pairs, if necessary. 18

The 80/20 Rule It’s tradeoffs all the way down 19

80/20 Rule – diminishing returns tradeoff  Returns on engineering investment are not linear  80 percent of the benefit for 20% of the effort  Diminishing returns for effort/investment beyond  (last 80% of effort gets only 20% gain)  Also known as Pareto Principle  Is an $80K car 4x better than a $20K car? 20 small effort big gain

80/20 rule : Project Examples  Geofencing sounds cool…  …but a bit of overkill (Porsche vs. Honda)  20% better than brute force, but more effort than it’s worth  When we prioritize our Stories, we are invoking the benefits of the 80/20 rule  OK, we didn’t get the recurring events done  Complicated! Time-consuming!  But we still have location-based reminders!  JUnit is installed, working, and familiar…  …and JBehave/Robotium is not  Yes, it’s 20% better, but cost you time you didn’t have  Remember the 80/20 rule, diminishing returns, cost/benefit, tradeoffs, and risk in general 21

Take-Aways  Be wary of mistaking instances for classes  A class represents a class or category of things, not one thing, which is an instance or object  Composition over subclassing  Composition and delegation supercharge extensibility and reuse  Allows runtime extension  Also called aggregation  Open/Closed Principle (OCP)  Open your classes to extension by following the above principles, then you don’t have to modify them!  80/20 rule – a shorthand for tradeoffs and risk 22

Adapter – easy peasy 23 Real-world examples: Database connectors Chat systems Adapter: Uses Composition and Delegation Adapter (wrapper) is not subclass of wrapped object (because we’re adapting) Naming: InputStreamToReaderAdapter

Adapter - Example 24

Façade – variant of adapter  Not for matching incompatible interfaces, but for simplifying use  Hide unneeded operations  Combine low-level operations into convenient one  Combine several objects into one object  Using a façade in XMPP lab 25

Façade - Example  Aggregates three classes  Replaces low-level operations with high-level  Façade doesn’t have to be interface (but if it does, it will be adapter, too)  Note: if low- level classes declared as interfaces, then becomes Strategy, too 26

Which pattern? Suppose we extended myCity to provide a graph of friend proximity over the whole day. We’d start with a simple display, but think we’ll eventually adopt a powerful library to support a glanceable, informative display. Which pattern should be applied? A.Strategy B.Decorator C.Adapter D.Façade E.None 27

Open/Closed? Which of the following design patterns enable the open-closed principle in your software designs? A.Decorator B.Decorator, Strategy C.Decorator, Adapter D.Decorator, Strategy, Adapter E.Decorator, Strategy, Adapter, Facade 28

In our last Episode… class Operation { public int execute (int a, int b) { return a + b; } class Multiply extends Operation { public int execute (int a, int b) { return fa * fb; } } class Subtract extends Operation {…} class Divide extends Operation {…} 29 ___Operation___ execute(int,int) ____Multiply____ execute(int,int) _____Divide____ execute(int,int) … The “smelly” pre-Strategy design

The Strategy Design Interface OperationStrategy { public int execute (int a, int b); } class MultiplyStrategy implements OperationStrategy { public int execute (int a, int b) { return fa * fb; } } … 30 > _OperationStrategy_ execute(int,int) _MultiplyStrategy_ execute(int,int) _DivideStrategy_ execute(int,int) … class Calculate { private OperationStrategy opStrategy; public Calculate (Operation Strategy os) { opStrategy = os; } public int execute (int a, int b) { return opStrategy.execute(a, b); } } ___________Calculate___________ _ Operation Strategy_opStrategy _ execute(int,int) Composition/aggregation Delegation