© SERG Software Design (OOD Patterns) Object-Oriented Design Patterns Topics in Object-Oriented Design Patterns Compliments of Spiros Mancoridis Material.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Design Patterns Design Patterns Composite Pattern Observer Pattern.
CSE3308/CSC Software Engineering: Analysis and DesignLecture 5B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/CSC3080/DMS/2000/12.
CSE Software Engineering: Analysis and Design, 2005Lecture 8A.1 Software Engineering: Analysis and Design - CSE3308 Design and Analysis Patterns.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
CSE Software Engineering: Analysis and Design, 2002Lecture 7B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/DMS/2002/15.
1 CS 501 Spring 2008 CS 501: Software Engineering Lectures 17 & 18 Object Oriented Design 3 & 4.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns Ric Holt & Sarah Nadi U Waterloo, March 2010.
OOMPA Lecture 9 Design Patterns Composite Pattern Observer Pattern.
Composite Design Pattern. Motivation – Dynamic Structure.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
© Spiros Mancoridis 27/09/ Software Design Topics in Object-Oriented Design Patterns Material drawn from [Gamma95] and [Coplien95] Revised and augmented.
UML - Patterns 1 Design Patterns. UML - Patterns 2 Becoming Good OO Developers Developing good OO Software is hard Takes a lot of time to take advantage.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
©Ian Sommerville 1995/2000 (Modified by Spiros Mancoridis 1999) Software Engineering, 6th edition. Chapter 14 Slide 1 Software Reuse l Building software.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Structural Design Patterns
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns: Elements of Reusable Object – Oriented Software Web Apps and Services.
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
© SERG Software Design (OOD Patterns) Pg 1 Object-Oriented Design Patterns Topics in Object-Oriented Design Patterns Material drawn from [Gamma95,Coplien95]
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
©Ian Sommerville 1995/2000 (Modified by DUCS 1999) Software Engineering, 6th edition. Chapter 14 Slide 1 Software Reuse l Building software from reusable.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Examples (D. Schmidt et al)
Design Patterns: MORE Examples
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Factory Patterns 1.
Design Patterns with C# (and Food!)
Object-Oriented Design Patterns
Object-Oriented Design
Design Patterns - A few examples
Software Reuse Building software from reusable components.
Design Patterns Outline
Design Patterns A Case Study: Designing a Document Editor
Jim Fawcett CSE776 – Design Patterns Summer 2003
Observer Pattern 1.
Introduction to Design Patterns
Presented by Igor Ivković
Adapter Pattern Jim Fawcett
Adapter Pattern Jim Fawcett
Presentation transcript:

© SERG Software Design (OOD Patterns) Object-Oriented Design Patterns Topics in Object-Oriented Design Patterns Compliments of Spiros Mancoridis Material drawn from [Gamma95,Coplien95]

© SERG Software Design (OOD Patterns) Why design patterns It is very difficult to design object-oriented software that is reusable Good design should be specific to the problem you trying to solve, yet general enough to address future problems and new requirements Good designers reuse the solutions that have worked well in the past. Good solutions need to be reused!

© SERG Software Design (OOD Patterns) Why design patterns Design patterns solve specific design problems in an elegant, flexible, and reusable way Design pattern is a record of experience in designing object-oriented software. Design pattern is generic and can be applied to the wide range of software design problems

© SERG Software Design (OOD Patterns) Why design patterns? Make system more reusable Improve documentation and maintenance Provide with explicit solutions –Well defined classes, and class interactions New Design Patterns are still being discovered today as new software being written

© SERG Software Design (OOD Patterns) OOD Patterns Topics Terminology and Motivation Some of the Reusable OO Design Patterns: –Creational (abstraction of the object instantiation ) Singleton Abstract Factory –Structural (Object composition ) Adapter Façade Composite –Behavioral (Algorithms, assigning responsibilities) Iterator Observer

© SERG Software Design (OOD Patterns) Terminology and Motivation

© SERG Software Design (OOD Patterns) Design Patterns Good designers know not to solve every problem from first principles. They reuse solutions. Practitioners do not do a good job of recording experience in software design for others to use.

© SERG Software Design (OOD Patterns) Design Patterns (Cont’d) A Design Pattern systematically names, explains, and evaluates an important and recurring design. We describe a set of well-engineered design patterns that practitioners can apply when crafting their applications.

© SERG Software Design (OOD Patterns) Becoming a Master Designer First, One Must Learn the Rules: –Algorithms –Data Structures –Languages Later, One Must Learn the Principles: –Structured Programming –Modular Programming –OO Programming

© SERG Software Design (OOD Patterns) Becoming a Master Designer (Cont’d) Finally, One Must Study the Designs of Other Masters: –Design patterns must be understood, memorized, and applied. –There are thousands of existing design patterns.

© SERG Software Design (OOD Patterns) Reusable OO Design Patterns

© SERG Software Design (OOD Patterns) The Adapter Pattern Intent: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. Motivation: When we want to reuse classes in an application that expects classes with a different interface, we do not want (and often cannot) to change the reusable classes to suit our application.

© SERG Software Design (OOD Patterns) Example of the Adapter Pattern Editor Shape BoundingBox() CreateManipulator() TextView GetExtent() LineShape BoundingBox() CreateManipulator() TextShape BoundingBox() CreateManipulator() return text -> GetExtent() return new Text Manipulator text

© SERG Software Design (OOD Patterns) Structure of the Adapter Pattern Using Multiple Inheritance Client Target Request() Adaptee SpecificRequest() Adapter Request() SpecificRequest() (implementation)

© SERG Software Design (OOD Patterns) Structure of the Adapter Pattern Using Object Composition Client Target Request() Adaptee SpecificRequest() Adapter Request() SpecificRequest() adaptee

© SERG Software Design (OOD Patterns) Participants of the Adapter Pattern Target: Defines the application-specific interface that clients use. Client: Collaborates with objects conforming to the target interface. Adaptee: Defines an existing interface that needs adapting. Adapter: Adapts the interface of the adaptee to the target interface.

© SERG Software Design (OOD Patterns) The Facade Pattern (Intent) Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

© SERG Software Design (OOD Patterns) The Facade Pattern (Motivation) Structuring a system into subsystems helps reduce complexity. A common design goal is to minimize the communication and dependencies between subsystems. Use a facade object to provide a single, simplified interface to the more general facilities of a subsystem.

© SERG Software Design (OOD Patterns) Example of the Facade Pattern Compiler Scanner Parser Token ProgNode ProgNodeBuilder RISCCG StackMachineCG Statement Node Expression Node Variable Node Compiler Subsystem Classes Compile() CodeGenerator

© SERG Software Design (OOD Patterns) Structure of the Facade Pattern Subsystem Classes Facade Client Classes

© SERG Software Design (OOD Patterns) Participants of the Facade Pattern Facade: –Knows which subsystem classes are responsible for a request. –Delegates client requests to appropriate subsystem objects. Subsystem Classes: –Implement subsystem functionality. –Handle work assigned by the facade object. –Have no knowledge of the facade; that is, they keep no references to it.

© SERG Software Design (OOD Patterns) The Iterator Pattern (Intent) Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Move the responsibility for access and traversal from the aggregate object to the iterator object.

© SERG Software Design (OOD Patterns) The Iterator Pattern (Motivation) One might want to traverse an aggregate object in different ways. One might want to have more than one traversal pending on the same aggregate object. Not all types of traversals can be anticipated a priori. One should not bloat the interface of the aggregate object with all these traversals.

© SERG Software Design (OOD Patterns) Example of the Iterator Pattern List Count() Append(Element) Remove(Element) … ListIterator First() Next() IsDone() CurrentItem() index list

© SERG Software Design (OOD Patterns) Structure of the Iterator Pattern Aggregate CreateIterator() ConcreteAggregate CreateIterator() Iterator First() Next() IsDone() CurrentItem() ConcreteIterator return new ConcreteIterator(this)

© SERG Software Design (OOD Patterns) Participants of the Iterator Pattern Iterator: Defines an interface for accessing and traversing elements. Concrete Iterator: Implements an iterator interface and keeps track of the current position in the traversal of the aggregate. Aggregate: Defines an interface for creating an iterator object. Concrete Aggregate: Implements the iterator creation interface to return an instance of the proper concrete iterator.

© SERG Software Design (OOD Patterns) The Composite Pattern (Intent) Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

© SERG Software Design (OOD Patterns) The Composite Pattern (Motivation) If the composite pattern is not used, client code must treat primitive and container classes differently, making the application more complex than is necessary.

© SERG Software Design (OOD Patterns) Example of the Composite Pattern Graphic Draw() Add(Graphic) Remove(Graphic) GetChild(int) LineTextRect. Draw() Picture Draw() Add(Graphic) Remove(Graphic) GetChild(int) forall g in graphics g.Draw() graphics

© SERG Software Design (OOD Patterns) Structure of the Composite Pattern Client Component Operation() Add(Component) Remove(Component) GetChild(int) LeafComposite Operation() Add(Component) Remove(Component) GetChild(int) forall g in children g.Operation() children

© SERG Software Design (OOD Patterns) Participants of Composite Pattern Component: –Declares the interface for objects in the composition. –Implements default behavior for the interface common to all classes. –Declares an interface for accessing and managing its child components. –Defines an interface for accessing a component’s parent in the recursive structure (optional).

© SERG Software Design (OOD Patterns) Participants of Composite Pattern (Cont’d) Leaf: –Represents leaf objects in the composition. A leaf has no children. –Defines behavior for primitive objects in the composition. Composite: –Defines behavior for components having children. –Stores child components. –Implements child-related operations in the component interface.

© SERG Software Design (OOD Patterns) Participants of Composite Pattern (Cont’d) Client: –Manipulates objects in the composition through the component interface.

© SERG Software Design (OOD Patterns) The Abstract Factory Pattern (Intent) Provides an interface for creating families of related or dependent objects without specifying their concrete classes.

© SERG Software Design (OOD Patterns) The Abstract Factory Pattern (Behavior) Sometimes we have systems that support different representations depending on external factors. There is an Abstract Factory that provides an interface for the client. In this way the client can obtain a specific object through this abstract interface.

© SERG Software Design (OOD Patterns) Example of the Abstract Factory Pattern WidgetFactory CreateScrollBar() Create Window() CreateScrollBar() Create Window() CreateScrollBar() Create Window() MotifWidgetFactoryPMWidgetFactoryPMWindowMotifWindow Window Client PMScrollBarMotifScrollBar ScrollBar

© SERG Software Design (OOD Patterns) Structure of the Abstract Factory Pattern AbstractFactory CreateProductA() CreateProductB() CreateProductA() CreateProductB() CreateProductA() CreateProductB() ConcreteFactory1ConcreteFactory2ProductA1ProductA2 AbstractProductA Client ProductB1ProductB2 AbstractProductB

© SERG Software Design (OOD Patterns) Participants of the Abstract Factory Pattern Abstract Factory: –Declares an interface for operations that create abstract product objects. Concrete Factory: –Implements the operations to create concrete product objects.

© SERG Software Design (OOD Patterns) Participants of the Abstract Factory Pattern (Cont’d) Abstract Product: –Declares an interface for a type of product object. Concrete Product: –Defines a product object to be declared by the corresponding concrete factory. (Implements the Abstract Product interface). Client: –Uses only interfaces declared by Abstract Factory and Abstract Product classes.

© SERG Software Design (OOD Patterns) Abstract Factory Example class AbstractFactory { private: static final String MOTIF_WIDGET_NAME = "Motif"; static final String WINDOWS_WIDGET_NAME = "Windows"; public: static AbstractFactory * getFactory(String name) { if (name.equals(MOTIF_WIDGET_NAME)) return new MotifFactory( ); else if (name.equals(WINDOWS_WIDGET_NAME)) return new WindowsFactory( ); return null; } virtual AbstractWindow * getWindow() = 0; };

© SERG Software Design (OOD Patterns) Abstract Factory Example (Cont’d) // Code for class MotifFactory: class MotifFactory : public AbstractFactory { public: MotifFactory() { } AbstractWindow * getWindow() { return new MotifWindow(); } };

© SERG Software Design (OOD Patterns) Abstract Factory Example (Cont’d) // Code for class WindowsFactory: class WindowsFactory : public AbstractFactory { public: WindowsFactory() { } AbstractWindow *getWindow() { return new WindowsWindow(); } };

© SERG Software Design (OOD Patterns) Abstract Factory Example (Cont’d) // Code for class AbstractWindow: class AbstractWindow { // … public: virtual void show() = 0; };

© SERG Software Design (OOD Patterns) Abstract Factory Example (Cont’d) //Code for class MotifWindow: class MotifWindow : public AbstractWindow { public: MotifWindow() { } virtual void show() { JFrame * frame = new JFrame(); frame->setSize(300, 300); frame->setVisible(true); } };

© SERG Software Design (OOD Patterns) Abstract Factory Example (Cont’d) // Code for class WindowsWindow: class WindowsWindow : public AbstractWindow { public: WindowsWindow() { } virtual void show() { JFrame * frame = new JFrame(); frame->setSize(300, 300); frame->setVisible(true); } };

© SERG Software Design (OOD Patterns) Abstract Factory Example (Cont’d) // Code for class Client: class Client { public: Client(String factoryName) { AbstractFactory * factory = AbstractFactory::getFactory(factoryName); AbstractWindow * window = factory->getWindow(); window->show(); } }; void main(int argC, char ** argV) { //args[0] contains the name of the family of widgets //to be used by the Client class (Motif or Windows) Client(args[0]) client; }

© SERG Software Design (OOD Patterns) The Observer Pattern (Intent) Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

© SERG Software Design (OOD Patterns) The Observer Pattern (Motivation) A common side-effect of partitioning a system into a collection of cooperating classes is the need to maintain consistency between related objects. You don’t want to achieve consistency by making the classes tightly coupled, because that reduces their reusability.

© SERG Software Design (OOD Patterns) Example of the Observer Pattern abc 60 y x z8010 a b c a b c a = 50% b = 30% c = 20% change notificationrequests, modifications

© SERG Software Design (OOD Patterns) Structure of the Observer Pattern Subject Attach(Observer) Detach(Observer) Notify() ConcreteSubject subjectState GetState() SetState() for all o in observers { o -> Update()} Observer Update() observers ConcreteObserver observerState = subject->GetState() Update() observerState return subjectState subject

© SERG Software Design (OOD Patterns) Structure of the Observer Pattern The key objects in this pattern are subject and observer. –A subject may have any number of dependent observers. –All observers are notified whenever the subject undergoes a change in state.

© SERG Software Design (OOD Patterns) Participants of the Observer Pattern Subject: –Knows its numerous observers. –Provides an interface for attaching and detaching observer objects. –Sends a notification to its observers when its state changes. Observer: –Defines an updating interface for concrete observers.

© SERG Software Design (OOD Patterns) Participants of the Observer Pattern (Cont’d) Concrete Subject: – Stores state of interest to concrete observers. Concrete Observer: –Maintains a reference to a concrete subject object. –Stores state that should stay consistent with the subject's. –Implements the updating interface.

© SERG Software Design (OOD Patterns) Singleton Intent –Ensure a class only has one instance, and provide a global point of access to it It is important for some classes to have exactly one instance –One file system, one window manager. We can make a class responsible for controlling the number of instances we create, but this would require modifications

© SERG Software Design (OOD Patterns) Singleton Example class MyClass { public: static MyClass* Instance() { if( _instance == 0 ) { _instance = new MyClass(); } return _instance; } protected: MyClass(); // Disable construction of a class private: static MyClass * _instance; }; MyClass* MyClass::_instance = 0;

© SERG Software Design (OOD Patterns) Singleton Another Example class MyClass { friend class Singleton; public: // … private: MyClass(); // Disable construction };

© SERG Software Design (OOD Patterns) Singleton Another Example template class Singleton { public: static void deleteSingleton(); static T & getSingleton(); private: Singleton(); Singleton(const Singleton & ); Singleton & operator=(const Singleton &); ~Singleton(); static T * _self; };

© SERG Software Design (OOD Patterns) Singleton Another Example template T & Singleton :: getSingleton( ) { if ( _self == NULL ) { _self = new T( ); } return *_self; } void main() { MyClass & myClassA = Singleton ::getSingleton(); MyClass & myClassB = Singleton ::getSingleton(); } // myClassA and myClassB both reference the same object

© SERG Software Design (OOD Patterns) References [Gamma95] Gamma, E., Helm, R., Johnson, R., Vlissides, J., Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, [Coplien95] J. O. Complien, D.C. Schmidt, Pattern Languages of Program Design. Addison-Wesley, 1995.