Design Patterns. Definition of a Design Pattern

Similar documents
Observer Pattern. CS580 Advanced Software Engineering October 31, Yu Sun, Ph.D.

Lecture 13: Design Patterns

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

CISC 322 Software Architecture

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

2.1 Design Patterns and Architecture (continued)

Introduction and History

2.1 Design Patterns and Architecture (continued)

Topics in Object-Oriented Design Patterns

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

Design Patterns. An introduction

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

Object-Oriented Oriented Programming

Design Patterns. CSC207 Fall 2017

COSC 3351 Software Design. Design Patterns Structural Patterns (I)

Design Patterns. CSC207 Fall 2017

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Design Patterns. CSC207 Winter 2017

SOFTWARE PATTERNS. Joseph Bonello

6.3 Patterns. Definition: Design Patterns

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

An Introduction to Patterns


Design Patterns. GoF design patterns catalog

Outline. Design Patterns. Observer Pattern. Definitions & Classifications

Design Patterns So Far. Design Patterns, cont. Design Patterns So Far. Outline

Software Engineering I (02161)

Design Patterns IV Structural Design Patterns, 1

Review: Activities and products of System design. Chapter 8: Object design: Reusing Pattern Solutions. Object Design: closing the gap

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Software Eningeering. Lecture 9 Design Patterns 2

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Design Patterns Lecture 2

Using Design Patterns in Java Application Development

An Introduction to Patterns

EECS 4314 Advanced Software Engineering. Topic 04: Design Pattern Review Zhen Ming (Jack) Jiang

Design Patterns. Gunnar Gotshalks A4-1

Object-Oriented Design

CPSC 310 Software Engineering. Lecture 11. Design Patterns

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

DESIGN PATTERN - INTERVIEW QUESTIONS

The GoF Design Patterns Reference

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Adapter Pattern Structural

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Crash course on design patterns

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Object Design II: Design Patterns

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

A reusable design concept

Reuse at Design Level: Design Patterns

Object-Oriented Oriented Programming Adapter Pattern. CSIE Department, NTUT Woei-Kae Chen

SDC Design patterns GoF

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Programmazione. Prof. Marco Bertini

Details of Class Definition

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Refining the Observer Pattern: The Middle Observer Pattern

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

3/6/17. Object design: Reusing Pattern Solutions" (Chapter 8 in Bruegge) Set implemented by extending Hashtable

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Tuesday, October 4. Announcements

The Composite Design Pattern

Software Engineering I (02161)

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Object-Oriented Oriented Programming Adapter Pattern

Design Patterns 2. Page 1. Software Requirements and Design CITS 4401 Lecture 10. Proxy Pattern: Motivation. Proxy Pattern.

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

Design Patterns Reid Holmes

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design Patterns: Part 2

L23.1 Introduction... 2

Patterns of learning

Applying the Observer Design Pattern

Advanced Topics in Software Engineering (02265) Ekkart Kindler

Object-Oriented Design

Design Patterns (DP) In the beginning. It s not a course about DP (just a little) A lot of good design and efficient implementation is based on DP

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

CHAPTER 6: CREATIONAL DESIGN PATTERNS

As a programmer, you know how easy it can be to get lost in the details

are most specifically concerned with

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

The Object Recursion Pattern

3 Product Management Anti-Patterns by Thomas Schranz

administrivia today UML start design patterns Tuesday, September 28, 2010

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

Design patterns. OOD Lecture 6

Transcription:

Design Patterns Barbara Russo Definition of a Design Pattern A Pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice (Alexander, Ishikawa, Silverstein, Jacobson, Fiksdhal-King, Angel A Pattern Language, 1977) 2

Looking for Patterns 3 Same Pattern in a Similar Tower 4

Same Pattern in a Slightly Different Tower 5 Same Pattern in a Completely Different Tower 6

Reflecting Here we identify the problem: Building the tower the solution: a suitable configuration of bricks The reuse of the solution... The pattern is repeated become robust Design Patterns in Software Development We can translate the concept of design patterns to software development We have to define: The bricks The configurations of the bricks Object-Orientation provides a natural way to express design patterns 8

OO Design Patterns Design objects are our bricks Informally, a design pattern is a particular configuration of design objects that is, a set of objects and their mutual relations (inheritance, composition, aggregation, association, creation, ) 9 Design Patterns (cont d) A pattern has four elements: ❶ The pattern name. This is used to describe a problem, its solutions and consequences in one or two words. ❷ The problem. This element describes a particular design problem and its context. ❸ The solution. This describes the design elements, their relationships, their responsibilities, and collaborations. ❹ The consequences. These elements are the results and tradeoffs of applying design patterns. 10

Example ❶ The pattern name. Tower. ❷ The problem. Being visible at distance over 360 o. ❸ The solution. Bricks are put side by side in a (circular) sequence. Circles of brick are put one over the other until to reach a predefined height ❹ The consequences. The height can be relevant if the surrounding ground is not flat Exercise Describe a pattern ❶ The pattern name. ❶ The problem. ❶ The solution. ❶ The consequences.

The reference book to read The "Gang of Four": Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley The GoF Approach GoF distinguishes 3 kinds of patterns: Creational: patterns dealing with object creation Structural: patterns dealing with the composition of classes and objects Behavioral: patterns dealing with objects interactions and sharing of responsibilities 14

Patterns Singleton Goal: a class has only one instance AND any other instantiation points to the same object Use: very useful in context where we want to have only one object of a given class If we want to have one and only one we can additionally make all the member data static or nesting a static class

The Singleton Examples Having only one file system Having only one window manager Having one accounting system per single company Singleton pattern Hiding the creation of the class instance in a static function Clients can access to the instance only through the function The constructor is either private or protected

The Singleton Pattern - Creational in Java, constructor must be private. Singleton static instance() SingletonOperation() GetSingletonData() static uniqueinstance singletondata return uniqueinstance 19 Uniqueness of the Budget Budget static instance() BudgetOperation() GetBudgetData() static uniquebudget static nobudgetcreated budgetdata return uniquebudget 20

Java Skeleton public class Budget { private static Budget uniquebudget = null; public static Budget instance() { if (uniquebudget == null) uniquebudget=new Budget(); return uniquebudget; private Budget() {......... Budget townshipbudget = Budget.instance(); Budget wrongbudget = new Budget(); WRONG!!! 21 Patterns

Structural Patterns These patterns are concerned with how structures are formed by the composition of classes and objects. Two types of structural patterns: Structural class pattern which uses inheritance to compose interfaces or implementations. Structural object pattern, which describes the ways to compose objects to realize new functionality. 23 Goal and use Goal: It structures objects and compositions in a tree structure It lets clients treat individual objects and compositions in a uniform way Use: Very useful when we deal with objects that can be located at different levels of abstractions

The Composite Pattern Client Component Operation() Add(Component) Remove(Component) GetChild(int) * children Leaf Operation() Composite Operation() Add(Component) Remove(Component) forall g in children g.operation(); GetChild (int) 25 Example Client Graphic Draw() Add(Graphic) Remove(Graphic) GetChild(int) * Rectangle Draw() graphs Text Picture Draw() Draw() for all g in graphs Add(Graphic g) g.draw(); Remove(Graphic g) GetChild (int) add g to the list of graphs 26

Example of Component class Directory { Directory dir; File[] f;... boolean isdirectory() { return f == null; boolean isfile() { return f!= null; File getfile(int i) {... if (isfile()) return f[i]; return null Directory getdirectory() { if (isdirectory()) return dir; return null; Re-written to get a child Benefits Composite objects can in turn be compounded to created iteratively new complex object The client has a easier work as they can deal with composite and primitive objects at the same way. Clients do not know whether they operate with primitive or composite It is easier to add new objects. No existing code needs to be modified with the addition of a new object

Shortcomings It can make a project too generic It is hard to limit the expansion of the tree Anyone can add a new leaf or composite Adapter (Wrapper) Let different classes work together when they have incompatible interfaces Also called Wrapper 30

Example - Subclasses of Shape LineShape and Polygon shape are strightforward to implement as they are composite of few drawing objects and rules for re-shaping. TextShape is harder it may need complex updating of the text and memory management A TestView class is used to visualize TextShape, The problem is that is not built for visualizing other format shapes. 31 Two solutions Inheriting the interface of Shape and implementing TextView (class adapter) Compounding an instance of TextView in a TextShape and implementing TextShape through the interface Shape (object adapter)

Class Adapter (with multiple inheritance) Client Target Request() Adaptee SpecificRequest() Adapter Request() (protected or private) implementation SpecificRequest() 33 Object Adapter (without multiple inheritance) Client Target Request() Adaptee SpecificRequest() adaptee Adapter Request() adaptee->specificrequest() 34

Example TestView GetExtent() TextShape BoundingBox() Text in TextShape class Return text.getextent() Z

Observer (1/2) Intent Define a 1:n dependency between objects so that when one object changes state, all its dependents are notified and updated automatically Also Known As Dependents, Publish-Subscribe, Model-View Controller Motivation The need to maintain consistency between related objects without making classes tightly coupled 37 Observer (2/2) 38

Structure 39 Participants (1/2) Subject Keeps track of its observers Provides an interface for attaching and detaching Observer objects Observer Defines an interface for update notification 40

Participants (2/2) ConcreteSubject The object being observed Stores state of interest to ConcreteObserver objects Sends a notification to its observers when its state changes ConcreteObserver The observing object Stores state that should stay consistent with the subject's Implements the Observer update interface to keep its state consistent with the subject's 41 Consequences (1/3) Benefits Observers can be added without modifying the subject All subjects know its list of observers Subject does not need to know the concrete class of an observer 42

Implementation Issues (1/4) How does the subject keep track of its observers? Array, linked list What if an observer wants to observe more than one subject? Have the subject tell the observer who it is via the update interface 43 Implementation Issues (2/4) Make sure the subject updates its state before sending out notifications How much info about the change should the subject send to the observers? Push Model Lots Pull Model - Very Little 44

Implementation Issues (3/4) Can the observers subscribe to specific events of interest? If so, it's publish-subscribe Can an observer also be a subject? Yes 45 Implementation Issues (4/4) What if an observer wants to be notified only after several subjects have changed state? Use an intermediary object which acts as a mediator, ChangeManager Subjects send notifications to the mediator object which performs any necessary processing before notifying the observers 46

MVC - Model View Controller Model/View/Controller user interface framework Model = Subject View = Observer Controller is whatever object changes the state of the subject Since Java 1.1 AWT/Swing.event package 47 Java implementation of Observer Java provides the Observable/Observer classes as builtin support for the Observer pattern The java.util.observable class is the base Subject class. Provides methods to add/delete observers Provides methods to notify all observers Uses a Vector for storing the observer references The java.util.observer interface is the Observer interface. It must be implemented by any observer class 48

Observable/Observer Example (1/6) /** * A subject to observe! */ public class ConcreteSubject extends Observable { private String name; private float price; public ConcreteSubject(String name, float price) { this.name = name; this.price = price; System.out.println("ConcreteSubject created: " + name + " at " + price); 49 Observable/Observer Example (2/6) public String getname() { return name; public float getprice() { return price; public void setname(string name) { this.name = name; setchanged(); notifyobservers(name); public void setprice(float price) { this.price = price; setchanged(); notifyobservers(new Float(price)); 50

Observable/Observer Example (3/6) // An observer of name changes. public class NameObserver implements Observer { private String name; public NameObserver() { name = null; System.out.println("NameObserver created: Name is " + name); public void update(observable obj, Object arg) { if (arg instanceof String) { name = (String)arg; System.out.println("NameObserver: Name changed to " + name); else { System.out.println("NameObserver: Some other change to subject!"); 51 Observable/Observer Example (4/6) // An observer of price changes. public class PriceObserver implements Observer { private float price; public PriceObserver() { price = 0; System.out.println("PriceObserver created: Price is " + price); public void update(observable obj, Object arg) { if (arg instanceof Float) { price = ((Float)arg).floatValue(); System.out.println("PriceObserver: Price changed to " + price); else { System.out.println( PriceObserver: Some other change to subject!"); 52

Observable/Observer Example (5/6) // Test program for ConcreteSubject, NameObserver and // PriceObserver public class TestObservers { public static void main(string args[]) { // Create the Subject and Observers. ConcreteSubject s = new ConcreteSubject("Corn Pops", 1.29f); NameObserver nameobs = new NameObserver(); PriceObserver priceobs = new PriceObserver(); // Add those Observers! s.addobserver(nameobs); s.addobserver(priceobs); // Make changes to the Subject. s.setname("frosted Flakes"); s.setprice(4.57f); s.setprice(9.22f); s.setname("sugar Crispies"); 53 A Problem With Observable/Observer (1/2) Problem Suppose the class which we want to be an observable is already part of an inheritance hierarchy: class SpecialSubject extends ParentClass Since Java does not support multiple inheritance, how can we have ConcreteSubject extend both Observable and ParentClass? 54

A Problem With Observable/Observer (2/2) Solution Use Delegation We will have SpecialSubject contain an Observable object We will delegate the observable behavior that SpecialSubject needs to this contained Observable object 55