Skip to content

Commit 3659675

Browse files
committed
OOD
1 parent 7138677 commit 3659675

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

Composition/Bicycle.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Bicycle # Bicycle is composed of parts
2+
attr_reader :size, :parts
3+
4+
def initialize(size:, parts:)
5+
@size = size
6+
@parts = parts
7+
end
8+
9+
def spares
10+
parts.spares
11+
end
12+
end

Composition/MountainBikeParts.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require_relative 'Parts'
2+
3+
class MountainBikeParts<Parts
4+
attr_reader :front_shock, :rear_shock
5+
6+
def post_initialize(**opts)
7+
@front_shock = opts[:front_shock]
8+
@rear_shock = opts[:rear_shock]
9+
end
10+
11+
def local_spares
12+
{
13+
front_shock: front_shock
14+
}
15+
end
16+
17+
def default_tire_size
18+
"2.1"
19+
end
20+
end
21+

Composition/Parts.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Parts # Abstract class
2+
attr_reader :chain, :tire_size
3+
4+
def initialize(**opts)
5+
@chain = opts[:chain] || default_chain
6+
@tire_size = opts[:tire_size] || default_tire_size
7+
8+
post_initialize(**opts)
9+
end
10+
11+
def spares
12+
{ chain: chain,
13+
tire_size: tire_size}.merge(local_spares)
14+
end
15+
16+
def default_tire_size
17+
raise NotImplementedError
18+
end
19+
20+
def post_initialize(**opts)
21+
# hook
22+
end
23+
24+
def default_chain
25+
"11-Speed"
26+
end
27+
28+
def local_spares
29+
{}
30+
end
31+
end

Composition/RoadBikeParts.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require_relative 'Parts'
2+
3+
class RoadBikeParts<Parts
4+
attr_reader :tape_color # getter method
5+
6+
def post_initialize(**opts)
7+
@tape_color = opts[:tape_color]
8+
end
9+
10+
def local_spares
11+
{
12+
tape_color: tape_color
13+
}
14+
end
15+
16+
def default_tire_size
17+
"23"
18+
end
19+
end
20+

Composition/Test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require_relative 'Bicycle'
2+
require_relative 'Parts'
3+
require_relative 'RoadBikeParts'
4+
require_relative 'MountainBikeParts'
5+
6+
class Test
7+
road_bike = Bicycle.new(
8+
size: "L",
9+
parts: RoadBikeParts.new(tape_color: "pink")
10+
)
11+
12+
puts road_bike.size
13+
puts road_bike.parts.tape_color
14+
15+
16+
end
17+

0 commit comments

Comments
 (0)