Algorithm Programming 2 89-211 Behavioral Design Patterns Bar-Ilan University 2005-2006 תשס " ו by Moshe Fresko.

Slides:



Advertisements
Similar presentations
Chain of Responsibility Pattern Gof pp Yuyang Chen.
Advertisements

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.
Design Patterns David Talby. This Lecture Representing other objects Proxy, Adapter, Façade Re-routing method calls Chain of Responsibility Coding partial.
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.
(A Completely Colorless Powerpoint Presentation of) The Interpreter Pattern David Witonsky.
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.
Zaki Alasadi Supervisor:Dr noorhosseini.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns Examples in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
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.
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.
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.
Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,
SWE 316: Software Design and Architecture Objectives Lecture # 15 Behavioral Design Patterns SWE 316: Software Design and Architecture  To learn the behavioral.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Pattern Oriented Design Chain of Responsibility From:Design Patterns Gamma. Johnson Helm. Vlissides (GoF) Present: F 楊汝康 R 徐德皓 R 高稚翔.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
02 - Behavioral Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Chain of Responsibility Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Computing IV Singleton Pattern Xinwen Fu.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
ECE450S – Software Engineering II
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
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 -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.
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.
Chain of Responsibility Behavioral Pattern. Defination Avoid coupling between the sender and receiver by giving more than one object a chance to handle.
Chain of Responsibility A graphical user interface and a user that needs help; a security system with multiple sensors; a banking automated coin storage.
Design Patterns Introduction
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
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.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The State 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.
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.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Design Patterns Behavioral Patterns. Chain of Responsibility Avoid coupling the sender of a request to its receiver by giving more than one object a chance.
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
1/23/2018 Design Patterns David Talby.
Chapter 10 Design Patterns.
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
Introduction to Design Patterns
Presentation by Julie Betlach 7/02/2009
Design Patterns Satya Puvvada Satya Puvvada.
Behavioral Patterns Part-I introduction UNIT-VI
Behavioral Design Pattern
Interpreter Pattern.
Informatics 122 Software Design II
Presentation transcript:

Algorithm Programming Behavioral Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko

Behavioral Patterns Behavioral Patterns are concerned with algorithms and the assignment of responsibilities between objects. Not only patterns of objects/classes but also patterns of communication between them. These patterns are: Template Method: An abstract definition of an algorithm. Interpreter: Represents a grammar as a class hierarchy and implements an interpreter as an operation on instances of these classes. Mediator: Provides the indirection needed for loose coupling. Chain of Responsibility: Lets you send requests to an object implicitly through a chain of candidate objects. Observer: Defines and Maintains dependency between objects. (MVC) Strategy: Encapsulates an algorithm in an Object. Command: Encapsulates a request in an Object. State: Encapsulates the states of an Object so that the Object can change its behavior when its state object is changes. Visitor: Encapsulates behavior that would otherwise be distributed across classes. Iterator: Abstracts the way you access and traverse objects in an aggregate.

Chain of Responsibility Intent Avoid coupling the sender of a request to its receiver by giving more then one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Motivation A context sensitive Help Facility. To organize help information from most specific to most general The object that provides the help isn’t known explicitly to the help initiator.

Chain of Responsibility (Example Structure)

Chain of Responsibility - Example

Chain of Responsibility Use Chain of Responsibility when … More then one object may handle a request, and the handler isn’t known a-priori. You want to issue a request to one of several objects without specifying the receiver explicitly. The set of objects that handle a request should be specified dynamically.

Chain of Responsibility General Structure

Chain of Responsibility Participants Handler (HelpHandler) Defines and interface for handling requests. Implements the successor link. ConcreteHandler (PrintButton, PrintDialog) Handles requests it is responsible for. Can access its successor. If it does not handle the request, then it forwards it to its successor. Client Initiates the request to a ConcreteHandler object on the chain.

Chain of Responsibility Consequences Reduced Coupling Flexibility in assigning responsibilities to Objects Receipt isn’t guaranteed. Implementation Implementing the successor chain can be done with a new implementation or use existing links. Representing Requests may be via an object.

Chain of Responsibility // Chain with a new implementation class HelpHandler { private HelpHandler successor = null ; HelpHandler(HelpHandler successor) { this.successor = successor ; } public void handleHelp() { if (successor!=null) { successor.handleHelp() ; } } // Any relationship (hierarchical or list) like is-a can be used for chaining

Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. Motivation If a particular kind of problem occurs often enough, then it might be worthwhile to express instances of the problem as sentences in a simple language. For example: Regular Expressions Document Retrieval Query

Interpreter Regular Expression Example A simple Regular Expression Grammar expression ::= literal | alternation | sequence | repetition | ‘(’ expression ‘)’ alternation ::= expression ‘|’ expression sequence ::= expression ‘&’ expression repetition ::= expression ‘*’ literal ::= ‘a’ | ‘b’ | ‘c’ | … { ‘a’ | ‘b’ | ‘c’ | … } *

Interpreter – Example Structure

Interpreter – Possible Structure

Interpreter Applicability Use Interpreter pattern when there is a language to interpret, and you can represent statements in the language as abstract syntax trees. The interpreter works well, when The grammar is simple Efficiency is not a critical concern

Interpreter – General Structure

Interpreter Participants AbstractExpression (RegularExpression) Declares an abstract interpret() operation TerminalExpression (LiteralExpression) Implements the interpret() operation for terminal symbols in the grammar NonterminalExpression (AlternationExpression, RepetitionExpression, SequenceExpression) Keeps AbstractExpression for each internal symbol it keeps Implements interpret() operation. Context Contains information that is global to the interpreter Client Builds the abstract syntax tree Calls the interpret() operation

Interpreter Consequences It is easy to change and extend the grammar Implementing the grammar is easy Complex grammars are hard to maintain Implementation Creating the abstract syntax tree Defining the interpret() operation Sharing terminal symbols with the Flyweight pattern

Interpreter – Example Grammar for Document Search expression ::= literal | alternation | intersection alternation ::= expression OR expression intersection ::= expression AND expression literal ::= ‘a’|‘b’|‘c’|… literal ::= literal ‘a’|‘b’|‘c’…

Interpreter – Example // Interface of a document collection interface DocCollection { int[] getDocNumbersForWord(String word) ; } // Interface for searching a document colletion interface DocSearch { int[] getDocNumbers(DocCollection d) ; } // A Literal search class Literal implements DocSearch { String word ; Literal(String word) { this.word = word ; } public int[] getDocNumbers(DocCollection d) { return d.getDocNumbersForWord(this.word) ; } }

Interpreter – Example // An alternation search class Alternation implements DocSearch { DocSearch search1=null ; DocSearch search2=null ; Alternation(DocSearch search1, DocSearch search2) { this.search1=search1; this.search2=search2; } public int[] getDocNumbers(DocCollection d) { return Utils.union(search1.getDocNumbers(d),search2.getDocNumbers(d)) ; } } // An intersection search class Intersection implements DocSearch { DocSearch search1=null ; DocSearch search2=null ; Intersection(DocSearch search1, DocSearch search2) { this.search1=search1; this.search2=search2; } public int[] getDocNumbers(DocCollection d) { return Utils.intersection(search1.getDocNumbers(d),search2.getDocNumbers(d)) ; } }

Interpreter – Example // The factory for creating the interpreted DocSearch pointer class DocSearchInterpreter { public static DocSearch interpret(String query) { String[] alt = query.split(" OR ") ; DocSearch d = interpretAnd(alt[0]) ; for (int i=1;i<alt.length;++i) d = new Alternation(d,interpretAnd(alt[i])) ; return d ; } private static DocSearch interpretAnd(String query) { String[] alt = query.split(" AND ") ; DocSearch d = interpretOne(alt[0]) ; for (int i=1;i<alt.length;++i) d = new Intersection(d,interpretOne(alt[i])) ; return d ; } private static DocSearch interpretOne(String query) { return new Literal(query.trim()) ; } }

Interpreter – Example // Some utilities for union and // intersection of sorted integer lists class Utils { public static int[] union(int[] a, int[] b) { List l = new ArrayList() ; int i=0, j=0; while (i<a.length && j<b.length) { if (a[i]==b[j]) { l.add(new Integer(a[i])) ; i++ ; j++ ; continue ; } else if (a[i]<b[j]) { l.add(new Integer(a[i])) ; i++ ; continue ; } else { l.add(new Integer(b[j])) ; j++ ; continue ; } } for (;i<a.length;++i) l.add(new Integer(a[i])) ; for (;j<b.length;++j) l.add(new Integer(b[j])) ; return arrayFromList(l) ; } public static int[] intersection(int[] a, int[] b) { List l = new ArrayList() ; int i=0, j=0; while (i<a.length && j<b.length) { if (a[i]==b[j]) { l.add(new Integer(a[i])) ; i++ ; j++ ; continue ; } else if (a[i]<b[j]) { i++ ; continue ; } else { j++ ; continue ; } } return arrayFromList(l) ; } private static int[] arrayFromList(List l) { int[] r=new int[l.size()] ; for (int i=0;i<r.length;++i) r[i]=((Integer)l.get(i)).intValue() ; return r ; } }