Download presentation
Presentation is loading. Please wait.
Published byJamie Wilbanks Modified over 9 years ago
1
Behavioral Design Patterns May 5, 2015May 5, 2015May 5, 2015
2
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
3
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(); }
4
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());
5
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);
6
Using Iterator Class Client extends … { print_employees(Iterator it) { for(it.first(); !it.isDone(); it.next()) { print(i.currentItem()); } // other operations }
7
Iterator Class Model ClientIterator first() next() isDone() currentItem() Container createIterator() append() remove() ConcreteIterator return new ConcreteIterator(this) ConcreteContainer createIterator()
8
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 } }
9
Iterator - Question Client1 IteratorContainer Client2 Client1 is dependent upon two interfaces: Iterator and Container Client2 is dependent upon only one interface: Iterator
10
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 }
11
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() { … }
12
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
13
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
14
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.
15
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(); …
16
Observer: Sequence diagram ClientO1:Observer:SubjectO2: Observer setState() notify() getState() update()
17
Observer in Java Observable notifyObservers() attach(Observer) detach(Observer) Observer update(Observable, Object ) MyObservable subjectState MyConcreteObserver observerState update(…) subject
18
Observer : Key Concept -- to keep a set of objects up to date with the state of a designated object.
19
Mediator Design Purpose Design Purpose –Avoid references between dependent objects. Design Pattern Summary Design Pattern Summary –Capture mutual behavior in a separate class.
20
Mediator - Model MediatorColleague ConcreteMediator Colleague_BColleague_A
21
Mediator Sequence Diagram ClientA:Colleague_A:Mediator B: Colleague_B request() takeAction_1() mediate() :MediatorC: Colleague_A takeAction_2() takeAction_3()
22
Key Concept: Mediator -- to capture mutual behavior without direct dependency.
23
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.
24
Command - Structure Client Invoker +request() ConcreteCommand execte() State Command execute(); receiver.action() receiver Receiver action()
25
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()
26
Command – Sequence Diagram Cut:ButtonCopyCommandCommand DocumentCutCommandCopy:ButtonCut:MenuItem selected() execute() cut() clicked() cut() execute() clicked() copy() execute()
27
Key Concept - Command -- to avoid calling a method directly (e.g., so as to record or intercept it).
28
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.