7 Patterns-442-12-by6

G by something like factory method pattern and dynamic

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: d) Pattern 5. Disadvantage: Proliferation of methods O(mn), where m = |concrete elements| and n = |concrete visitors| 6. Disadvantage: (Possible) breaking of encapsulation in element classes Similar problem to Iterator pattern 100 Visitor pattern (cont Double dispatching: d) Pattern Visitor pattern (cont Plausible answers: d) Pattern Receiver of arithmetic operation (numeric instance) sends message encoding both its class and operator invoked to original argument Example: Arithmetic message: 3 + 5.5 Integer addition in Smalltalk : + arg arg sumFromInteger: self. Next message: 5.5 sumFromInteger: 3 Method sumFromInteger deGined in class Float converts argument (e.g., 3) to Gloating point and does addition Advantage: Fast!!! 101 Implementation issue: Who is responsible for traversing object structure? 1. The visitor does the traversal Disadvantage: Must replicate traversal code in each concrete visitor object 2. The object structure does the traversal DeGine operation that performs recursive traversal while applying visitor to each structure component 3. An iterator does the traversal Use iterator, then send message to visitor with current element as argument Example: do: method of Smalltalk 102 Observer pattern Name Observer (aka Publish-Subscribe, Dependents) Applicability Pattern Observer pattern (cont Problem d) Pattern Guarantee that state of dependents is always consistent with state of master Solution structure DeGine four kinds of objects: Abstract subject - Store and maintain list of dependents; notify objects in list when master changes Abstract observer - DeGine protocol for updating dependents Concrete subject - Manage data relevant to dependents; notify dependents when master changes 103 When master object changes, group of dependent objects must be updated Example: In a system with a GUI, separate classes with logical information from classes that handle user interaction The master does not know number and identity of dependents Low coupling between master and dependents Concrete observers - Get new subject state upon receiving 104 update message Pattern Subject Add(observer) Remove(observer) Notify() list Pattern Subject! Observer1! SetState() Observer Update() Observer2! ConcreteSubject SetState(...) GetState() subjectState ConcreteObserver Update() observerState Structure of Observer pattern Notify() Update() GetState() I...
View Full Document

This note was uploaded on 02/23/2012 for the course COMP 553 taught by Professor Ajay during the Spring '12 term at Ill. Chicago.

Ask a homework question - tutors are online