Software Architecture Bertrand Meyer & Till Bay ETH Zurich, February-May 2008 Lecture 4: Observer Pattern, Event Library and Componentization Program overview Date Topic Who? last week Introduction; A Basic Architecture Example Till 26. Feb. Modularity and reusability; Abstract Data Types Till 4. Mar. Project description and Delta Debugging Jason, Andy Today Patterns 1: observer + event library, componentization Till 18. Mar. Design by Contract Prof. Meyer 25. Mar. No course :-) 1. Apr. Patterns 2: visitor, strategy, state, chain of responsibility Till 8. Apr. Patterns 3: factory, builder, singleton Till 15. Apr. Patterns 4: bridge, composite, decorator, facade Michela 22. Apr. Patterns 5: Wrap up Till 29. Apr. Language constructs for mod. and info. hiding Till Program overview Date Topic Who? 6. May Exception handling Martin 13. May Concurrent programming Jason 20. May Project presentation Everybody 27. May Exam Everybody Exercises overview Date Topic 28. Feb. Abstract Data Types 3. Apr. Design by Contract 8. May Exception Handling Rest Project
design patterns? [Design Patterns] are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context. Design Patterns, GOF hierarchy of reuse Library Bottleneck Language Wizard / Tool Design Pattern Documented Solution Communicated Solution ( CutʼnʼPaste ) Ad-hoc Solution reuse and quality Reuse Reuse increases quality Only by reuse, the extra effort of quality pays off Given enough eyeballs, all bugs are shallow (Eric Raymond) Quality increases reuse Understandable Trust gained by quality Quality
Some design patterns Creational Abstract Factory Builder Factory Method Prototype Singleton Structural Adapter Bridge Composite Decorator Façade Flyweight Proxy Behavioral Chain of Responsibility Command (undo/redo) Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor Observer Pattern Observers or subscribers or subject, or publisher A = 50% B = 30% C = 20% Observed observer pattern Loose coupling between caller and call target. Implement a publish-subscribe and MVC architecture.
observer pattern SUBJECT observers LIST[] CONCRETE_ SUBJECT CONCRETE_ observer example CLICKABLE observers LIST[] CLICK_ BUTTON ON_CLICK_ ACTION observer as library SUBJECT event_x EVENT_TYPE Library class target ( agent ) subscribers Language feature
EVENT_TYPE class EVENT_TYPE [G -> TUPLE] feature -- Subscription subscribers: LIST [ [ANY, G]] subscribe (an_action: [ANY, G]) -- Add `an_actionʼ to the subscription list. require not_void: an_action /= Void ensure subscribed: subscribers.has (an_action) feature -- Publish publish (arguments: G) -- Publish event with `argumentsʼ. require not_void: arguments /= Void events in MVC class DATABASE feature -- Slots remove_customer is class BUTTON feature -- Signals clicked: EVENT_TYPE [TUPLE []] gui_remove_button.clicked.subscribe (agent db.remove_customer) dropping arguments class DATABASE feature -- Slots remove_customer is TUPLE[] class BUTTON TUPLE[INTEGER] feature -- Signals clicked: EVENT_TYPE [TUPLE [INTEGER]] gui_remove_button.clicked.subscribe (agent db.remove_customer)
type system caveat 1 [ANY,TUPLE[INTEGER]] not [ANY,TUPLE[ANY]] print (what:any) clicked: EVENT_TYPE [TUPLE [INTEGER]] clicked.subscribe (print (?)) Does not type check! type system caveat 2 [ANY,TUPLE[ANY]] print_integer (v: INTEGER) network.received: EVENT_TYPE [TUPLE [ANY]] [ANY,TUPLE[INTEGER]] received.subscribe (print_integer (?)) Compiles with type error (CAT call!) observer: summary Not needed as pattern Language powerful enough (agents, generics, tuple) to implement as library Available library classes: ACTION_SEQUENCE (used by Vision2) EVENT_TYPE (early implementation at ETH) Not yet in GOBO GOBO did support compilers w. o. agents will change in the future
End of lecture 4