CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a.

Slides:



Advertisements
Similar presentations
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Advertisements

18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Design Patterns CS is not simply about programming
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
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.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
More OOP Design Patterns
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CSE 332: Design Patterns (Part I) Introduction to Design Patterns Design patterns were mentioned several times so far –And the Singleton Pattern was discussed.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Lexi case study (Part 2) Presentation by Matt Deckard.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Design Patterns
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Design Pattern Dr. Zhen Jiang West Chester University url:
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
Interface Patterns. Adapter Provides the interface a client expects, using the services of a class with a different interface Note Avoid using object.
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.
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.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
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.
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Jim Fawcett CSE776 – Design Patterns Summer 2006
Software Design Refinement Using Design Patterns
Chapter 10 Design Patterns.
Software Design Patterns
Jim Fawcett CSE776 – Design Patterns Fall 2016
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
Design Patterns Lecture part 2.
Introduction to Design Patterns
Behavioral Design Patterns
Design Patterns with C# (and Food!)
object oriented Principles of software design
Informatics 122 Software Design II
Review: Design Pattern Structure
Informatics 122 Software Design II
Presentation transcript:

CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a class instance across multiple uses –Command: package up a function as an object –Iterator: access elements sequentially no matter how stored –Adapter: converts interface you get into one you want –Factory method: creates a related type polymorphically Now, let’s look at some other important patterns (Part II) –Bridge: allow interface, implementation to vary separately –Chain of responsibility: give request to chain of handlers –Composite: common interface to composite/simple objects –Interpreter: build a representation for a simple language –Observer: tell registered observers when state changes –Strategy/template method: vary steps/all of an algorithm –Proxy: forward requests from placeholder to another object –Memento: package up object state without violating encapsulation –Visitor: allow various operations on fixed set of objects (2-way dispatch)

CSE 332: Design Patterns (Part II) Bridge Pattern Problem –Want to decouple an abstraction from its implementation, allowing them to vary separately Context –Want to change implementation details at run-time without impact to clients Solution –Use delegation from an interface class to an implementation class –Commonly used with templates Consequences –Can change (or add) interface without re-implementing –Can change implementation but not impact interface clients

CSE 332: Design Patterns (Part II) Chain of Responsibility Pattern Problem –May not know which objects can handle a request, or may add complexity to specify exactly which ones Context –More than one object can handle a request –Want to pass requests to a group of objects –Handler objects can be specified dynamically Solution –Chain handler objects, pass requests along the chain until one (variation: multiple) object handles it Consequences –Can hand request to a chain without knowing its members –Can tack on a “no one handled request” notifier at end

CSE 332: Design Patterns (Part II) Chain of Responsibility Structure Handler abstract base class provides interface Handler ABC also links handlers together Concrete handlers implement code –To decide whether or not to handle a message –To handle the message itself –To decide whether to consume the message or pass it along after handling it Handler ConcreteHandler method () = 0; method (); successor...

CSE 332: Design Patterns (Part II) Composite Pattern Problem –Distinguishing between composite and simple objects makes applications more complex Context –Want to represent whole-part hierarchies –Want to hide differences between composite and simple objects from clients Solution –Encapsulate composite and simple objects behind a common interface Consequences –Can treat objects consistently as though they were simple –Can treat particular composites in specialized ways Example –Directories contain file system elements (files and directories)

CSE 332: Design Patterns (Part II) Composite Pattern Structure Diagram Component class role gives a consistent interface –For example, file system elements can be listed with ls in Linux Leaf class role is for components without further sub-structure –For example files in Linux Composite class role is for components with multiple parts –For example, Linux directories that contain files and other directories Leaf method (); Component Composite method () = 0; method (); children 1 n

CSE 332: Design Patterns (Part II) Interpreter Pattern Problem –Simple languages can be useful but need an interpreter –Don’t want to write a complex interpreter, e.g., Lex/YACC Context –Grammar is simple, and simplicity more important than efficiency Solution –Represent an interpreter by designing objects for each expression/sub-expression type Consequences –Can write expressions in simple language instead of C++ –Helps simplify some programming tasks like parsing inputs

CSE 332: Design Patterns (Part II) Interpreter Pattern Structure Diagram Expression abstract base class provides interface SubExpression combines child expressions TerminalExpression has no children Expression SubExpression value () = 0; value (); children k 1 TerminalExpression value ();

CSE 332: Design Patterns (Part II) Observer Pattern Problem –Need to update multiple objects when the state of one object changes Context –Multiple objects depend on the state of one object –Set of dependent objects may change at run-time Solution –Allow dependent objects to register with object of interest, notify them of updates when state changes Consequences –When observed object changes others are notified –Useful for user interface programming, other applications

CSE 332: Design Patterns (Part II) Observer Pattern Structure Diagram Each subject may have multiple observers Each concrete observer has a concrete subject Observers call attach and detach on subject –To register/deregister interest in knowing when it changes A concrete observer calls get and set methods on a concrete subject Subject attach (); detach (); notify (); Observer *observers_; ConcreteSubject get (); set (); Observer ConcreteObserver update () = 0; update (); 1 n 1 1

CSE 332: Design Patterns (Part II) Observer Pattern Collaboration Diagram ConcreteObserverB notify (); set (); update (); get (); update (); get (); attach (); ConcreteSubjectConcreteObserverA

CSE 332: Design Patterns (Part II) Strategy Pattern Problem –You want to plug a family of algorithms into and out of a program, interchangeably Context –Must have a consistent interface (used by program) Solution –Encapsulate algorithms as different classes (or templates) –Provide polymorphic interface that each class implements Consequences –Program need not change when new strategies are added Calculator push()=0; pop()=0; Bounded_ Calculator push(); pop(); Unbounded_ Calculator push(); pop();

CSE 332: Design Patterns (Part II) “Template Method” Pattern Problem (besides its name ;-) –You want to plug in and out individual steps of an algorithm Context –You have a fixed algorithm structure within which individual steps may vary Solution –Define a fixed base class function that calls virtual “hook” functions which derived classes override Consequences –Algorithm is parameterized by behavior of the virtual hook functions Examples we’ve seen –Generic programming version is provided by greater, less functors in the STL (e.g., with sort algorithm) Encyclopedia look_up (); find()=0; print()=0; Close_Match_ Encyclopedia find(); print(); Exact_Match_ Encyclopedia find(); print(); Caveat: the name can be confusing – the pattern is not about C++ templates

CSE 332: Design Patterns (Part II) Using Strategy vs. Template Method Use the Strategy pattern when you want to plug in and out entire algorithms –E.g., using different implementation classes for sorting –Need to ensure polymorphism works correctly –Often implies having an abstract base class Use the Template Method pattern when you want to plug in and out individual steps of an algorithm –E.g., using different implementations of steps in an algorithm –Watch out for “proliferation of classes” problem: Strategy may be a better choice to provide all combinations of steps

CSE 332: Design Patterns (Part II) Proxy Pattern Problem –Need to access an object but may not have access (or may need to defer cost of access) until just before use Context –Client may need interface to target object initially E.g., in order to compile –Implementation of target object may be created later May be on a remote machine, etc. Solution –Provide a placeholder with same interface as the target object, which forwards requests to equivalent methods Consequences –Extreme decoupling of proxy and target –For example, they can even be on different machines

CSE 332: Design Patterns (Part II) Proxy Pattern Structure Diagram Proxy “stands in” for target object Proxy exhibits same interface as target object –Forwards method invocations it receives to the target object TargetObject method (); Interface Proxy method () = 0; method (); 1 1

CSE 332: Design Patterns (Part II) Memento Pattern Problem –Want to externalize state of an object without violating encapsulation Context –A snapshot of object state is needed –Providing a state interface would violate encapsulation Solution –Create a memento class with methods to get, set state –Provide an opaque representation of state itself Consequences –Can use memento to send object state over a socket, save it in a file, put it into an undo stack, etc.

CSE 332: Design Patterns (Part II) Visitor Pattern Problem –Operations on collections of objects may not apply to all objects, or apply differently to different objects Context –Object interfaces are fixed and diverse –Need to allow new operations, without coupling Solution –Represent operations to be performed as visitors, with the interface of every visitor representing the different kinds of objects Consequences –Can add new operations without changing objects –Visitors can traverse lists, trees, graphs, etc. of objects

CSE 332: Design Patterns (Part II) Visitor Pattern Structure Diagram Different elements have different concrete interfaces Element abstract base class adds accept interface “Double handshake” between visitor, element –Visitor calls accept on an element –Element calls back to appropriate method (visitA or visitB) This lets visitor tailor its actions to concrete elements Element accept () = 0; ElementA accept (); A_method (); Visitor ConcreteVisitor visitA () = 0; visitB () = 0; visitA (); visitB (); ElementB accept (); B_method ();

CSE 332: Design Patterns (Part II) Visitor Pattern Collaboration Diagram A_method (); visitA (); accept (); ElementBConcreteVisitorElementA B_method (); visitB (); accept ();

CSE 332: Design Patterns (Part II) Summary We’ve looked at quite a few patterns –Singleton: share a class instance across multiple uses –Command: package up a function as an object –Iterator: access elements sequentially no matter how stored –Adapter: converts interface you get into one you want –Factory method: creates a related type polymorphically –Bridge: allow interface, implementation to vary separately –Chain of responsibility: give request to chain of handlers –Composite: common interface to composite/simple objects –Interpreter: build a representation for a simple language –Observer: tell registered observers when state changes –Strategy/template method: vary steps/all of an algorithm –Proxy: forward requests from placeholder to another object –Memento: package up object state without violating encapsulation –Visitor: allow various operations on fixed set of objects (2-way dispatch) Think of these as a “design vocabulary” that you can use CSE 432 will use these throughout next semester –In iterative refinement of project designs and implementations