9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
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
CS 210 Introduction to Design Patterns September 12 th, 2006.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Plab – Tirgul 12 Design Patterns
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Dept. of Computer Engineering, Amirkabir University of Tech. 1 Design Patterns Dr. Noorhosseini Introduction.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
ASP.NET Programming with C# and SQL Server First Edition
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
CS 4240: Introduction to Design Patterns Readings: Chap. 5 of Design Patterns Explained Also, Chap. 6 and 7 Also, on-line info on Composite pattern (and.
Design Patterns Discussion of pages: xi-11 Sections: Preface, Forward, Chapter
Design Patterns.
Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
Creational Patterns (1) CS350, SE310, Fall, 2010.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
Objects First With Java A Practical Introduction Using BlueJ Designing applications 1.0.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
CSE 301 Exam Revision Lecture
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.
GoF Sections Design Problems and Design Patterns.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Basic OOP Concepts and Terms. In this class, we will cover: Objects and examples of different object types Classes and how they relate to objects Object.
ECE450S – Software Engineering II
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Designing applications Main concepts to be covered Discovering classes CRC cards Designing interfaces Patterns Objects First with Java - A Practical.
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
1/15/03A-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Course Introduction Dr. Tom Horton Phone: Office.
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
1/20/05A-1 © 2001 T. Horton CS 494 Adv. SW Design and Development A Tasting…. Course 1: Design patterns: Intro, example Course 2: Inheritance, Interfaces,
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Designing applications Main concepts to be covered Discovering classes CRC cards Designing interfaces Patterns © 2017 Pearson Education, Inc. Hoboken,
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
CS 210 Introduction to Design Patterns September 14 th, 2006.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Factory Patterns 1.
Software Design and Architecture
OO Design Patterns - Decorator
Decorator Pattern Richard Gesick.
Presentation transcript:

9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns

9/28/01F-2 Readings Chapter 1 of GoF book –Especially pp. 1-10, –I’ll get this to you (toolkit, reserve, Web?) Eckel’s Thinking in Patterns, on Web –Chap. 1, “The pattern concept” –Chap. 5, “Factories” Handouts on various patterns

9/28/01F-3 Idioms, Patterns, Frameworks Idiom: a small language-specific pattern or technique –A more primitive building block Design pattern: a description of a problem that reoccurs and an outline of an approach to solving that problem –Generally domain, language independent –Also, analysis patterns Framework: –A partially completed design that can be extended to solve a problem in a domain Horizontal vs. vertical

9/28/01F-4 Examples of C++ Idioms Use of an Init() function in constructors –If there are many constructors, make each one call a private function Init() Init() guarantees all possible attributes are initialized Initialization code in one place despite multiple constructors Don’t do real work in a constructor –Define an Open() member function Constructors just do initialization Open() called immediately after construction –Constructors can’t return errors They can throw exceptions

9/28/01F-5 Design Patterns: Essential Elements Pattern name –A vocabulary of patterns is beneficial Problem –When to apply the pattern, what context. –How to represent, organize components –Conditions to be met before using Solution –Design elements: relationships, responsibilities, collaborations –A template for a solution that you implement Consequences –Results and trade-offs that result from using the pattern –Needed to evaluate design alternatives

9/28/01F-6 Patterns Are (and Aren’t) Name and description of a proven solution to a problem Documentation of a design decision They’re not: –Reusable code, class libraries, etc. (At a higher level) –Do not require complex implementations –Always the best solution to a given situation –Simply “a good thing to do”

9/28/01F-7 Describing Design Patterns The GoF defined a standard format –Generally followed –Not just a UML diagram! Pattern Format (13 sections): –Pattern name and classification –Intent: what’s it do? Rationale? –Also known as –Motivation A scenario that illustrates a sample problem and how this patterns helps solve it. –Applicability For which situations can this be applied? –Structure Graphical representation (e.g. UML)

9/28/01F-8 Pattern Format (cont’d) –Participants Classes and objects, their responsibilities –Collaborations How participants interact –Consequences –Implementation Pitfalls, hints, techniques, language issues –Sample code Code fragments that illustrate the pattern –Known uses From real systems –Related patterns Similar patterns, collaborating patterns

9/28/01F-9 Example 1: Singleton Pattern Context: Only one instance of a class is created. Everything in the system that needs this class interacts with that one object. Controlling access: Make this instance accessible to all clients Solution: –The class has a static variable called theInstance (etc) –The constructor is made private (or protected) –Clients call a public operation getInstance() that returns the one instance This may construct the instance the very first time or be given an initializer

9/28/01F-10 Singleton: Java implementation public class MySingleton { private static MySingleton theInstance = new MySingleton(); private MySingleton() { // constructor … } public static MySingleton getInstance() { return theInstance; } }

9/28/01F-11 Static Factory Methods Singleton patterns uses a static factory method –Factory: something that creates an instance Advantages over a public constructor –They have names. Example: BigInteger(int, int, random) vs. BigInteger.probablePrime() –Might need more than one constructor with same/similar signatures –Can return objects of a subtype (if needed) Wrapper class example: Double d1 = Double.valueOf(“3.14”); Double d2 = new Double (“3.14”); More info: Bloch’s Effective Java

9/28/01F-12 The State Design Pattern A connection can be in various states –Handles requests differently depending on state Connection delegates requests to its state object –Which changes dynamically

9/28/01F-13

9/28/01F-14 Categories of Design Patterns GoF categories –Creational –Structural –Behavioral Can you classify patterns we’ve seen so far?

9/28/01F-15 Factories for Creating Objects Recall ideas about reducing coupling between a client and objects in an inheritance hierarchy Common approaches for achieving this –Factory pattern (AKA Factory Method pattern) A client calls a method to get a reference to an object, but only knows its interface or supertype –Abstract factory: A client gets an instance of a factory class factory object creates one particular kind of many related objects AKA kit or toolkit pattern

9/28/01F-16 Factory Method Pattern A factory method –Is called to create an object for the client –Returns a type that is interface or abstract class –May be implemented in several places or in one Factory class –Seeks to improve coupling, reduce dependancies Examples in Java (or are they? you decide) –toString() –Arrays.asList(obj[] ary) // returns List –iterator()

9/28/01F-17 Desired Framework: (From Grand’s book.) But how can Application create instances of Document objects without subclass coupling? It can’t according to this diagram. But see next slide!

9/28/01F-18 Solution: Use Document Factory class to create various types of documents (From Grand’s book.)

9/28/01F-19 Factory Method Pattern: General Structure discriminator is some parameter that indicates what kind of Product subclass to instantiate (perhaps a string). (From Grand’s book.)

9/28/01F-20 Another Example: Java Iterator What kind of type is Iterator? –Answer: interface How do we get an Iterator object? –Answer: from a factory method defined in the collection class Consider: List l1 = Arrays.asList(ary); Iterator i1 = l1.iterator();

9/28/01F-21 Abstract Factory Pattern Abstract Factory is a class –That contains a set of related factory methods for creating various types of objects (e.g. widgets) –Abstract class: all these are abstract methods –Developer writes subclass of the Abstract Factory Set of methods create related types of objects –Client instantiates one of several subclasses to get a particular kind of factory object Calls methods on that object to get instances of different widgets Factory creates only widgets of one “family”

9/28/01F-22 General Overview of Abstract Factory Note use of static method in factory class to return particular concrete factory object. This could be a factory method that takes a string as a discriminator. (From Grand’s book.)

9/28/01F-23 Example of Abstract Factory (From Grand’s book.)

9/28/01F-24 Benefit of Abstract Factories: substituting one factory for another. Application can be modified by swapping out the type of factory it uses. (From Martin’s ASD book.)

9/28/01F-25 The problem (again): If SomeApp only uses the Shape interface, it shouldn’t use new to create instances of concrete classes. •(From Martin’s ASD book.)

9/28/01F-26 Abstract Factory is a better Solution: SomeApp uses a particular ShapeFactory implementation (no use of new) Note here that “abstract” factory is an interface. That seems fine, but it’s not clear from this diagram how SomeApp creates instance of a concrete factory. (From Martin’s ASD book.)

9/28/01F-27 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

9/28/01F-28 Another Design Solution for This 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?

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

9/28/01F-30 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?

9/28/01F-31 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

9/28/01F-32 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

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

9/28/01F-34 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 pattern class

9/28/01F-35 Swing Examples of Decorators Example 1: 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