-
Notifications
You must be signed in to change notification settings - Fork 31
/
dci1.rb
97 lines (76 loc) · 2.09 KB
/
dci1.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# dci1.rb
# Data-Context-Interaction pattern
# The code for separate use case in OO is typically spread out between lots of differen classes.
# To understand how the code works, you must also know the relationships between objects in runtime.
# These relationships aren't set in code, they depend on the situation.
# What DCI proposes is that code for a given use-case is separated out from the classes and put into
# a different artifact called context. Objects of different classes can enter into a relationship in this
# context and take part in interaction where they have different roles.
# Suggestions:
#
# a) keep core model classes very thin
# b) keep additional logic/behaviour in roles
# c) do not do class oriented programming, do object oriented programming
# 1. Data
# thin data/model
class DataObject
attr_accessor :basic_property
end
# 2. Roles: broken into pieces functionality
module Role1
attr_accessor :role1_property
end
module Role2
attr_accessor :role2_property
end
# 3. The place where role is assigned to data/model.
# We do it dynamically at run-time, on object, not class declaration level.
# or use case
class Context
def initialize data
@data = data
end
def execute
p @data.basic_property # OK
begin
@data.role1_property
rescue StandardError
p 'Error: role1_property is not available yet'
end
@data.extend Role1
@data.role1_property = 'test_role1'
p @data.role1_property
end
end
# or context
class UseCase
def initialize data
@data = data
end
def execute
p @data.basic_property # OK
begin
@data.role2_property
rescue StandardError
p 'Error: role2_property is not available yet'
end
@data.extend Role2
@data.role2_property = 'test_role2'
p @data.role2_property
end
end
# 4. Actual Program
class Interaction
def initialize
@data = DataObject.new
@data.basic_property = 'test_basic'
end
def interact
context = Context.new @data
context.execute
use_case = UseCase.new @data
use_case.execute
end
end
interaction = Interaction.new
interaction.interact