Last active
August 6, 2017 15:38
-
-
Save Sihui/815ca8e6b9189d9edf941b2253195e7f 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 ClearedState | |
attr_reader :lock, :state_name | |
def initialize(lock) | |
@lock = lock | |
@state_name = 'Cleared State' | |
end | |
def dial_to(number) | |
if number == 7 | |
lock.state = lock.entered_pin_one_state | |
else | |
lock.state = lock.entered_wrong_pin_state | |
end | |
end | |
def clear | |
puts "The lock is cleared. Clear does nothing." | |
end | |
def pull_to_open | |
puts 'The lock is still locked' | |
puts " currently in #{state_name}" | |
end | |
def push_to_lock | |
puts 'The lock is already locked' | |
puts " currently in #{state_name}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment