Design Patterns in Eiffel
- Dr. Till Bay
Design Patterns in Eiffel Dr. Till Bay design patterns? [Design - - PowerPoint PPT Presentation
Design Patterns in Eiffel Dr. Till Bay 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,
Comerge AG
communicating objects and classes that are customized to solve a general design problem in a particular context.”
2
Comerge AG
Ad-hoc Solution Communicated Solution (“CutʼnʼPaste”) Documented Solution Design Pattern Wizard / Tool Language Library Bottleneck
3
Comerge AG
shallow” (Eric Raymond)
4
Comerge AG
target.
architecture.
5
Comerge AG
OBSERVER SUBJECT
CONCRETE_ OBSERVER CONCRETE_ SUBJECT LIST[OBSERVER]
6
Comerge AG
CLICK_ OBSERVER CLICKABLE
ON_CLICK_ ACTION BUTTON LIST[OBSERVER]
7
Comerge AG
EVENT_TYPE SUBJECT event_x PROCEDURE (“agent”) subscribers OBSERVER target
8
Library class Language feature
Comerge AG
class EVENT_TYPE [G -> TUPLE] feature -- Subscription subscribers: LIST [PROCEDURE [ANY, G]] subscribe (an_action: PROCEDURE [ANY, G])
require not_void: an_action /= Void ensure subscribed: subscribers.has (an_action) feature -- Publish publish (arguments: G)
require not_void: arguments /= Void end
9
Comerge AG
class DATABASE ... feature -- Slots remove_customer is ... end ... end class BUTTON ... feature -- Signals clicked: EVENT_TYPE [TUPLE []] ... end gui_remove_button.clicked.subscribe (agent db.remove_customer)
10
Comerge AG
class DATABASE ... feature -- Slots remove_customer is ... end ... end class BUTTON ... feature -- Signals clicked: EVENT_TYPE [TUPLE [INTEGER]] ... end gui_remove_button.clicked.subscribe (agent db.remove_customer)
11
TUPLE[INTEGER] TUPLE[]
Comerge AG
PROCEDURE [ANY,TUPLE[INTEGER]] PROCEDURE [ANY,TUPLE[ANY]] clicked: EVENT_TYPE [TUPLE [INTEGER]] ... clicked.subscribe (print (?)) Does not type check! not print (what:ANY) ...
12
Comerge AG
PROCEDURE [ANY,TUPLE[ANY]] PROCEDURE [ANY,TUPLE[INTEGER]] network.received: EVENT_TYPE [TUPLE [ANY]] ... received.subscribe (print_integer (?)) Compiles with type error (CAT call!) print_integer (v: INTEGER) ...
13
Comerge AG
generics, tuple) to implement as library
14
Comerge AG
method) or class (abstract factory).
15
Comerge AG
class ET_AST_FACTORY ... feature -- Factory method new_class: (a_name: ET_CLASS_NAME): ET_CLASS is
require ... do create Result.make (a_name) ensure ... end end
16
Comerge AG
17
CLIENT
WIDGET_FACTORY
GDI_W_FACTORY AQUA_W_FACTORY
WINDOW GDI_WINDOW AQUA_WINDOW SCROLL_BAR GDI_SCROLL_B AQUA_SCROLL_B
Comerge AG
18
ABSTRACT_FACTORY [G -> CREATABLE]
GDI_W_FACTORY AQUA_W_FACTORY
CREATABLE default_create
Comerge AG
method) or class (abstract factory).
19
Comerge AG
instance.
20
Comerge AG
1.Because some environment constraint makes it necessary: OPERATING_SYSTEM, STANDARD_FILES, COMMAND_LINE_ARGUMENTS 2.To get a stateless functional infrastructure URL_ENCODING, MATH_SUPPORT 3.To access shared data PARSED_AST, DATABASE, SESSION_DATA
21
Comerge AG
class FILE_SYSTEM ... end class SHARED_FILE_SYSTEM feature -- Access File_system: FILE_SYSTEM is
create Result ensure not_void: Result /= Void end end
22
Comerge AG
Does it work?
23
problems with reuse:
local application1: MY_APPLICATION application2: MY_APPLICATION do create application1 application1.do_something create application2 application2.do_something_else end
Comerge AG
system.
increased “noise” in the code
24
Comerge AG
local u1,u2: MY_UNIVERSE application1: MY_APPLICATION application2: MY_APPLICATION do create u1 create application1.make (u1) application1.do_something create u2 create application2.make (u2) application2.do_something_else end
25
Comerge AG
class MY_APPLICATION feature -- Initialization make (u: MY_UNIVERSE) is
do universe := u create my_subsystem.make (u) ensure universe_set: universe = u end feature -- Access universe: MY_UNIVERSE
my_subsystem: MY_SUBSYSTEM
end
26
Comerge AG
universes.
class SHARED_UNIVERSE_STACK feature -- Access Universe_stack: LINKED_STACK [MY_UNIVERSE] is
local default_universe: MY_UNIVERSE
create Result create default_universe Result.extend (default_universe) ensure not_void: Result /= Void end
27
Comerge AG
feature -- Universe universe: MY_UNIVERSE is
do Result := Universe_stack.item end push_universe is
local new_universe: MY_UNIVERSE
create new_universe Result.extend (new_universe) end pop_universe is
do ... end
28
Some contracts skipped for brevity!
Comerge AG
local application1: MY_APPLICATION application2: MY_APPLICATION do create application1.make application1.do_something push_universe create application2.make application2.do_something_else pop_universe end
29
Comerge AG
30
and environment constraints.
universe object.
application creating a lot of noise can benefit from using a universe stack.
comerge AG
during object creation.
repository of objects.
Comerge AG
class FLYWEIGHT_FACTORY feature -- Factory make_new_flyweight (a: SOME_ARG): FLYWEIGHT is
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
32
comerge AG
semantics
equality (is_equal, ~)
comerge AG
where you would have used flyweight for Java or .NET
following:
comerge AG
Expanded Type Flyweight
types
comerge AG
recursive data structure
Comerge AG
37
Comerge AG
38
LEAF * NODE NON_LEAF sub_node: NODE
Comerge AG
deferred class EXPRESSION end
39
Comerge AG
class INTEGER_EXPRESSION inherit EXPRESSION feature -- Access value: INTEGER
end
40
Comerge AG
class ADDITION_EXPRESSION inherit EXPRESSION feature -- Access left: EXPRESSION
right: EXPRESSION
invariant left_not_void: left /= Void right_not_void: right /= Void end
41
Comerge AG
class MULTIPLICATION_EXPRESSION inherit EXPRESSION feature -- Access left: EXPRESSION
right: EXPRESSION
invariant left_not_void: left /= Void right_not_void: right /= Void end
42
Comerge AG
43
(recursively) built from parts.
nodes.
using visitors (next pattern).
Comerge AG
data structure.
data node.
composite pattern.
44
Comerge AG
INTEGER_ EXPRESSION ADDITION_ EXPRESSION MULTIPLICATION_ EXPRESSION EXPRESSION_ COMPUTATION_ VISITOR Return the integer value. Compute the left and right subtree, then add the values. Compute the left and right subtree, then multiply the values. CODE_ GENERATOR_ VISITOR Generate code that stores the value on the stack. Generate code for the left and right subtree, then code that pops two elements off the stack and adds them. Generate code for the left and right subtree, then code that pops two elements off the stack and multiplies them.
45
Comerge AG
the dynamic type of two objects.
MultiJava, LISP, Python (via language extensions)
dynamic binding on the visitor and the subject
46
Comerge AG
EXPRESSION_ COMPUTATION_ VISITOR ADDITION_ EXPRESSION process (Current) process_addition (Current)
47
Comerge AG
deferred class EXPRESSION_VISTOR feature -- Processing process_integer (e: INTEGER_EXPRESSION) is
deferred end process_addition (e: ADDITION_EXPRESSION) is
deferred end process_multiplication (e: MULTIPLICATION_EXPRESSION) is
deferred end end
48
Some contracts skipped for brevity!
Comerge AG
deferred class EXPRESSION feature -- Processing process (v: EXPRESSION_VISITOR) is
require not_void: v /= Void deferred end end
49
Comerge AG
class INTEGER_EXPRESSION inherit EXPRESSION feature -- Access value: INTEGER
feature -- Processing process (v: EXPRESSION_VISITOR) is
do v.process_integer (Current) end end
50
Comerge AG
class ADDITION_EXPRESSION inherit EXPRESSION ... feature -- Processing process (v: EXPRESSION_VISITOR) is
do v.process_addition (Current) end ... end
51
Comerge AG
class EXPRESSION_COMPUTATION_VISTOR feature -- Access last_result: INTEGER feature -- Processing process_integer (e: INTEGER_EXPRESSION) is
do last_result := e.value end
52
Comerge AG
process_addition (e: ADDITION_EXPRESSION) is
local right_value, left_value: INTEGER do e.left.process (Current) left_value := last_result e.right.process (Current) right_value := last_result last_result := right_value + left_value end
end
53
Comerge AG
class EXPRESSION_ITERATOR inherit EXPRESSION_VISITOR feature -- Processing process_integer (e: INTEGER_EXPRESSION) is
do end process_addition (e: ADDITION_EXPRESSION) is
do e.left.process (Current) e.right.process (Current) end ... end
54
Comerge AG
structures
adaptation of the abstract visitor and its children
55
Comerge AG
aggregate object sequentially without exposing its underlying representation.”
56
Comerge AG
deferred class LINEAR [G] feature -- Access item: G is
require not_off: not off deferred end feature -- Cursor movement forth is
require not_off: not off deferred end feature -- Status
end
57
NOT like Java: hasNext() next()
Comerge AG
58
comerge AG
LINKED_LIST [G] LINKABLE [G] LINKABLE [G] LINKABLE [G] right cursor_position first right class LINKED_LIST [G] inherit LINEAR [G]
comerge AG
LINKED_LIST [G] LINKABLE [G] LINKABLE [G] LINKABLE [G] right cursor_position first right LINKED_LIST_ CURSOR [G]
comerge AG
LINKED_LIST [G] LINKABLE [G] LINKABLE [G] LINKABLE [G] right cursor_position first right LINKED_LIST_ CURSOR [G] iterators
Comerge AG
feature -- Iterators do_all (c: PROCEDURE [ANY, TUPLE [G]])
for_all (q: FUNCTION [ANY, TUPLE [G], BOOLEAN]): BOOLEAN
there_exists (q: FUNCTION [ANY, TUPLE [G], BOOLEAN]): BOOLEAN
62
comerge AG
+ easy to track and contract
+ independent operations, less side-effects
+ No dead nodes, well-defined behavior
+ powerful, very expressive, safe
Comerge AG
64
WINDOW
GDI_WINDOW AQUA_WINDOW
WINDOW
GDI_WINDOW FULLSCREEN_ WINDOW AQUA_WINDOW
FULLSCREEN_ GDI_WINDOW FULLSCREEN_ AQUA_WINDOW
Comerge AG
65
WINDOW FULLSCREEN_ WINDOW WINDOW_IMP
GDI_ WINDOW_IMP AQUA_ WINDOW_IMP FULLSCREEN_ WINDOW_IMP
FULLSCREEN_ GDI_WINDOW_IMP FULLSCREEN_ AQUA_WINDOW_IMP
Comerge AG
66
EV_BUTTON EV_BUTTON_I
EV_BUTTON _IMP EV_BUTTON _IMP
Selection done in .ecf files
Comerge AG
class EV_BUTTON feature {EV_ANY, EV_ANY_I } -- Implementation implementation: EV_BUTTON_I feature {NONE } -- Implementation feature -- Cursor movement create_implementation is
do create { EV_BUTTON_IMP } implementation.make (Current) end end
67
Comerge AG
implementation
subclassing
clients
clients
68
Comerge AG
69