Skip to content

Instantly share code, notes, and snippets.

@Sihui
Last active November 29, 2017 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sihui/e3020d9d9bd2071e0a496ad2564c2280 to your computer and use it in GitHub Desktop.
Save Sihui/e3020d9d9bd2071e0a496ad2564c2280 to your computer and use it in GitHub Desktop.
Design Pattern: State and Combination Locks
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