Trusted Components Reuse, Contracts and Patterns Prof. Dr. Bertrand - - PowerPoint PPT Presentation

trusted components
SMART_READER_LITE
LIVE PREVIEW

Trusted Components Reuse, Contracts and Patterns Prof. Dr. Bertrand - - PowerPoint PPT Presentation

1 Last update: 19 October 2004 Trusted Components Reuse, Contracts and Patterns Prof. Dr. Bertrand Meyer Dr. Karine Arnout Trusted Components: Reuse, Contracts and Patterns - Lecture 17 Chair of Softw are Engineering 2 Lecture 17:


slide-1
SLIDE 1

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

1

Chair of Softw are Engineering

Last update: 19 October 2004

Trusted Components

Reuse, Contracts and Patterns

  • Prof. Dr. Bertrand Meyer
  • Dr. Karine Arnout
slide-2
SLIDE 2

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

2

Chair of Softw are Engineering

Lecture 17: Composite, Flyweight

slide-3
SLIDE 3

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

3

Chair of Softw are Engineering

Agenda for today

Composite pattern / Library Flyweight pattern / Library

slide-4
SLIDE 4

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

4

Chair of Softw are Engineering

Composite pattern: Intent

“Way to compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of

  • bjects uniformly.” [GoF, p 163]

Architecture of a typical application

slide-5
SLIDE 5

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

5

Chair of Softw are Engineering

Composite pattern

* COMPONENT + COMPOSITE + LEAF

i_th parts add remove has do_something* do_something+ do_something+

Transparency version

* COMPONENT + COMPOSITE + LEAF

i_th parts add remove has do_something* do_something+ do_something+

Safety version

slide-6
SLIDE 6

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

6

Chair of Softw are Engineering

Flaws of the approach

To make MY_COMPOSITE a composite of parts of type MY_LEAF, one must: Create a descendant MY_COMPONENT of class COMPONENT Make MY_COMPOSITE inherit from COMPOSITE Make MY_LEAF inherit from LEAF Redefine i_th to return an instance of MY_COMPONENT Genericity

“Natural to represent a composite as a generic class with a parent and a set of children” [Jézéquel 1999]

slide-7
SLIDE 7

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

7

Chair of Softw are Engineering

Composite Library

* COMPONENT [G] + COMPOSITE [G] + LEAF

item i_th parts add remove has … do_something* do_something+ do_something+

Transparency version

* COMPONENT [G] + COMPOSITE [G] + LEAF

item i_th parts add remove has do_something* do_something+ do_something+

Safety version

slide-8
SLIDE 8

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

8

Chair of Softw are Engineering

Composite Library, safety version (1/5)

deferred class COMPONENT [G] feature -- Basic operation do_something is

  • - Do something.

deferred end feature -- Status report is_composite: BOOLEAN is

  • - Is component a composite?

do Result := False end end

Mechanism enabling componentization: Unconstrained genericity

slide-9
SLIDE 9

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

9

Chair of Softw are Engineering

Composite Library, safety version (2/5)

class COMPOSITE [G] inherit COMPONENT [G] redefine is_composite end create make, make_from_components feature {NONE} -- Initialization make is

  • - Initialize component parts.

do create parts.make end

slide-10
SLIDE 10

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

10

Chair of Softw are Engineering

Composite Library, safety version (3/5)

make_from_components (some_components: like parts) is

  • - Set parts to some_components.

require some_components_not_void: some_components /= Void no_void_component: not some_components.has (Void) do parts := some_components ensure parts_set: parts = some_components end feature -- Status report is_composite: BOOLEAN is

  • - Is component a composite?

do Result := True end

slide-11
SLIDE 11

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

11

Chair of Softw are Engineering

Composite Library, safety version (4/5)

feature -- Basic operation do_something is

  • - Do something.

do from parts.start until parts.after loop parts.item.do_something parts.forth end end feature -- Access item: COMPONENT [G] is

  • - Current part of composite

do Result := parts.item ensure definition: Result = parts.item component_not_void: Result /= Void end

slide-12
SLIDE 12

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

12

Chair of Softw are Engineering

Composite Library, safety version (5/5)

feature -- Others

  • - Access: i_th, first, last
  • - Status report: has, is_empty, off, after, before
  • - Measurement: count
  • - Element change: add
  • - Removal: remove
  • - Cursor movement: start, forth, finish, back

feature {NONE} -- Implementation parts: LINKED_LIST [like item]

  • - Component parts
  • - (which are themselves components)

invariant is_composite: is_composite parts_not_void: parts /= Void no_void_part: not parts.has (Void) end

slide-13
SLIDE 13

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

13

Chair of Softw are Engineering

Composite pattern vs. Composite Library

BORROWABLE COMPOSITE_ BORROWABLE BOOK ENCYCLOPEDIA COMPOSITE

slide-14
SLIDE 14

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

14

Chair of Softw are Engineering

Composite: Componentization outcome

  • Completeness

All cases of the Composite pattern

  • Usefulness

Reusable Easy-to-use

  • Faithfulness

Similar to a traditional implementation of Composite (with genericity)

  • Type-safety

Type-safe (unconstrained genericity, assertions)

  • Performance

Same order as the Composite pattern

  • Extended applicability

No more cases

slide-15
SLIDE 15

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

15

Chair of Softw are Engineering

Agenda for today

Composite pattern / Library Flyweight pattern / Library

slide-16
SLIDE 16

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

16

Chair of Softw are Engineering

Flyweight pattern: Description

Intent: “Use sharing to support large numbers of fine-grained objects efficiently.” [GoF, p 195] Motivation: see next

slide-17
SLIDE 17

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

17

Chair of Softw are Engineering

Without the Flyweight pattern (1/2)

class CLIENT ... feature -- Basic operation draw_lines is

  • - Draw some lines in color.

local line1, line2: LINE red: INTEGER do ... create line1.make (red, 100, 200) line1.draw create line2.make (red, 100, 400) line2.draw ... end ... end

Creates one LINE object for each line to draw

slide-18
SLIDE 18

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

18

Chair of Softw are Engineering

Without the Flyweight pattern (2/2)

class interface LINE create make feature -- Initialization make (a_color, x, y: INTEGER)

  • - Set color to a_color, x as x_position, and y as y_position.

ensure color_set: color = a_color x_set: x_position = x y_set: y_position = y feature -- Access color: INTEGER

  • - Line color

x_position, y_position: INTEGER

  • - Line position

feature -- Basic operation draw

  • - Draw line at position (x_position, y_position) with color.

end

slide-19
SLIDE 19

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

19

Chair of Softw are Engineering

With the Flyweight pattern (1/3)

class CLIENT feature -- Basic operation draw_lines is

  • - Draw some lines in color.

local line_factory: LINE_FACTORY red_line: LINE red: INTEGER do ... red_line := line_factory.new_line (red) red_line.draw (100, 200) red_line.draw (100, 400) ... end ... end Creates only one LINE

  • bject per color
slide-20
SLIDE 20

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

20

Chair of Softw are Engineering

With the Flyweight pattern (2/3)

class interface LINE_FACTORY feature -- Initialization new_line (a_color: INTEGER): LINE

  • - New line with color a_color

ensure new_line_not_void: Result /= Void ... end

slide-21
SLIDE 21

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

21

Chair of Softw are Engineering

With the Flyweight pattern (3/3)

class interface LINE create make feature -- Initialization make (a_color: INTEGER) is

  • - Set color to a_color.

ensure color_set: color = a_color feature -- Access color: INTEGER

  • - Line color

feature -- Basic operation draw (x, y: INTEGER)

  • - Draw line at position (x, y) with color.

end

slide-22
SLIDE 22

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

22

Chair of Softw are Engineering

Shared/unshared and (non-)composite objects

Two kinds of properties: Intrinsic characteristics stored in the flyweight Extrinsic characteristics moved to the client (typically a “flyweight context”)

The color of the LINE The coordinates of the LINE

slide-23
SLIDE 23

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

23

Chair of Softw are Engineering

Flyweight context

External characteristics are not stored in the flyweight object → client must handle them A possibility is to create a FLYWEIGHT_CONTEXT describing a list of flyweights grouped by FLYWEIGHT_ZONEs with the same external characteristic (e.g. characters with the same color in a row of a book page)

slide-24
SLIDE 24

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

24

Chair of Softw are Engineering

Shared/unshared and (non-)composite objects

Two kinds of flyweights: Composites (shared or unshared) Non-composites (shared)

slide-25
SLIDE 25

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

25

Chair of Softw are Engineering

Flyweight pattern: Description

Intent: “Use sharing to support large numbers of fine- grained objects efficiently.” Participants: FLYWEIGHT: Offers a service do_something to which the extrinsic characteristic will be passed SHARED_FLYWEIGHT: Adds storage for intrinsic characteristic COMPOSITE_FLYWEIGHT: Composite of shared flyweight; May be shared or unshared FACTORY: Creates and manages the flyweight

  • bjects

CLIENT: Maintains a reference to flyweight, and computes or stores the extrinsic characteristics of flyweight

slide-26
SLIDE 26

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

26

Chair of Softw are Engineering

Flyweight pattern: Typical architecture

slide-27
SLIDE 27

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

27

Chair of Softw are Engineering

Flaws of the approach

Not a reusable component Code repetition Low maintainability No Code Repetition principle If you find yourself having to copy and paste code, just stop: there is something wrong with your design.

slide-28
SLIDE 28

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

28

Chair of Softw are Engineering

Flyweight Library

Mechanisms enabling componentization: constrained genericity, agents + Factory Library and Composite Library

slide-29
SLIDE 29

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

29

Chair of Softw are Engineering

Flyweight Library: use of the Composite Library

Two kinds of flyweights: Non-composites (shared) Composites (shared or unshared) → Use the Composite Library

FLYWEIGHT [G] inherits from COMPONENT [FLYWEIGHT [G]] COMPOSITE_FLYWEIGHT [G, H] inherits from FLYWEIGHT [G] and COMPOSITE [FLYWEIGHT [G]]

Uses the safety version of the Flyweight Library where the COMPONENT does not know about its parent to allow a same flyweight object to be part of different composites

slide-30
SLIDE 30

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

30

Chair of Softw are Engineering

Class FLYWEIGHT (1/3)

deferred class FLYWEIGHT [G −> SHARABLE create make end] inherit COMPONENT [FLYWEIGHT [G]] renam e do_something as do_something_component end feature -- Initialization make (a_procedure: like procedure) is

  • - Set a_procedure to a_procedure.

require a_procedure_not_void: a_procedure /= Void do procedure := a_procedure ensure procedure_set: procedure = a_procedure end

slide-31
SLIDE 31

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

31

Chair of Softw are Engineering

Class FLYWEIGHT (2/3)

feature -- Access external_characteristic (a_context: FLYWEIGHT_CONTEXT [G]): G is

  • - External characteristic of flyweight in a_context

require a_context_not_void: a_context /= Void do Result := a_context.external_characteristic ensure external_characteristic_not_void: Result /= Void end procedure: PROCEDURE [ANY, TUPLE [FLYWEIGHT [G], FLYWEIGHT_CONTEXT [G]]]

  • - Procedure called by do_something for shared flyweights
slide-32
SLIDE 32

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

32

Chair of Softw are Engineering

Class FLYWEIGHT (3/3)

feature -- Element change set_external_characteristic (a_characteristic: like external_characteristic; a_context: FLYWEIGHT_CONTEXT [G]) is

  • - Set external_characteristic of a_context to a_characteristic.

require a_characteristic_not_void: a_characteristic /= Void a_context_not_void: a_context /= Void do a_context.start ensure external_characteristic_set: a_context.external_characteristic /= Void and then a_context.external_characteristic = a_characteristic end feature -- Output do_something (a_context: FLYWEIGHT_CONTEXT [G]) is deferred end

  • - Do something with flyweight according to a_context.

require a_context_not_void: a_context /= Void end

slide-33
SLIDE 33

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

33

Chair of Softw are Engineering

Flyweight constraint: SHARABLE

deferred class COMPOSITE [G −> SHARABLE create make end, H −> HASHABLE] With: deferred class SHARABLE inherit FLYWEIGHT_CONSTANTS feature make (a_code: like code) is … code: INTEGER … end

Actual generic parameter must conform to SHARABLE because the library requires COMPOSITEs to contain

  • nly SHARABLE objects

External characteristic must be represented by an INTEGER code

slide-34
SLIDE 34

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

34

Chair of Softw are Engineering

Flyweight pattern vs. Flyweight Library (1/2)

  • Example: A BOOK is made of SENTENCEs, which are made of

CHARACTERs (i.e. BOOK is a COMPOSITE_FLYWEIGHT of CHARACTERs)

  • With the Flyweight pattern:

new_sentence: SENTENCE is

  • - New sentence

local context: FLYWEIGHT_CONTEXT do create Result.make Result.set_text (“Successful componentization”) create context.make (‘e’) Result.draw (context) end

slide-35
SLIDE 35

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

35

Chair of Softw are Engineering

Flyweight pattern vs. Flyweight Library (1/2)

  • With the Flyweight Library:

new_sentence: SENTENCE is

  • - New sentence

local context: FLYWEIGHT_CONTEXT [CHARACTER] do create Result.make (agent draw) Result.set_text (“Successful componentization”) create context.make ('e') Result.do_something (context) end

Use of an agent to initialize the flyweight

slide-36
SLIDE 36

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

36

Chair of Softw are Engineering

Flyweight: Componentization outcome

  • Completeness

All cases of the Flyweight pattern

  • Usefulness

Reusable Easy-to-use

  • Faithfulness

Similar to a traditional implementation of Flyweight (with genericity, Composite Library and Factory Library)

  • Type-safety

Type-safe (constrained genericity, agents, assertions, and the Composite Library and Factory Library)

  • Performance

Same order as the Flyweight pattern

  • Extended applicability

No more cases

slide-37
SLIDE 37

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

37

Chair of Softw are Engineering

Complementary material (1/2)

  • From Patterns to Components:

Chapter 10: Composite Chapter 11: Flyweight

  • Further reading:

Erich Gamma: Design Patterns, 1995. (Composite, p 163-174; Flyweight, p 195-206) James W. Cooper. “C# Design Patterns: The Composite Pattern”, InformIT, 2002. http://www.informit.com/isapi/product_id~{960C23C8- 0FFE-46F2-A381-07D2036ED902}/session_id~{712CCF3C- 4969-43D3-8727-CE98E0B0E667}/content/index.asp.

slide-38
SLIDE 38

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

38

Chair of Softw are Engineering

Complementary material (2/2)

  • Further reading:

James W. Cooper. “C# Design Patterns: The Flyweight Pattern”. InformIT, 2003. http://www.informit.com/isapi/product_id~{6E1AEBDE- 0B70-4769-8C9F- AF46CC60E041}/session_id~{BFCAE277-479F-4825- AA3D-7FC5284514E4}/content/index.asp. David Geary. “A look at the Composite design pattern”, JavaWorld, 2002. http://www.javaworld.com/javaworld/jw-09-2002/jw- 0913-designpatterns.html. David Geary. “Make your apps fly, Implement Flyweight to Improve performance”. JavaWorld, 2003. http://www.javaworld.com/javaworld/jw-07-2003/jw- 0725-designpatterns.html.

slide-39
SLIDE 39

Trusted Components: Reuse, Contracts and Patterns - Lecture 17

39

Chair of Softw are Engineering

End of lecture 17