Design Patterns David Talby. This Lecture Handle Synchronization & Events Observer Simplify Complex Interactions Mediator Change Behavior Dynamically.

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

Software Engineering 2003 Jyrki Nummenmaa 1 OBJECT ARCHITECTURE DESIGN These slides continue with our example application, based on the simplified.
Design Patterns David Talby. This Lecture n More for a Document Editor u Synchronizing Multiple Windows F Observer u Simplifying complex interactions.
 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 David Talby. This Lecture Representing other objects Proxy, Adapter, Façade Re-routing method calls Chain of Responsibility Coding partial.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
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,
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Design Patterns David Talby. This Lecture n Patterns for a User Interface u Representing a Document F Composite, Flyweight, Decorator u Writing Portable.
Design Patterns David Talby. This Lecture n The Creational Patterns u Builder u Prototype u Factory Method u (Abstract Factory) u Singleton n Choosing.
Design Patterns David Talby. This Lecture n The Creational Patterns u Abstract Factory u Builder u Prototype u Factory Method n Choosing Between Them.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Object-Oriented Analysis and Design
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.
Design Patterns David Talby. This Lecture n What is it all about? n Abstract Factory n Composite n Strategy.
Design Patterns David Talby. This Lecture n The rest of the pack u Working over a network F Proxy, State, Chain of Responsibility u Working with external.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design 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.
BY VEDASHREE GOVINDA GOWDA
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
MVC pattern and implementation in java
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Design Patterns David Talby. This Lecture n Re-Routing Method Calls u Proxy, Chain of Responsibility n Working with external libraries u Adapter, Façade.
An Introduction to Software Architecture
CS 210 Introduction to Design Patterns September 28 th, 2006.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.
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.
Object Oriented Design David Talby. Welcome! n Introduction n UML u Use Case Diagrams u Interaction Diagrams u Class Diagrams n Design Patterns u Composite.
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.
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.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 31. Review Creational Design Patterns – Singleton Pattern – Builder Pattern.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
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.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
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.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
The State Design Pattern A behavioral design pattern. Shivraj Persaud
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
1/23/2018 Design Patterns David Talby.
4/16/2018 Design Patterns David Talby.
Observer Design Pattern
An Introduction to Software Architecture
Object Oriented Design
Presentation transcript:

Design Patterns David Talby

This Lecture Handle Synchronization & Events Observer Simplify Complex Interactions Mediator Change Behavior Dynamically Strategy, State Undo, Macros and Versions Command

10. Observer Define a one-to-many dependency between objects, so that changing one automatically updates others For example, a spreadsheet and several charts of it are open Changing data in a window should be immediately reflected in all

The Requirements Document and Chart classes must not know each other, for reuse Easily add new kinds of charts or other links A dynamic number of charts

The Solution Terminology Subject and Observer Publisher and Subscriber Listeners Subjects attach and detach listeners, and notify of events Clients update themselves after receiving a notification

The Solution II Here’s an abstract observer: class Observer { void update() = 0; } Concrete observers such as class Chart will inherit Observer

The Solution III Here’s the (concrete!) subject: class Subject { void attach(Observer *o) { observers.add(o); } void detach(Observer *o) { observers.remove(o); } void notify() { for i in observers do o->update(); } protected: List observers; }

The Solution IV Both subject and observer will usually inherit from other classes as well If multiple inheritance is not available, the observer must be a separate class that has a reference to the chart object and updates it Java has a special mechanism – Inner classes – to make this easier

The UML

The Fine Print Observing more than one subject Update must include an extra argument to tell who is updating Observing only certain events Attach must include an extra argument to tell which events interest this observer Observing small changes Update includes arguments to tell what changed, for efficiency

The Fine Print II Who calls Notify ? Greedy – the subjects, on change Lazy – the observers, on query Common errors Forgetting to detach an object when it is destroyed Calling Notify in an inconsistent state Java includes Observer as part of the standard libraries In package java.util

Known Uses All frameworks of all kinds MFC, COM, Java, EJB, MVC, … Handle user interface events Handle asynchronous messages

11. Mediator Encapsulate a complex interaction to preserve loose coupling Prevent many inter-connections between classes, which means that changing their behavior requires subclassing all of them For example, a dialog box includes many interactions of its widgets. How do we reuse the widgets?

The Requirements A widget is a kind of colleague Colleague don’t know about the interactions they participate in Can be reused for different dialogs Colleagues don’t know about others Allow only O(n) connections Easy to change interactions

The Solution All colleagues talk with a mediator The mediator knows all colleagues Whenever a colleague changes, it notifies its mediator The mediator codes the interaction logic, and calls operations on other colleagues

The Solution II An example interaction:

The Solution III Only O(n) connections:

The UML

The Fine Print The interaction logic (mediator) and colleagues can be reused separately and subclassed separately Protocols are simpler since n-to-1 relations replace n-to-m relations Abstract mediator class is unnecessary if there’s only one mediator Observer or mediator? One-to-many or many-to-many? Should the logic be centralized?

Known Uses Widgets in a user interface Delphi and VB “hide” this pattern Connectivity constraints in diagrams

12. Strategy A program must switch between complex algorithms dynamically For example, a document editor has several rendering algorithms, with different time/beauty tradeoffs Word is a common example

The Requirements Algorithms are complex, would be havoc to have them inside the one Document class Switch algorithms dynamically Easy to add new algorithms

The Solution Define an abstract class that represents an algorithm: class Renderer { void render(Document *d) = 0; } Each specific algorithm will be a descendant class FastRenderer, TexRenderer, …

The Solution II The document itself chooses the rendering algorithm: class Document { render() { renderer->render(this); } setFastRendering() { renderer = new FastRenderer(); } private: Renderer *renderer; }

The GoF UML

The Fine Print Inheriting a strategy would deny a dynamic switch Some strategies may not use all information passed from Context Strategies can be stateless, and then they can be shared In some cases strategy objects are optional

Known Uses Document rendering programs Compiler code optimizations Different heuristic algorithms (games, portfolio selection) Different memory management schemes (Booch components) Validation of dialog boxes (optional strategies, Borland ObjectWindows )

13. State Allow an object to alter its behavior when its internal state changes For example, most methods of a TCPConnection object behave in different ways when the connection is closed, established or listening How do we encapsulate the logic and data of every state?

The Requirements A class has a state diagram, and many methods behave in wildly different ways in different states When in a state, only allocate memory for data of that state The logic of a specific state should be encapsulated

Pattern of Patterns Encapsulate the varying aspect State of an object Interfaces Let’s have a TCPState interface that has all the state-sensitive methods Inheritance describes variants TCPEstablished, TCPListen and TCPClosed implement the interface Composition allows a dynamic choice between variants

The Solution II A TCPConnection codes state transitions and refers to a TCPState

The UML

The Fine Print In complex cases it is better to let states define transitions, by adding a SetState method to Context States may be created on-demand or on Context’s creation This pattern is really a workaround for the lack of dynamic inheritance State is very much like Strategy State = Many (small) actions Strategy = One (complex) action

Known Uses Streams and connections Different tools on a drawing program Select, Erase, Crop, Rotate, Add, …

14. Command Encapsulate commands as objects We’ll take the the uses one by one: Undo/Redo Macros Queues and logs Version control Crash recovery Message Grouping

The Requirements I Undo / redo at unlimited depth Only store relevant data for undo Easy to add commands

The Solution Repesent a command as a class: class Command { public: virtual void execute() = 0; virtual void undo() = 0; }

The Solution II Concrete commands hold undo data: class DeleteLine : public Command { void execute() { line = document->getLine(); document->removeLine(); } void undo() { document->addLine(line); } private: Line line; }

The Solution III Keep a list of executed commands: Array commands; int i; When you click the ‘Undo’ button: commands(i)->undo(); i--; When you click the ‘Redo’ button: commands(i)->execute(); i++;

The Solution IV Whenever a command is activated : commands.add(new_command); i = commands.count(); When you save a document: document->save(); commands.clear(); i = 0; The commands list may or may not be limited in size Only relevant undo data is kept

The Requirements II Macros are a series of commands Any command with any of its options may be used There are also for and while loops, if statements, calls to other macros...

The Solution A macro is a Composite Command if, for, while are Decorator Commands

The Requirements III Commands are accessible from menus as well as toolbars A command may be available from more than one place We’d like to configure the menus and toolbars at runtime

The Solution Each MenuItem or ToolbarItem refers to its command object Just as it refers to an image The command can be configured Less command classes Macros fit in the picture as well!

The Requirements IV Keep multiple versions of a document When saving, only store the changes from the previous version

The Solution The changes are exactly the list of commands since the last version was loaded In addition, a compaction algorithm is needed for commands that cancel each other Save = Serialize the compacted list Load = Read early version and call execute on command lists

(More!) Known Uses Programs log commands to disk so they can be used in case of a crash Works, since commands are small Usually in a background thread Commands can be grouped and sent as one command to a server Grouping for efficient communication Grouping to define a transaction Works even for user defined macros!

Summary Pattern of patterns Encapsulate the varying aspect Interfaces Inheritance describes variants Composition allows a dynamic choice between variants Design patterns are old, well known and thoroughly tested ideas Over twenty years!