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

lecture 10 flyweight composite iterator program overview
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Software Architecture Bertrand Meyer & Till Bay

ETH Zurich, February-May 2008

Lecture 10: Flyweight, Composite, Iterator

slide-2
SLIDE 2

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

  • 11. Mar.

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

Today

Patterns 5: Wrap up

Till

  • 29. Apr.

Language constructs for mod. and info. hiding

Bertrand

slide-3
SLIDE 3

Program overview

Date Topic Who?

  • 6. May

Exception handling

Martin

  • 13. May

Concurrent programming

Jason

  • 20. May

Project presentation

Everybody

  • 27. May

Exam

Everybody

Date Topic

  • 28. Feb.

Abstract Data Types

  • 3. Apr.

Design by Contract

  • 8. May

Exception Handling Rest Project

Exercises overview

slide-4
SLIDE 4

comerge AG

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
  • bject value.

Value semantics

slide-5
SLIDE 5

comerge inc.

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

coding flyweight

5

slide-6
SLIDE 6

comerge AG

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, ~)

slide-7
SLIDE 7

comerge AG

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
slide-8
SLIDE 8

comerge AG

flyweight examples

Expanded Type Flyweight

  • characters
  • pairs
  • currency values
  • enumeration

types

  • string labels
  • tokens
  • sets
  • (some) units
slide-9
SLIDE 9

comerge AG

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
slide-10
SLIDE 10

comerge inc.

composite

10

  • 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
slide-11
SLIDE 11

comerge inc.

composite

11

LEAF * NODE NON_LEAF sub_node: NODE

slide-12
SLIDE 12

comerge inc.

deferred class EXPRESSION end

composite example

12

slide-13
SLIDE 13

comerge inc.

class INTEGER_EXPRESSION inherit EXPRESSION feature -- Access value: INTEGER

  • - Representation of expression

end

composite example

13

slide-14
SLIDE 14

comerge inc.

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

composite example

14

slide-15
SLIDE 15

comerge inc.

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

composite example

15

slide-16
SLIDE 16

comerge inc.

composite summary

16

  • 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).

slide-17
SLIDE 17

comerge inc.

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

slide-18
SLIDE 18

comerge inc.

deferred class LINEAR [G] feature -- Access item: G is

  • - Current item

require not_off: not off deferred end feature -- Cursor movement forth is

  • - Move to next element.

require not_off: not off deferred end feature -- Status

  • ff: BOOLEAN

end

simple iterators

18

NOT like Java: hasNext() next()

slide-19
SLIDE 19

comerge inc.

  • ther 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

slide-20
SLIDE 20

comerge AG

internal iterators

LINKED_LIST [G] LINKABLE [G] LINKABLE [G] LINKABLE [G] right cursor_position first right class LINKED_LIST [G] inherit LINEAR [G]

slide-21
SLIDE 21

comerge AG

external iterators

LINKED_LIST [G] LINKABLE [G] LINKABLE [G] LINKABLE [G] right cursor_position first right LINKED_LIST_ CURSOR [G]

slide-22
SLIDE 22

comerge AG

external iterators

LINKED_LIST [G] LINKABLE [G] LINKABLE [G] LINKABLE [G] right cursor_position first right LINKED_LIST_ CURSOR [G] iterators

slide-23
SLIDE 23

comerge inc.

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?

higher-order

23

slide-24
SLIDE 24

comerge AG

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
slide-25
SLIDE 25

End of lecture 10