Created
April 14, 2012 19:01
-
-
Save zymsys/2386972 to your computer and use it in GitHub Desktop.
SRP and OCP example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TrafficLightDisplay | |
def showState(state) | |
puts state == :LIGHT_STATE_RED ? "* RED *" : " red" | |
puts state == :LIGHT_STATE_AMBER ? "* AMBER *" : " amber" | |
puts state == :LIGHT_STATE_GREEN ? "* GREEN *" : " green" | |
puts "---------" | |
end | |
end | |
class TrafficLight | |
def initialize(schedule) | |
@state = :LIGHT_STATE_RED | |
@schedule = schedule | |
@maxTime = schedule.keys.max | |
@displays = [] | |
end | |
def cycle | |
0.upto @maxTime do |time| | |
if @schedule.has_key?(time) | |
@state = @schedule[time] | |
signalStateChange | |
end | |
sleep 1 | |
end | |
end | |
def addDisplay(display) | |
@displays.push display | |
display.showState @state | |
end | |
def signalStateChange | |
@displays.each do |display| | |
display.showState @state | |
end | |
end | |
def run | |
while true | |
cycle | |
end | |
end | |
end | |
tl = TrafficLight.new({ | |
5=>:LIGHT_STATE_GREEN, | |
10=>:LIGHT_STATE_AMBER, | |
12=>:LIGHT_STATE_RED | |
}) | |
tl.addDisplay(TrafficLightDisplay.new) | |
tl.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TrafficLightDisplay | |
def showState(state) | |
puts state == :LIGHT_STATE_RED ? "* RED *" : " red" | |
puts state == :LIGHT_STATE_AMBER ? "* AMBER *" : " amber" | |
puts state == :LIGHT_STATE_GREEN ? "* GREEN *" : " green" | |
puts "---------" | |
end | |
end | |
class WalkSignalDisplay | |
def showState(state) | |
puts state == :LIGHT_STATE_RED ? "* DON'T WALK *" : " don't walk" | |
puts state == :LIGHT_STATE_AMBER ? "* BLINKING DON'T WALK *" : " blinking don't walk" | |
puts state == :LIGHT_STATE_GREEN ? "* WALK *" : " walk" | |
puts "-----------------------" | |
end | |
end | |
class TrafficLight | |
def initialize(schedule) | |
@state = :LIGHT_STATE_RED | |
@schedule = schedule | |
@maxTime = schedule.keys.max | |
@displays = [] | |
end | |
def cycle | |
0.upto @maxTime do |time| | |
if @schedule.has_key?(time) | |
@state = @schedule[time] | |
signalStateChange | |
end | |
sleep 1 | |
end | |
end | |
def addDisplay(display) | |
@displays.push display | |
display.showState @state | |
end | |
def signalStateChange | |
@displays.each do |display| | |
display.showState @state | |
end | |
end | |
def run | |
while true | |
cycle | |
end | |
end | |
end | |
tl = TrafficLight.new({ | |
5=>:LIGHT_STATE_GREEN, | |
10=>:LIGHT_STATE_AMBER, | |
12=>:LIGHT_STATE_RED | |
}) | |
tl.addDisplay(TrafficLightDisplay.new) | |
tl.addDisplay(WalkSignalDisplay.new) | |
tl.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TrafficLight | |
def initialize(schedule) | |
setState :LIGHT_STATE_RED | |
@schedule = schedule | |
@maxTime = schedule.keys.max | |
end | |
def cycle | |
0.upto @maxTime do |time| | |
if @schedule.has_key?(time) | |
setState(@schedule[time]) | |
end | |
sleep 1 | |
end | |
end | |
def setState(newState) | |
@state = newState | |
showState() | |
end | |
def showState | |
puts @state == :LIGHT_STATE_RED ? "* RED *" : " red" | |
puts @state == :LIGHT_STATE_AMBER ? "* AMBER *" : " amber" | |
puts @state == :LIGHT_STATE_GREEN ? "* GREEN *" : " green" | |
puts "---------" | |
end | |
def run | |
while true | |
cycle | |
end | |
end | |
end | |
TrafficLight.new({ | |
5=>:LIGHT_STATE_GREEN, | |
10=>:LIGHT_STATE_AMBER, | |
12=>:LIGHT_STATE_RED | |
}).run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TrafficLightDisplay | |
def showState(state) | |
puts state == :LIGHT_STATE_RED ? "* RED *" : " red" | |
puts state == :LIGHT_STATE_AMBER ? "* AMBER *" : " amber" | |
puts state == :LIGHT_STATE_GREEN ? "* GREEN *" : " green" | |
puts "---------" | |
end | |
end | |
class TrafficLight | |
def initialize(schedule) | |
@schedule = schedule | |
@maxTime = schedule.keys.max | |
@display = TrafficLightDisplay.new | |
setState :LIGHT_STATE_RED | |
end | |
def cycle | |
0.upto @maxTime do |time| | |
if @schedule.has_key?(time) | |
setState(@schedule[time]) | |
end | |
sleep 1 | |
end | |
end | |
def setState(newState) | |
@state = newState | |
@display.showState(@state) | |
end | |
def run | |
while true | |
cycle | |
end | |
end | |
end | |
TrafficLight.new({ | |
5=>:LIGHT_STATE_GREEN, | |
10=>:LIGHT_STATE_AMBER, | |
12=>:LIGHT_STATE_RED | |
}).run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment