-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_responsibility.rb
174 lines (135 loc) · 3.52 KB
/
single_responsibility.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Another way to hone in on what a class is actually doing is to attempt to describe it
# in one sentence. Remember that a class should do the smallest possible useful thing.
# That thing ought to be simple to describe. If the simplest description you can devise
# uses the word “and,” the class likely has more than one responsibility. If it uses the word
# “or,” then the class has more than one responsibility and they aren’t even very related.
class Gear
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
def ratio
@chainring / @cog.to_f
end
end
######################################
# Hide instance variables
class Gear
attr_reader :chainring, :cog
def initialize(chainring, cog)
@chainring = chainring
@cog = cog
end
def ratio
chainring / cog.to_f
end
end
######################################
class ObscuringReferences
attr_reader :data
def initialize(data)
@data = data
end
def diameters
# 0 is rim, 1 is tire
data.collect {|cell|
cell[0] + (cell[1] * 2)}
end
# ... many other methods that index into the array
end
# rim and tire sizes (now in milimeters!) in a 2d array
@data = [[622, 20], [622, 23], [559, 30], [559, 40]]
######################################
class RevealingReferences
attr_reader :wheels
def initialize(data)
@wheels = wheelify(data)
end
# This method clearly has two responsibilities: it iterates over the wheels and it calculates
# the diameter of each wheel.
def diameters
wheels.map {|wheel| wheel.rim + wheel.tire * 2}
end
Wheel = Struct.new(:rim, :tire)
def wheelify(data)
data.map {|cell| Wheel.new(cell[0], cell[1])}
end
end
######################################
# Extract extra responsibilites from methods
#
# Methods, like classes, should have a single responsibility. All of the same reasons
# apply; having just one responsibility makes them easy to change and easy to reuse. All
# the same design techniques work; ask them questions about what they do and try to
# describe their responsibilities in a single sentence.
class RevealingReferences
attr_reader :wheels
def initialize(data)
@wheels = wheelify(data)
end
def diameters
wheels.map {|wheel| diameter(wheel)}
end
def diameter(wheel)
wheel.rim + wheel.tire * 2
end
Wheel = Struct.new(:rim, :tire)
def wheelify(data)
data.map {|cell| Wheel.new(cell[0], cell[1])}
end
end
######################################
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@wheel = Wheel.new(rim, tire)
end
def ratio
chainring / cog.to_f
end
def gear_inches
ratio * wheel.diameter
end
Wheel = Struct.new(:rim, :tire) do
def diameter
rim + (tire * 2)
end
end
end
######################################
class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, wheel = nil)
@chainring = chainring
@cog = cog
@wheel = wheel
end
def ratio
chainring / cog.to_f
end
def gear_inches
ratio * wheel.diameter
end
end
class Wheel
attr_reader :rim, :tire
def initialize(rim, tire)
@rim = rim
@tire = tire
end
def diameter
rim + tire * 2
end
def circumference
diameter * Math::PI
end
end
@wheel = Wheel.new(26, 1.5)
puts @wheel.circumference
# -> 91.106186954104
puts Gear.new(52, 11, @wheel).gear_inches
# -> 137.090909090909
puts Gear.new(52, 11).ratio
# -> 4.72727272727273