Last active
November 29, 2017 06:02
-
-
Save Sihui/e3020d9d9bd2071e0a496ad2564c2280 to your computer and use it in GitHub Desktop.
Design Pattern: State and Combination Locks
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 Lock | |
attr_reader :cleared_state, :entered_pin_one_state, | |
:entered_pin_two_state, :entered_pin_three_state, | |
:unlocked_state, :entered_wrong_pin_state | |
attr_accessor :state | |
def initialize | |
@cleared_state = ClearedState.new(self) | |
@entered_pin_one_state = EnteredPinOneState.new(self) | |
@entered_pin_two_state = EnteredPinTwoState.new(self) | |
@entered_pin_three_state = EnteredPinThreeState.new(self) | |
@unlocked_state = UnlockedState.new(self) | |
@entered_wrong_pin_state = EnteredWrongPinState.new(self) | |
@state = cleared_state | |
end | |
def show_current_state | |
puts "currently in #{state.state_name}" | |
end | |
def dial_to(number) | |
state.dial_to(number) | |
end | |
def clear | |
state.clear | |
end | |
def pull_to_open | |
state.pull_to_open | |
end | |
def push_to_lock | |
state.push_to_lock | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment