1 Pattern Games CS 236700: Software Design Winter 2004-2005/T9.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Chain of Responsibility Pattern Gof pp Yuyang Chen.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
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,
Zaki Alasadi Supervisor:Dr noorhosseini.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
1 (More) Pattern Games CS : Software Design Winter /T10.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Design Patterns II 1. Behavioral Pattern Visitor: intent and structure Represent an operation to be performed on the elements of an object structure.
Iterators CS 367 – Introduction to Data Structures.
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.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
CS 210 Introduction to Design Patterns September 28 th, 2006.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.
CS 350 – Software Design The Observer Pattern – Chapter 18 Let’s expand the case study to include new features: Sending a welcome letter to new customers.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
LinkedList Many slides from Horstmann modified by Dr V.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object-Oriented Programming Simple Stack Implementation.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
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.
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.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
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.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Java Programming Java Programming II Concurrent Programming: Threads ( II)
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.
Command Pattern Encapsulation Invocation. One size fits all.
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.
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.
Modern Programming Tools And Techniques-I
Sixth Lecture ArrayList Abstract Class and Interface
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
MPCS – Advanced java Programming
Delegates and Events 14: Delegates and Events
Observer Design Pattern
Observer Design Pattern
COP 3503 FALL 2012 Shayan Javed Lecture 8
Top Ten Words that Almost Rhyme with “Peas”
Linked Lists [AJ 15].
CSE 143 Lecture 2 Collections and ArrayIntList
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Behavioral Design Pattern
Introduction to Design Patterns
Presentation transcript:

1 Pattern Games CS : Software Design Winter /T9

2 Overview of the programs  Three similar programs Each one is actually a single collaboration The main goal: manage a Vector of integer values The programs are similar but not identical The differences are in specific details of the required behavior  We will use Design Patterns to address the various needs: Command Observer Chain-of-Responsibility

3 Participating classes  Client – the “application”. Has a constructor and a run() method Role: Client  PowerVector – Maintains a list of Objects Role: Invoker, Concrete Subject, Concrete Handler  SizeKeeper – Notified when an add/remove request happens Allows the client to obtain the size of the PowerVector object Role: Receiver, Concrete Observer  Additional classes in each program SizeCommand, IObserver, …

4 Skeleton of class PowerVector public class PowerVector { private Vector items_ = new Vector(); public PowerVector(...) {... } public Object get(int index) { return items_.get(index); } public void add(Object o) { items_.add(o); } public void remove() { items_.remove(items_.size() - 1); } public class PowerVector { private Vector items_ = new Vector(); public PowerVector(...) {... } public Object get(int index) { return items_.get(index); } public void add(Object o) { items_.add(o); } public void remove() { items_.remove(items_.size() - 1); }

5 Skeleton of class Client public class Client { private PowerVector vec_; public Client() {... } public void run() { // add 25 integers to vec_ (how?) // print size (how?) for(int i = 0; i < 3; ++i) vec_.remove(); // print size (how?) } public static void main(String[] args) { Client c = new Client(); c.run(); } public class Client { private PowerVector vec_; public Client() {... } public void run() { // add 25 integers to vec_ (how?) // print size (how?) for(int i = 0; i < 3; ++i) vec_.remove(); // print size (how?) } public static void main(String[] args) { Client c = new Client(); c.run(); }

6 1 st program  In this program, class PowerVector has no getSize() method  A SizeKeeper object will monitor the size of the vector  We will use the Command pattern to notify the SizeKeeper object of add/remove operations

7 Command: intent Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

8 SizeKeeper public class SizeKeeper { private int size_ = 0; public void incSize(int diff) { size_ += diff; } public int getSize() { return size_; } } public class SizeKeeper { private int size_ = 0; public void incSize(int diff) { size_ += diff; } public int getSize() { return size_; } }

9 ICommand, SizeCommand public interface ICommand { public void execute(); } public class SizeCommand implements ICommand { private int diff_; private SizeKeeper keeper_; public SizeCommand(SizeKeeper keeper, int diff) { diff_ = diff; keeper_ = keeper; } public void execute() { keeper_.incSize(diff_); } public interface ICommand { public void execute(); } public class SizeCommand implements ICommand { private int diff_; private SizeKeeper keeper_; public SizeCommand(SizeKeeper keeper, int diff) { diff_ = diff; keeper_ = keeper; } public void execute() { keeper_.incSize(diff_); }

10 PowerVector public class PowerVector { private Vector items_ = new Vector(); private ICommand on_add_; private ICommand on_remove_; public PowerVector(ICommand on_add, ICommand on_remove) { on_add_ = on_add; on_remove_ = on_remove; } public void add(Object o) { items_.add(o); on_add_.execute(); // add notification } public void remove() { items_.remove(items_.size() - 1); on_remove_.execute(); // remove notification } public class PowerVector { private Vector items_ = new Vector(); private ICommand on_add_; private ICommand on_remove_; public PowerVector(ICommand on_add, ICommand on_remove) { on_add_ = on_add; on_remove_ = on_remove; } public void add(Object o) { items_.add(o); on_add_.execute(); // add notification } public void remove() { items_.remove(items_.size() - 1); on_remove_.execute(); // remove notification }

11 Client public class Client { private PowerVector vec_; private SizeKeeper keeper_ = new SizeKeeper(); public Client() { ICommand onInc = new SizeCommand(keeper_, 1); ICommand onDec = new SizeCommand(keeper_, -1); vec_ = new PowerVector(onInc, onDec); } public void run() { for(int i = 0; i < 25; ++i) vec_.add(new Integer(i)); System.out.println("Size=" + keeper_.getSize()); for(int i = 0; i < 3; ++i) vec_.remove(); System.out.println("Size=" + keeper_.getSize()); } // main() omitted } public class Client { private PowerVector vec_; private SizeKeeper keeper_ = new SizeKeeper(); public Client() { ICommand onInc = new SizeCommand(keeper_, 1); ICommand onDec = new SizeCommand(keeper_, -1); vec_ = new PowerVector(onInc, onDec); } public void run() { for(int i = 0; i < 25; ++i) vec_.add(new Integer(i)); System.out.println("Size=" + keeper_.getSize()); for(int i = 0; i < 3; ++i) vec_.remove(); System.out.println("Size=" + keeper_.getSize()); } // main() omitted }

12 2 nd program  Now, class PowerVector provides a getSize() method SizeKeeper will use this method => Tight-coupling between SizeKeepr and PowerVector  SizeKeeper is an observer of PowerVector

13 Observer: intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

14 IObserver, SizeKeeper public interface IObserver { public void update(PowerVector subject); } public class SizeKeeper implements IObserver { private int size_; public SizeKeeper() { } public void update(PowerVector subject) { size_ = subject.getSize(); } public int getSize() { return size_; } public interface IObserver { public void update(PowerVector subject); } public class SizeKeeper implements IObserver { private int size_; public SizeKeeper() { } public void update(PowerVector subject) { size_ = subject.getSize(); } public int getSize() { return size_; }

15 PowerVector public class PowerVector { private Vector items_ = new Vector(); private LinkedList observers_ = new LinkedList(); public PowerVector() { } public int getSize() { return items_.size(); } public void addObserver(IObserver o) { observers_.add(o); } public void add(Object o) { items_.add(o); for(Iterator i = observers_.iterator(); i.hasNext(); ) ((IObserver) i.next()).update(this); } // remove() is similarly implemented } public class PowerVector { private Vector items_ = new Vector(); private LinkedList observers_ = new LinkedList(); public PowerVector() { } public int getSize() { return items_.size(); } public void addObserver(IObserver o) { observers_.add(o); } public void add(Object o) { items_.add(o); for(Iterator i = observers_.iterator(); i.hasNext(); ) ((IObserver) i.next()).update(this); } // remove() is similarly implemented }

16 Client public class Client { private PowerVector vec_ = new PowerVector(); private SizeKeeper keeper_ = new SizeKeeper(); public Client() { vec_.addObserver(keeper_); } public void run() { for(int i = 0; i < 25; ++i) vec_.add(new Integer(i)); System.out.println("Size=" + keeper_.getSize()); for(int i = 0; i < 3; ++i) vec_.remove(); System.out.println("Size=" + keeper_.getSize()); } // main() omitted } public class Client { private PowerVector vec_ = new PowerVector(); private SizeKeeper keeper_ = new SizeKeeper(); public Client() { vec_.addObserver(keeper_); } public void run() { for(int i = 0; i < 25; ++i) vec_.add(new Integer(i)); System.out.println("Size=" + keeper_.getSize()); for(int i = 0; i < 3; ++i) vec_.remove(); System.out.println("Size=" + keeper_.getSize()); } // main() omitted }

17 3 rd program  At this stage, we would like to control the values added to the PowerVector object  The Chain-Of-Responsibility allows screening of requests  Client class will invoke an “add element” request The request will propagate through a chain of request handlers The last handler is the PowerVector object itself

18 Chain-of-Reponsibility: intent Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

19 RequestHandler public class RequestHandler { private RequestHandler next_ = null; public void attach(RequestHandler other) { other.next_ = this.next_; this.next_ = other; } public final void invoke(Integer val) { if(this.handle(val)) return; if(next_ != null) next_.invoke(val); } public boolean handle(Integer val) { return false; } public class RequestHandler { private RequestHandler next_ = null; public void attach(RequestHandler other) { other.next_ = this.next_; this.next_ = other; } public final void invoke(Integer val) { if(this.handle(val)) return; if(next_ != null) next_.invoke(val); } public boolean handle(Integer val) { return false; }

20 EvenFilter, PrimeFilter public class EvenFilter extends RequestHandler { public boolean handle(Integer val) { return (val.intValue() % 2 == 0); } public class PrimeFilter extends RequestHandler { public boolean handle(Integer val) { int n = val.intValue(); for(int i = 2; i * i <= n; ++i) { if(n % i == 0) return false; } return true; } public class EvenFilter extends RequestHandler { public boolean handle(Integer val) { return (val.intValue() % 2 == 0); } public class PrimeFilter extends RequestHandler { public boolean handle(Integer val) { int n = val.intValue(); for(int i = 2; i * i <= n; ++i) { if(n % i == 0) return false; } return true; }

21 PowerVector public class PowerVector { private Vector items_ = new Vector(); public PowerVector() { } public Object get(int index) { return items_.get(index); } public void add(Object o) { items_.add(o); } public void remove() { items_.remove(items_.size() - 1); } public int getSize() { return items_.size(); } public class PowerVector { private Vector items_ = new Vector(); public PowerVector() { } public Object get(int index) { return items_.get(index); } public void add(Object o) { items_.add(o); } public void remove() { items_.remove(items_.size() - 1); } public int getSize() { return items_.size(); }  High degree of coherency in PowerVector  Is not concerned with notification issues

22 Client public class Client { private PowerVector vec_ = new PowerVector(); private RequestHandler chain_ = new RequestHandler(); public Client() { chain_.attach(new RequestHandler() { public boolean handle(Integer val) { vec_.add(val); return true; } }); chain_.attach(new EvenFilter()); chain_.attach(new PrimeFilter()); } public void run() { for(int i = 0; i < 25; ++i) chain_.invoke(new Integer(i)); System.out.println("Size=" + vec_.getSize()); for(int i = 0; i < 3; ++i) vec_.remove(); System.out.println("Size=" + vec_.getSize()); } public class Client { private PowerVector vec_ = new PowerVector(); private RequestHandler chain_ = new RequestHandler(); public Client() { chain_.attach(new RequestHandler() { public boolean handle(Integer val) { vec_.add(val); return true; } }); chain_.attach(new EvenFilter()); chain_.attach(new PrimeFilter()); } public void run() { for(int i = 0; i < 25; ++i) chain_.invoke(new Integer(i)); System.out.println("Size=" + vec_.getSize()); for(int i = 0; i < 3; ++i) vec_.remove(); System.out.println("Size=" + vec_.getSize()); }

23 Summary  The three patterns allow modeling of different situations Obviously, they can be used together to get a combined effect  Command: An invoker does not know who is the actual receiver An invoker does not know which method should be called  Observer: Several observers are all interested in a single object  Chain-of-Responsibility Several handlers are all interested in a single request The chain represents a single request A handler does not know who is the actual invoker