Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Behavioral Design Patterns

Similar presentations


Presentation on theme: "Chapter 9 Behavioral Design Patterns"— Presentation transcript:

1 Chapter 9 Behavioral Design Patterns

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

3 Iterator Interface Iterator { // Iterator "points" to first item:
void first(); // true if iterator "points" past the last item: boolean isDone(); // Causes the iterator to point to its next item: void next(); // Return the item pointed to by the iterator: Object currentItem(); }

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

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

6 Iterator Class Model Client Iterator Aggregate first() next()
isDone() currentItem() Aggregate createIterator() Append() Remove() ConcreteAggregate createIterator() ConcreteIterator Return new ConcreteIterator();

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

8 The Mediator Class Model
Colleague ConcreteMediator ConcreteColleague1 ConcreteColleague2

9 Mediator Sequence Diagrams
1. Initiation by ConcreteMediator :ConcreteMediator :ConcreteColleague1 :ConcreteColleague2 :Mediator doPart1() doPart2() mediate() doSome1() doSome2() 2. Initiation on a ConcreteColleague

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

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

12 Observer Design Pattern
Client part Server part Client of this system 1 Source notify() 1..n Observer update() observers 2 for each of observers: o.update();

13 Observer Design Pattern
Source notify() Observer update() Client 1 1..n 2 for all Observer’s o: o.update(); ConcreteObserver observerState update() ConcreteSource state 3

14 Observer in the Java API
Observable notifyObservers() Observer update( Observable, Object ) MyConcreteObserver observerState update(…) MyObservable subject Key: Java API Class Developer Class

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

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

17 The Command Design Pattern
Client Command execute() replaced Action1Command execute() Target1 action1() Target2 action2() Action2Command execute()

18 Command: Example MenuItem Command CutCommand Document CopyCommand
handleClick() command Command execute() document.cut() command.execute() CutCommand execute() document Document cut() copy() document.copy() CopyCommand execute() document

19 Sequence Diagram for Command Example
execute() :MenuItem handleClick() command :Command :CopyCommand doc :Document copy()

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

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


Download ppt "Chapter 9 Behavioral Design Patterns"

Similar presentations


Ads by Google