Behavioral Design Patterns May 5, 2015May 5, 2015May 5, 2015.

Slides:



Advertisements
Similar presentations
Linked Lists Linear collections.
Advertisements

COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
 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.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-1 PS95&96-MEF-L15-1 Dr. M.E. Fayad Creationa l.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
SWE 316: Software Design and Architecture Objectives Lecture # 15 Behavioral Design Patterns SWE 316: Software Design and Architecture  To learn the behavioral.
Chapter 9: The Iterator Pattern
Behavioral Design Patterns October 19, 2015October 19, 2015October 19, 2015.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Lexi case study (Part 2) Presentation by Matt Deckard.
Information and Computer Sciences University of Hawaii, Manoa
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Chapter 5 Linked Lists II
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
© SERG Software Design (OOD Patterns) Pg 1 Object-Oriented Design Patterns Topics in Object-Oriented Design Patterns Material drawn from [Gamma95,Coplien95]
CS 210 Iterator Pattern October 31 st, Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
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.
Iterator Pattern. Traversing two different collections  When bringing two previously developed objects together, it can be difficult to change an implementation.
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.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Review of last class. Design patterns CreationalStructuralBehavioral Abstract Factory Builder Factory Singleton etc. Adapter Bridge Composite Decorator.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
1 CS162: Introduction to Computer Science II Abstract Data Types.
Observer Pattern Context:
Linked Lists Chapter 5 (continued)
Data Structures Lakshmish Ramaswamy.
Foundations of Software Engineering Patterns - Introduction
Iterator Design Pattern
Design Patterns - A few examples
Programming in Java Lecture 11: ArrayList
(Java Collections Framework) AbstractSequentialList
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Chapter 9 Behavioral Design Patterns
Observer Pattern 1.
Behavioral Patterns Part-I introduction UNIT-VI
The iterator and memento patterns
Computer Science and Engineering
JCF Collection classes and interfaces
Linked Lists Chapter 5 (continued)
Software Design Lecture : 39.
TCSS 143, Autumn 2004 Lecture Notes
CSE 143 Lecture 21 Advanced List Implementation
Java Generics & Iterators
Presentation transcript:

Behavioral Design Patterns May 5, 2015May 5, 2015May 5, 2015

Iterator Design Purpose Design Purpose –Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation Design Pattern Summary Design Pattern Summary –Encapsulate the iteration in a class pointing (in effect) to an element of the aggregate

Iterator Interface Iterator { // move to first item: void first(); // if it is at last item: boolean isDone(); // move point to next item: void next(); // Return the current: Object currentItem(); }

Using Iterator /* To perform desiredOperation() on items in the container according to the iteration (order) i: */ Iterator i = IteratorObject; for(i.first(); !i.isDone(); i.next()) desiredOperation(i.currentItem());

Iterator Sample Code // Suppose that we have iterators for forward and // backward order: we can re-use print_employees() List employees = new List(); ForwardListIterator fwd = employees.createForwardIterator(); ReverseListIterator bckwd = employees.createBackwardIterator(); // print from front to back client.print_employees(fwd); // print from back to front client.print_employees(bckwd);

Using Iterator Class Client extends … { print_employees(Iterator it) { for(it.first(); !it.isDone(); it.next()) { print(i.currentItem()); } // other operations }

Iterator Class Model ClientIterator first() next() isDone() currentItem() Container createIterator() append() remove() ConcreteIterator return new ConcreteIterator(this) ConcreteContainer createIterator()

Iterator - Question What is the difference between the two clients Class Client1 { void operation( Container c) { Iterator it = c.createIterator(); for (it.first(); it.isDone(); it.next()) { Object item = it.currentItem(); // process item } } Class Client2 { void operation( Iterator it) { for (it.first(); it.isDone(); it.next()) { Object item = it.currentItem(); // process item } }

Iterator - Question Client1 IteratorContainer Client2 Client1 is dependent upon two interfaces: Iterator and Container Client2 is dependent upon only one interface: Iterator

Iterator in Java - 1 Interface Iterator { // check if there are more elements. boolean hasNext();hasNext // Returns the next element in the iteration. E next() // Removes from the underlying collection next // the last element returned by the iterator // (optional operation). // (optional operation). void remove(); remove }

Iterator in Java - 2 import java.util.Iterator; public class ArrayListViaListWithIterator { private Node head, tail; private int count; public ArrayListViaLinkedListWithInnerIterator() { head = null; tail = null; count = 0; } public Iterator iterator() { return new ALIterator(); } public void add(int i, E e) { … } public E get(int i) { … } public boolean isEmpty() { … } public E remove(int i) { … } public E set(int i, E e) { … } public int size() { … }

Iterator in Java - 3 private class Node { private E item; private Node next; Node(E i, Node n) { … } public E getItem() { … } public void setItem(E item) {{ … } public Node getNext() { … } public void setNext(Node next) { … } } // end of Node

Iterator in Java - 4 private class ALIterator implements Iterator { private Node cursor; public AIIterator() { cursor = head; } public boolean hasNext() { return cursor != null; } public E next() { Node tmp = cursor; cursor = cursor.getNext(); return tmp.getItem(); } public void remove() { throw new RuntimeException("not supported"); } } // end of Iterator } // end of ArrayList

Observer Design Purpose Design Purpose –Arrange for a set of objects to be affected by a single object. Design Pattern Summary Design Pattern Summary –The single object aggregates the set, calling a method with a fixed name on each member.

Observer - Structure 1..n for o: observers o.update(this); observers Subject Attach(Observer) Detach(Observer) Notify() Observer Update(Subject) ConcreteSubject state getState() setState() ConcreteObserver observerState Update(Subject s) // change state Notify(); … s.getState(); …

Observer: Sequence diagram ClientO1:Observer:SubjectO2: Observer setState() notify() getState() update()

Observer in Java Observable notifyObservers() attach(Observer) detach(Observer) Observer update(Observable, Object ) MyObservable subjectState MyConcreteObserver observerState update(…) subject

Observer : Key Concept -- to keep a set of objects up to date with the state of a designated object.

Mediator Design Purpose Design Purpose –Avoid references between dependent objects. Design Pattern Summary Design Pattern Summary –Capture mutual behavior in a separate class.

Mediator - Model MediatorColleague ConcreteMediator Colleague_BColleague_A

Mediator Sequence Diagram ClientA:Colleague_A:Mediator B: Colleague_B request() takeAction_1() mediate() :MediatorC: Colleague_A takeAction_2() takeAction_3()

Key Concept: Mediator -- to capture mutual behavior without direct dependency.

Command Design Purpose Design Purpose –Increase flexibility in calling for a service e.g., allow undo-able operations. Design Pattern Summary Design Pattern Summary –Capture operations as classes.

Command - Structure Client Invoker +request() ConcreteCommand execte() State Command execute(); receiver.action() receiver Receiver action()

A B: A composed in B Command - Example Cut:Button Clicked() CopyCommand execte() Command execute(); doc.copy() doc Document copy() cut() command command.execute() CutCommand execte() doc doc.cut() Copy:Button Clicked() command.execute() command Cut:MenuItem Clicked() command.execute()

Command – Sequence Diagram Cut:ButtonCopyCommandCommand DocumentCutCommandCopy:ButtonCut:MenuItem selected() execute() cut() clicked() cut() execute() clicked() copy() execute()

Key Concept - Command -- to avoid calling a method directly (e.g., so as to record or intercept it).

Iterator visits members of a collection Iterator visits members of a collection Mediator captures behavior among peer objects Mediator captures behavior among peer objects Observer updates objects affected by a single object Observer updates objects affected by a single object Command captures function flexibly (e.g. undo-able) Command captures function flexibly (e.g. undo-able) Summary