Behavioral Patterns Part-I introduction UNIT-VI

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chain of Responsibility Pattern Gof pp Yuyang Chen.
Computer Science 313 – Advanced Programming Topics.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Matt Klein 7/2/2009.  Intent  Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.
Zaki Alasadi Supervisor:Dr noorhosseini.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
Command Pattern Chihung Liao Cynthia Jiang. Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Cook Make Food()
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
Algorithm Programming Behavioral Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
1 CS 501 Spring 2007 CS 501: Software Engineering Lectures 17 & 18 Object Oriented Design 3 & 4.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
The Interpreter Pattern. Defining the Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses.
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.
Composite Design Pattern. Motivation – Dynamic Structure.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,
02 - Behavioral Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
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 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.
Salman Marvasti Sharif University of Technology Winter 2015.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Chain of Responsibility Behavioral Pattern. Defination Avoid coupling between the sender and receiver by giving more than one object a chance to handle.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
CS 5150 Software Engineering Lecture 16 Program Design 3.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
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.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Chapter 10 Design Patterns.
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
MPCS – Advanced java Programming
Common Design Patterns
Introduction to Design Patterns
Behavioral Design Patterns
Observer Design Pattern
Leftover Patterns Chain of Responsibility
Presentation by Julie Betlach 7/02/2009
Design Patterns Satya Puvvada Satya Puvvada.
Design Patterns A Case Study: Designing a Document Editor
Jim Fawcett CSE776 – Design Patterns Summer 2003
CSE 143 Lecture 27: Advanced List Implementation
Iterator.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Behavioral Design Pattern
PH Chapter 3 Thanks for the Memory Leaks Pushme-Pullyu (pp
Interpreter Pattern.
Software Design Lecture : 39.
CSE 143 Lecture 21 Advanced List Implementation
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

Behavioral Patterns Part-I introduction UNIT-VI S. No TOPIC PPT Slides Behavioral Patterns Part-I introduction UNIT-VI 1 Chain of Responsibility L1 2 – 3 2 Command L2 4 – 9 3 interpreter L3 10 – 12 4 Iterator L4 13 – 17 5 Reusable points in Behavioral Patterns L5 18 – 21 6 (Intent, Motivation, Also Known As ……………) L6 22 – 24 7 Review Unit-VI L7 25 – 25 UNIT-IV

Chain of Responsibility Decouple sender of a request from receiver Give more than one object a chance to handle Flexibility in assigning responsibility Often applied with Composite successor Client Handler ContextInterface() handleRequest() ConcreteHandler1 ConcreteHandler2 handleRequest() handleRequest() UNIT-IV

Chain of Responsibility (2) Example: handling events in a graphical hierarchy If interactor != null interactor.handle(event,this) else parent.handleEvent(event) 0..1 0..* 0..* Interactor Figure children handle(Event,Figure) handleEvent(Event) CompositeFigure parent UNIT-IV

Command: Encapsulating Control Flow Name: Command design pattern Problem description: Encapsulates requests so that they can be executed, undone, or queued independently of the request. Solution: A Command abstract class declares the interface supported by all ConcreteCommands. ConcreteCommands encapsulate a service to be applied to a Receiver. The Client creates ConcreteCommands and binds them to specific Receivers. The Invoker actually executes a command. UNIT-IV

Command: Class Diagram invokes Command execute() Invoker ConcreteCommand1 execute() <<binds>> Receiver ConcreteCommand2 execute() UNIT-IV

Command: Class Diagram for Match invokes Move play() replay() Match Game1Move play() replay() <<binds>> GameBoard Game1Move play() replay() UNIT-IV

Command: Consequences L2 Consequences: The object of the command (Receiver) and the algorithm of the command (ConcreteCommand) are decoupled. Invoker is shielded from specific commands. ConcreteCommands are objects. They can be created and stored. New ConcreteCommands can be added without changing existing code. UNIT-IV

Command You have commands that need to be L2 Command You have commands that need to be executed, undone, or queued Command design pattern separates Receiver from Invoker from Commands All commands derive from Command and implement do(), undo(), and redo() UNIT-IV

Command Design Pattern L2 Separates command invoker and receiver UNIT-IV

Pattern: Interpreter Intent: Given a language, interpret sentences Participants: Expressions, Context, Client Implementation: A class for each expression type An Interpret method on each class A class and object to store the global state (context) No support for the parsing process (Assumes strings have been parsed into exp trees) UNIT-IV

Pattern: Interpreter with Macros L3 Pattern: Interpreter with Macros Example: Definite Clause Grammars A language for writing parsers/interpreters Macros make it look like (almost) standard BNF Command(move(D)) -> “go”, Direction(D). Built-in to Prolog; easy to implement in Dylan, Lisp Does parsing as well as interpretation Builds tree structure only as needed (Or, can automatically build complete trees) May or may not use expression classes UNIT-IV

Method Combination Build a method from components in different classes Primary methods: the “normal” methods; choose the most specific one Before/After methods: guaranteed to run; No possibility of forgetting to call super Can be used to implement Active Value pattern Around methods: wrap around everything; Used to add tracing information, etc. Is added complexity worth it? Common Lisp: Yes; Most languages: No UNIT-IV

L4 Iterator pattern iterator: an object that provides a standard way to examine all elements of any collection uniform interface for traversing many different data structures without exposing their implementations supports concurrent iteration and element removal removes need to know about internal structure of collection or different methods to access data from different collections UNIT-IV

objects that traverse collections Pattern: Iterator objects that traverse collections UNIT-IV

Iterator interfaces in Java L4 public interface java.util.Iterator { public boolean hasNext(); public Object next(); public void remove(); } public interface java.util.Collection { ... // List, Set extend Collection public Iterator iterator(); public interface java.util.Map { ... public Set keySet(); // keys,values are Collections public Collection values(); // (can call iterator() on them) UNIT-IV

L4 Iterators in Java all Java collections have a method iterator that returns an iterator for the elements of the collection can be used to look through the elements of any kind of collection (an alternative to for loop) List list = new ArrayList(); ... add some elements ... for (Iterator itr = list.iterator(); itr.hasNext()) { BankAccount ba = (BankAccount)itr.next(); System.out.println(ba); } set.iterator() map.keySet().iterator() map.values().iterator() UNIT-IV

Adding your own Iterators L4 Adding your own Iterators when implementing your own collections, it can be very convenient to use Iterators discouraged (has nonstandard interface): public class PlayerList { public int getNumPlayers() { ... } public boolean empty() { ... } public Player getPlayer(int n) { ... } } preferred: public Iterator iterator() { ... } public int size() { ... } public boolean isEmpty() { ... } UNIT-IV

Command: Encapsulating Control Flow Name: Command design pattern Problem description: Encapsulates requests so that they can be executed, undone, or queued independently of the request. Solution: A Command abstract class declares the interface supported by all ConcreteCommands. ConcreteCommands encapsulate a service to be applied to a Receiver. The Client creates ConcreteCommands and binds them to specific Receivers. The Invoker actually executes a command. UNIT-IV

Command: Class Diagram invokes Command execute() Invoker ConcreteCommand1 execute() <<binds>> Receiver ConcreteCommand2 execute() UNIT-IV

Command: Class Diagram for Match invokes Move play() replay() Match Game1Move play() replay() <<binds>> GameBoard Game1Move play() replay() UNIT-IV

Command: Consequences L5 Consequences: The object of the command (Receiver) and the algorithm of the command (ConcreteCommand) are decoupled. Invoker is shielded from specific commands. ConcreteCommands are objects. They can be created and stored. New ConcreteCommands can be added without changing existing code. UNIT-IV

Pattern: Interpreter Intent: Given a language, interpret sentences Participants: Expressions, Context, Client Implementation: A class for each expression type An Interpret method on each class A class and object to store the global state (context) No support for the parsing process (Assumes strings have been parsed into exp trees) UNIT-IV

Pattern: Interpreter with Macros L6 Pattern: Interpreter with Macros Example: Definite Clause Grammars A language for writing parsers/interpreters Macros make it look like (almost) standard BNF Command(move(D)) -> “go”, Direction(D). Built-in to Prolog; easy to implement in Dylan, Lisp Does parsing as well as interpretation Builds tree structure only as needed (Or, can automatically build complete trees) May or may not use expression classes UNIT-IV

Method Combination Build a method from components in different classes Primary methods: the “normal” methods; choose the most specific one Before/After methods: guaranteed to run; No possibility of forgetting to call super Can be used to implement Active Value pattern Around methods: wrap around everything; Used to add tracing information, etc. Is added complexity worth it? Common Lisp: Yes; Most languages: No UNIT-IV

References Information about Design Patterns: L7 Information about Design Patterns: http://msdn.microsoft.com/library/en-us/dnpag/html/intpatt.asp http://www.patternshare.org/ http://msdn.microsoft.com/architecture/ http://msdn.microsoft.com/practices/ http://www.dofactory.com/Patterns/Patterns.aspx http://hillside.net/ Contact: alex@stonebroom.com Slides & code: http://www.daveandal.net/download/ Article: http://www.daveandal.net/articles/ UNIT-IV