Skip to content

Instantly share code, notes, and snippets.

@Sihui
Created August 6, 2017 15:38
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/b6587e1b3c0cb5983468de3e906c2343 to your computer and use it in GitHub Desktop.
Save Sihui/b6587e1b3c0cb5983468de3e906c2343 to your computer and use it in GitHub Desktop.
Design Pattern: State and Combination Locks
class UnlockedState
attr_reader :lock, :state_name
def initialize(lock)
@lock = lock
@state_name = 'Unlocked State'
end
def dial_to(number)
puts 'The lock is unlocked. Dial does nothing.'
end
def clear
puts 'The lock is unlocked. Clear does nothing.'
end
def pull_to_open
puts 'The lock is unlocked. Pull does nothing.'
end
def push_to_lock
lock.state = lock.cleared_state
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment