March 200692.3913 Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable.
©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
Winter 2007ACS-3913 Ron McFadyen1 Also known as publish/subscribe The essence of this pattern is that one or more objects (called observers or listeners)
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Design Patterns Pepper. Find Patterns Gang of Four created 23 Siemens published another good set x
Chapter 7 – Object-Oriented Design
Design Patterns CMPS Design Patterns Consider previous solutions to problems similar to any new problem ▫ must have some characteristics in common.
Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Name of.
Computer Monitoring System for EE Faculty By Yaroslav Ross And Denis Zakrevsky Supervisor: Viktor Kulikov.
March Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
March Ron McFadyen1 Observer P Also known as Publish-Subscribe Applied in order to implement the Model-View Separation principle (see.
Fall 2009ACS-3913 Ron McFadyen1 Observer Problem: There are many objects (subscribers) needing to know of the state changes, or events, of another object.
Spring 2010ACS-3913 Ron McFadyen1 Weather Station Page 39+ In this application, weather station devices supply data to a weather data object. As the data.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
Feb Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface the client expects. An adapter lets classes work.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Winter 2007ACS-3913 Ron McFadyen1 Classes Represented by a rectangle with possibly 3 compartments Customer Name Address Customer Name Address getName()
Spring 2010ACS-3913 Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
Fall 2009ACS-3913 R McFadyen1 Singleton Problem: Exactly one instance of a certain object is required (this object is called a singleton). We must ensure.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Useful for.
Winter 2007ACS-3913 Ron McFadyen1 Observer Pattern Problem: There are many objects (observers / subscribers) needing to know of the state changes, or events,
March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.
Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
Design Patterns Ref : Chapter 15 Bennett et al. useful groups of collaborating classes that provide a solution to commonly occuring problems. provide.
March Ron McFadyen1 Observer Problem: There are many objects (subscribers) needing to know of the state changes, or events, of another object.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Design Patterns.
9/28/01F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Design Patterns.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
CS 210 Introduction to Design Patterns September 7 th, 2006.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
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.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CSC 480 Software Engineering Design With Patterns.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Design Patterns Introduction
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
The Observer Pattern.
Singleton Pattern Presented By:- Navaneet Kumar ise
Design Patterns CSCE 315 – Programming Studio Spring 2013.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Patterns in programming
MPCS – Advanced java Programming
Design Patterns C++ Java C#.
Design Patterns C++ Java C#.
Introduction To Design Patterns
Observer Design Pattern
Singleton Pattern Command Pattern
Introduction to Behavioral Patterns (1)
CSC 480 Software Engineering
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Advanced ProgramMING Practices
Advanced ProgramMING Practices
CSC 480 Software Engineering
Introduction To Design Patterns
Introduction To Design Patterns
Presentation transcript:

March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code; it is a description or template for how to solve a problem that can be used in many different situations.

March Ron McFadyen2 Design Patterns Patterns originated as an architectural concept by Christopher Alexander. 1977: A Pattern Language: Towns, Buildings, Construction is a book on architecture - by Christopher Alexander, Sara Ishikawa and Murray Silverstein In 1994: Design Patterns: Elements of Reusable Object-Oriented Software -GoF First Pattern Languages of Programs conference Some online resourses: en.wikipedia.org/wiki/Design_pattern_(computer_science)

March Ron McFadyen3 Design Patterns Design patterns are composed of several sections Format used by the Gang of Four. Pattern Name and Classification Intent Also Known As Motivation Applicability Structure Participants Collaboration Consequences Implementation Sample Code Known Uses Related Pattern Of particular interest are the Structure, Participants, and Collaboration sections.

March Ron McFadyen4 Also known as publish subscribe The essence of this pattern is that one or more objects (called observers or listeners) are registered (or register themselves) to observe an event which may be raised by the observed object (the subject). The object which may raise an event maintains a collection of the observers. Observer Pattern

March Ron McFadyen5 Observer Pattern The object that is responsible for the event is given the responsibility of monitoring for the event – this object is the subject. Objects that are interested in the event must register with the subject as interested parties – as observers. The subject will notify its observers when the event occurs. Two interfaces are used: a subscriber interface and a subject interface.

March Ron McFadyen6 Observer Pattern Observer: objects that want to be notified of a certain event. An observer must have an update method whereby it is notified of an event. Subject: the object that triggers the event. It must implement: attach (observer) - add an observer to its list of observers detach (observer) - remove an observer from … notify () - goes through its list of observers calling each observer’s update method As needed - additional methods to allow an observer to get additional information

March Ron McFadyen7 Interfaces «interface» Subject attach() detach() notify() «interface» Observer update()

March Ron McFadyen8 Generic pattern «interface» Subject attach() detach() notify() «interface» Observer update() ConcreteSubject attach() detach() notify() … ConcreteObserver update() … *

March Ron McFadyen9 Weather Station Example «interface» Subject attach() detach() notify() «interface» Observer update() WeatherData attach() detach() notify() CurrentConditionsDisplay update() display() * StatisticsDisplay update() display() ForecastDisplay update() display() «interface» DisplayElement display()

March Ron McFadyen10 Weather Station Example «interface» Subject attach() detach() notify() «interface» Observer update() WeatherData attach() detach() notify() CurrentConditionsDisplay update() display() * StatisticsDisplay update() display() ForecastDisplay update() display() «interface» DisplayElement display() Observer Concrete observer Concrete subject observersubject Concrete observer

March Ron McFadyen11 Object diagram What collection of objects exist? How are they related?

March Ron McFadyen12 Behaviour What is the typical behaviour expressed with sequence diagrams?

March Ron McFadyen13 Decorator pattern This design pattern allows new/additional behavior to be added to an existing method of an object dynamically Decorator works by wrapping a new "decorator" object around the original object, which is typically achieved by passing the original object as a parameter to the constructor of the decorator, with the decorator implementing the new functionality. The interface of the original object needs to be maintained by the decorator. Decorators are alternatives to subclassing. Subclassing adds behaviour at compile time whereas decorators provide new behaviour at runtime.

March Ron McFadyen14 Decorator 1 component decorator Concrete component Concrete decoratorA Concrete decoratorB operation() Client

March Ron McFadyen15 Decorator textbook example 1 component Beverage Condiment Decorator HouseBlend cost() getDescription getDescription() DarkRoast Decaf cost() Espresso cost() MilkMochaSoyWhip cost() getDescription() cost() getDescription() cost() getDescription() cost() getDescription()

March Ron McFadyen16 Decorator textbook example Beverage Condiment Decorator HouseBlend DarkRoast Decaf Espresso MilkMochaSoyWhip Decorator Concrete decorator Concrete component Component Concrete decorator Concrete component

March Ron McFadyen17 Object diagram What collection of objects exist? How are they related?

March Ron McFadyen18 Behaviour What is the typical behaviour expressed with sequence diagrams?

March Ron McFadyen19 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly one object is needed to coordinate actions across the system. Singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist.

March Ron McFadyen20 Singleton pattern If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made either private or protected. eager instantiation lazy instantiation - no resources until needed The singleton pattern must be carefully constructed in multi- threaded applications.

March Ron McFadyen21 Singleton pattern Structure? Behaviour?

March Ron McFadyen22 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly one object is needed to coordinate actions across the system. Singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist.

March Ron McFadyen23 Singleton pattern If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made either private or protected. eager instantiation lazy instantiation - no resources until needed The singleton pattern must be carefully constructed in multi- threaded applications. getInstance() Singleton() Singleton Public Private

March Ron McFadyen24 Singleton pattern Structure? Behaviour? How would singleton combine with observer if there should only be one subject? Could singleton and decorator combine? Could the same decorated object be decorated differently for different clients?

March Ron McFadyen25 Singleton pattern Example: Suppose we have a system of classes that need to use random numbers for some simulation purpose. A random number generator is typically used to generate a sequence of pseudo-random numbers; you provide a seed value that is used to begin the sequence. seed = (seed * ) % 2 48 We can design a singleton random number generator class that all classes would reference. «singleton » SingleRandom setSeed() nextInt() getInstance()

March Ron McFadyen26 Singleton pattern import java.io.*; public class DriverSingleRandom{ public static void main(String[] args){ // generate random numbers between 1 and 6 System.out.println("beginning listing"); SingleRandom r = SingleRandom.getInstance(); int seed = 101; r.setSeed(seed); for (int i=0;i<10;i++){ int result = Math.abs(r.nextInt())%6+1; System.out.println(result); } }// end main }

March Ron McFadyen27 Singleton pattern import java.util.*; public class SingleRandom { private Random generator; private static SingleRandom instance; private SingleRandom() { generator = new Random(); } public void setSeed(int seed) { generator.setSeed(seed); } public int nextInt() { return generator.nextInt(); } public static synchronized SingleRandom getInstance() { if (instance == null) instance = new SingleRandom(); return instance;} }

March Ron McFadyen28 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command to perform its function and the concrete command will in turn ask some receiver to perform a pre-determined action.

March Ron McFadyen29 Command – generic class diagram > Command Concrete Command Receiver Invoker Command Concrete commandreceiver command action1() action2() execute() setCommand() invoker

March Ron McFadyen30 Command – text example > Command lightOn Command Light Remote control Command Concrete command receiver command on() off() execute() setCommand() invoker lightOff Command execute() Concrete command null Command execute() Concrete command

March Ron McFadyen31 Command - behaviour :Light:RemoteControl execute() lightOn() execute() lightOff() :LightOn Command :LightOff Command :null Command execute()

March Ron McFadyen32 Command Example: User interfaces typically give you ways to issue a particular command Java Swing classes use the command pattern to perform application-specific functionality. Action objects are used. Button classes, menu items, etc. maintain a reference to an action defined by the Action interface. Whenever a Swing component activates, it calls its action’s (command’s) actionPerformed() method (execute() method). See sample files on web page