02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use.
 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.
Chapter 7 – Object-Oriented Design
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,
Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
Copyright © Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-1 PS95&96-MEF-L16-1 Dr. M.E. Fayad Creationa l.
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.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
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.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
Case Studies on Design Patterns Design Refinements Examples.
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
CS 4240: More Design Patterns Readings: Chap. 9. Let’s Recap Some Ideas and Strategies We’ll assume some design-planning is useful up-front Design with.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
CS 210 Introduction to Design Patterns September 7 th, 2006.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
Programming in C# Observer Design Pattern
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.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Strategy Design Patterns CS 590L - Sushil Puradkar.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
1 A Brief Introduction to Design Patterns Based on materials from Doug Schmidt 1.
Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
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.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Stéphane Ducasse 1 Strategy.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw.
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Strategy: A Behavioral Design Pattern
Design Patterns: Brief Examples
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Observer Design Pattern
Observer Design Pattern
Presented by Igor Ivković
Design Patterns - A few examples
Design pattern Lecture 9.
Strategy and Template Method Patterns, Single User Protection
Strategy Design Pattern
Introduction to Design Patterns
Informatics 122 Software Design II
Design Patterns (Gamma, Helm, Johnson, Vlissides)
Presented by Igor Ivković
Software Design Lecture : 40.
Software Design Lecture 11.
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008

Strategy Moshe Fresko Bar-Ilan University תשס"ו Design Patterns Course

Strategy Intent Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Motivation Many algorithms exist for breaking a stream of text into lines. Hard-coding them in client isn’t desirable since: Clients become more complex Non-used algorithms will be supported Difficult to add new algorithms We can avoid these problems by encapsulating different line-breaking algorithms in different classes. Each of these classes are called a strategy.

Strategy - Motivation

Strategy – Applicability Use Strategy pattern when Many different classes differ only in their behavior. You need different variants of an algorithm. An algorithm uses data that clients shouldn’t know about. A class uses multiple conditional statements.

Strategy – General Structure

Strategy – Participants Strategy Declares an interface to all supported algorithms ConcreteStrategy Implements the algorithm using the Strategy interface Context Is configured with a ConcreteStrategy object Maintains a reference to a Strategy object May define an interface that lets the Strategy access its data.

Strategy Collaborations Strategy and Context interact to implement the chosen Algorithm. Context may pass all data required to Strategy or alternatively it can pass a reference to itself. Clients usually create and pass a ConcreteStrategy object to the context, from a family of ConcreteStrategy objects. Consequences Families of related algorithsm. An alternative to subclassing Context. Strategies eliminate conditional statements. A choice of implementations. Time and space trade-offs. Clients must be aware of different strategies. Communication overhead between Strategy and Context. Increased number of objects. They may be shared.

Strategy – Implentation Issues Implementation Defining the Strategy and Context interfaces Context can pass data in parameters to Strategy (better decoupling) Context can pass itself as an argument to Strategy Strategy can keep Context Strategies as Template parameters In C++ templates can be used to configure a class with a strategy. template void sort(Iterator _First, Iterator _Last ); template void sort(Iterator _First, Iterator _Last, Pr _Comp ); Making Strategy objects optional

Strategy – Sample Code import java.util.* ; class A { public final int i ; A(int i) { this.i = i ; } } class Ascending implements Comparator { public int compare(Object o1, Object o2) { A a1=(A)o1, a2=(A)o2 ; if (a1.i<a2.i) return -1 ; if (a1.i>a2.i) return +1 ; return 0 ; } } class Descending implements Comparator { public int compare(Object o1, Object o2) { A a1=(A)o1, a2=(A)o2 ; if (a1.i<a2.i) return +1 ; if (a1.i>a2.i) return -1 ; return 0 ; } }

Strategy – Sample Code public class S { public static void main(String[] args) { A[] asc = { new A(1), new A(5), new A(3), new A(2) } ; A[] dsc = (A[]) asc.clone() ; Arrays.sort(asc,new Ascending()) ; Arrays.sort(dsc,new Descending()) ; System.out.println("Ascending : "+arrStr(asc)) ; System.out.println("Descending: "+arrStr(dsc)) ; } static String arrStr(A[] a) { String s = "[" + a[0].i ; for (int i=1;i<a.length;++i) s += ","+a[i].i ; s+="]" ; return s ; } // Output : Ascending : [1,2,3,5] //Descending: [5,3,2,1]

Strategy – Examples class TreeSet CTOR: TreeSet () : Default comparator. Contained objects must implement Comparable interface CTOR: TreeSet (Comparator) class JPanel CTOR: JPanel () : With default layout manager CTOR: JPanel ( LayoutManager ) C++ STL: class map template < class Key, class Type, class Traits = less, class Allocator=allocator > > class map { … }

Observer Moshe Fresko Bar-Ilan University תשס"ו Design Patterns Course

Observer Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updates automatically. Motivation Many UI toolkits separate presentation from application data. Classes defining presentation and data can be reused independently. Spreadsheet object and bar-chart object can depict information in the same application data. They must be notified of any data change. The Observer defines how to establish this relationship between “Subject” and “Observer”. The interaction is called “Publish-Subscribe”

Observer - Motivation

Observer – Applicability Use Observer pattern when An abstraction has two aspects, one dependent on the other and encapsulating these aspects in different objects lets you vary and reuse them independently. A change in an object requires changes on others and you don’t know how many objects to be changed. An object should be able to notify others without knowing them.

Observer – General Structure

Observer – Participants Subject Knows its observers. Observer Defines an updating interface for objects that should be notified of changes in a subject. ConcreteObject Stores state of interest to ConcreteObserver objects. Sends a notification to its Observers when its state changes. ConcreteObserver Maintains a reference to ConcreteSubject Store state that should be consistent with the subject’s. Implements the Observer updating interface to keep its state consistent.

Observer Collaborations ConcreteSubject notifies its observers whenever a change occurs that could make its observers' state inconsistent with its own After being informed of a change in the concrete subject, a ConcreteObserver object may query the subject for information.

Observer Consequences Abstract coupling between Subject and Observer Support for broadcast communication Unexpected updates

Observer – Implementation Mapping subjects to their observers Observing more then one subject Who triggers the update? Subject Client Dangling references to deleted Subjects. Making sure Subject state is self-consistent. Avoiding observer-specific update protocols. Push model Pull model Specifying modifications of interest explicitly.