F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.

Slides:



Advertisements
Similar presentations
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
Advertisements

Chapter 3: The Decorator Pattern
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
SOLID Object Oriented Design Craig Berntson
CS 210 Introduction to Design Patterns September 12 th, 2006.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Name of.
Marcelo Santos 1 Design Patterns Object Oriented Programming Advanced Course 2007.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns.
1 Software Maintenance and Evolution CSSE 575: Session 6, Part 1 The “SEAM” Model Steve Chenoweth Office Phone: (812) Cell: (937)
Liskov Substitution Principle
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Object-oriented metrics Design decisions: Class Cohesion Open-Closed Single Responsibility Interface Segregation Dependency Inversion Liskov Substitution.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
Software Engineering. Administrivia This is me: Cyndi Rader You can reach me: Or find me here: BB 280D Class notes here:
Lecture 16 Composition vs Inheritance: The Final Chapter.
Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.
CS2110: SW Development Methods Inheritance in OO and in Java Part 1: Introduction Readings: A few pages in Ch. 2 of MSD text introduce this Section 3.3.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
CSE 301 Exam Revision Lecture
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
CSSE 374: More GRASP’ing for Object Responsibilities
Decorator Pattern So many options!. Starbuzz Coffee  Want to offer a variety of combinations of coffee and condiments  Cost of a cup depends on the.
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
CSC 313 – Advanced Programming Topics. Open-Closed Principle Classes should be open for extension, but closed to modification  So, what does this mean?
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
Design Patterns. OO-Concepts Don’t rewrite code Encapsulation Inheritance Write flexible code.
1 Software Engineering: A Practitioner’s Approach, 6/e Chapter 11a: Component-Level Design Software Engineering: A Practitioner’s Approach, 6/e Chapter.
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Five design principles
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
CS 210 Review October 3, 2006.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
SOLID Design Principles
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
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.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Making the Concrete Abstract Adapter, Factory, and More 1.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
The Decorator Pattern Decorators in Java I/O classes SE-2811 Dr. Mark L. Hornick 1.
Builder Introduction. Intent Separate the construction of a complex object from its representation so that the same construction process can create different.
CS 210 Introduction to Design Patterns September 14 th, 2006.
Software Architecture & Difference from Design
object oriented Principles of software design
Factory Method, Abstract Factory, and More
7. Decorator, Façade Patterns
lecture 08, OO Design Principle
OO Design Patterns - Decorator
Decorator Pattern Richard Gesick.
Object-Oriented Design
Week 7, Class 1: The Command Pattern (cont.)
7. Decorator SE2811 Software Component Design
Some principles for object oriented design
Chapter 10 – Component-Level Design
Presentation transcript:

F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator

F-2 Well-known Design Principles Liskov Substitutability Principle Open-Closed Principle (OSP) Dependency Inversion Principle (ISP)

F-3 Substitutability Liskov Substitutability Principle: –“Wherever we see a reference to an abstract object in our code, we can legally replace that with a reference to any subclass object.” –Or another way: subclasses should always be substitutable for their base classes. All about inheritance and “general” references –Implies that we can “use” the subclass object in any way that’s legal for the superclass –Formal definition of IS-A (isn’t it?) Use it to evaluate your use of inheritance

F-4 Dependency Inversion Principle DIP says –High level modules should NOT depend on low- level modules. All modules should depend on abstractions –Abstractions should not depend on details. Details should depend on abstractions. Another way to say it: –Depend on abstractions, not concrete things.

F-5 OO Principle: Open-Closed Principle The Open-Closed Principle (OCP) –Classes should be open for extension, but closed for modification. Don’t allow clients to alter your code. Allow clients to easily add things to your classes. –Provide means of extension Example of this: the Observer design pattern Note there’s a cost to making classes extendable

F-6 Lots in Common Here First, these are formal “principles” for other principles we talked about –Or closely related ones Second, some of these are related –OCP is a goal –DIP is a way of helping you reach that goal

F-7 Another Design Problem, Another Pattern You’re doing Beverages for a coffee shop Four types of coffee-drink: –HouseBlend, DarkRoast, Decaf, Espresso Also can add (for a cost): –SteamedMilk, Soy, Mocha, WhippedMilk Want a cost() method in each class to calculate costs Question: how to structure classes for this? –Avoid class explosion. Same solution as for Customer and Accounts? Need a Bridge?

F-8 One Solution Beverage abstract super-class –Subclasses: HouseBlend, DarkRoast, Decaf,… Fields / Attributes: –milk, soy, mocha, whip Methods / Operations: –hasMilk(), setMilk(); hasSoy(), setSoy(); … –cost() Issues?

F-9 Problems with This Approach Price for condiments? Alter existing code New condiments? Add methods; alter cost() operation in superclass New beverage like ice tea that shouldn’t have whipped milk? Want a double mocha?

F-10 Decorator Design Pattern “Decorate” an object –Wrappers: a object defined so that it encloses another object Key points: –Decorators have the same supertype as the object they wrap. So you can use a decorated object in the same place as the original object (a la Liskov) –Can use more than one decorator on an object –Can decorate objects at run-time

F-11 Decorators in Java I/O Used for input and output file streams Many stream types are wrappers –Add extra functionality, e.g. push-back, line-numbering, buffering, etc. –Create by using “more basic” file-stream object in constructor –Can used a wrapped-stream where you’d use any stream See Java API: utStream.html utStream.html Also used in Java Swing

F-12 Decorators in Java I/O FileInputStream istream = new FileInputStream(“foo.txt”); BufferedInputStream bstream = new BufferedInputStream(istream);

F-13 Issues with Decorators Disadvantages: –May add many classes, makes package hard to understand Like Java I/O streams –Client code should not rely on knowing a reference’s specific concrete class May get wrapped. Wrapping intended to transparent to client code. –Creating new objects could be more complex A factory class may help

F-14 Swing Examples of Decorators Example 1: Yikes -- dead link now -- never mind Button with a diagonal line – –Note: quick read, but not a lot of explanation of Swing and how buttons are drawn or components Example 2 & 3: Component border, minimize – –Better explanation, especially of Decorator pattern Inheritance vs. decoration how components are composed and painted in Swing