lecture 10 flyweight composite iterator program overview
play

Lecture 10: Flyweight, Composite, Iterator Program overview Date - PowerPoint PPT Presentation

Software Architecture Bertrand Meyer & Till Bay ETH Zurich, February-May 2008 Lecture 10: Flyweight, Composite, Iterator Program overview Date Topic Who? last week Introduction; A Basic Architecture Example Till 26. Feb. Modularity


  1. Software Architecture Bertrand Meyer & Till Bay ETH Zurich, February-May 2008 Lecture 10: Flyweight, Composite, Iterator

  2. Program overview Date Topic Who? last week Introduction; A Basic Architecture Example Till 26. Feb. Modularity and reusability; Abstract Data Types Till Jason, Andy 4. Mar. Project description and Delta Debugging 11. Mar. Till Patterns 1: observer + event library, componentization 18. Mar. Design by Contract Prof. Meyer 25. Mar. No course :-) 1. Apr. Till Patterns 2: visitor, strategy, state, chain of responsibility 8. Apr. Till Patterns 3: factory, builder, singleton Michela 15. Apr. Patterns 4: bridge, composite, decorator, facade Today Till Patterns 5: Wrap up 29. Apr. Bertrand Language constructs for mod. and info. hiding

  3. Program overview Date Topic Who? Martin 6. May Exception handling 13. May Concurrent programming Jason Everybody 20. May Project presentation 27. May Exam Everybody Exercises overview Date Topic 28. Feb. Abstract Data Types 3. Apr. Design by Contract 8. May Exception Handling Rest Project

  4. flyweight • Reuse existing objects by aliasing the objects during object creation. • Requires an object factory and a shared repository of objects. • Works if the object identity follows from the object value. Value semantics comerge AG

  5. coding flyweight class FLYWEIGHT_FACTORY feature -- Factory make_new_flyweight (a: SOME_ARG): FLYWEIGHT is -- Create a new flyweight, initialized with `a ʼ . do if flyweight_pool.has_index (a) then Result := flyweight_pool.item (a) else create Result.make (a) flyweight_pool.extend (Result, a) end end flyweight_pool: HASH_TABLE [FLYWEIGHT,SOME_ARG] is once ... end end 5 comerge inc.

  6. expanded types • “Expanded types”: Define classes with value semantics • Much improved through ECMA • No explicit instance creation necessary • No recursive data structures • No subtyping • No aliasing: copy semantics • Reference equality (=) becomes value equality (is_equal, ~) comerge AG

  7. flyweight vs. expanded • Expanded classes cover nearly all cases where you would have used flyweight for Java or .NET • Use flyweight only if you need one of the following: • Complex type hierarchy • Unbounded data • Recursive data structure with value semantics comerge AG

  8. flyweight examples Expanded Type Flyweight • characters • string labels • pairs • tokens • currency values • sets • enumeration • (some) units types comerge AG

  9. flyweight summary • Flyweight implements value semantics • Straight forward implementation • Use a once container as flyweight pool • Choose: • Flyweight: Needs hierarchy, unbounded data or recursive data structure • Otherwise use expanded type comerge AG

  10. composite • Tree structure for “whole/part” hierarchies • Abstract tree node type defined • Leaf nodes: • implement abstract tree node type • Non-leaf nodes: implement • implement abstract tree node type • have a number of attributes of tree node type • Nothing special for Eiffel 10 comerge inc.

  11. composite sub_node: NODE * NODE LEAF NON_LEAF 11 comerge inc.

  12. composite example deferred class EXPRESSION end 12 comerge inc.

  13. composite example class INTEGER_EXPRESSION inherit EXPRESSION feature -- Access value: INTEGER -- Representation of expression end 13 comerge inc.

  14. composite example class ADDITION_EXPRESSION inherit EXPRESSION feature -- Access left: EXPRESSION -- Left subexpression right: EXPRESSION -- Right subexpression invariant left_not_void: left /= Void right_not_void: right /= Void end 14 comerge inc.

  15. composite example class MULTIPLICATION_EXPRESSION inherit EXPRESSION feature -- Access left: EXPRESSION -- Left subexpression right: EXPRESSION -- Right subexpression invariant left_not_void: left /= Void right_not_void: right /= Void end 15 comerge inc.

  16. composite summary • Use composite pattern for structures that are (recursively) built from parts. • Don ʼ t over-engineer. • Don ʼ t put any recursive computation into the nodes. • Compute on “composite object structures” by using visitors (next pattern). 16 comerge inc.

  17. iterator • “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” • Four types of iterators: • Internal iterators • External iterators without bookkeeping-reference • External iterators with bookkeeping-reference • Higher-order procedures and functions 17 comerge inc.

  18. simple iterators deferred class LINEAR [G] feature -- Access NOT like Java: item: G is -- Current item hasNext() require next() not_off: not off deferred end feature -- Cursor movement forth is -- Move to next element. require not_off: not off deferred end feature -- Status off: BOOLEAN end 18 comerge inc.

  19. other features • Rollback to the start of the data structure: • start, ... • Bilinear movement: • back, finish, before, after, ... • Index and direct access: • count, index, i_th, goto, ... • Manipulate the underlying structure: • remove, put, replace, ... 19 comerge inc.

  20. internal iterators LINKED_LIST [G] first cursor_position LINKABLE [G] LINKABLE [G] LINKABLE [G] right right class LINKED_LIST [G] inherit LINEAR [G] comerge AG

  21. external iterators LINKED_LIST_ LINKED_LIST CURSOR [G] [G] first cursor_position LINKABLE [G] LINKABLE [G] LINKABLE [G] right right comerge AG

  22. external iterators LINKED_LIST_ LINKED_LIST CURSOR [G] [G] iterators first cursor_position LINKABLE [G] LINKABLE [G] LINKABLE [G] right right comerge AG

  23. higher-order feature -- Iterators do_all (c: PROCEDURE [ANY, TUPLE [G]]) -- Call `c ʼ with on all elements for_all (q: FUNCTION [ANY, TUPLE [G], BOOLEAN]): BOOLEAN -- Does `q ʼ yield true on all elements? there_exists (q: FUNCTION [ANY, TUPLE [G], BOOLEAN]): BOOLEAN -- Does `q ʼ yield true on all elements? 23 comerge inc.

  24. iterator summary • Integrated iterator: + easy to track and contract - dangerous side-effects, multiple iterators • External iterators without bookkeeping: + independent operations, less side-effects - Access to “dead nodes” • External iterators with bookkeeping: + No dead nodes, well-defined behavior - potential memory leak • Integrated iterator: + powerful, very expressive, safe - limited, no change of underlying container comerge AG

  25. End of lecture 10

Recommend


More recommend