Skip to content

Instantly share code, notes, and snippets.

View Sihui's full-sized avatar

Sihui Huang Sihui

View GitHub Profile
@Sihui
Sihui / standard_car_builder.rb
Last active January 6, 2018 19:19
Design Pattern: Builder and Car
class StandardCarBuilder
def build
car = ''
# 1. build car frame
car << "This is a standard car\n"
# 2. add an engine
car << " with an engine\n"
# 3. add front wheels
car << " with two front wheels\n"
# 4. add back wheels
class OreoCheesecake
def make_crust
puts 'making OreoCheesecake curst'
end
def add_layers
puts 'making OreoCheesecake layers'
end
def bake
@Sihui
Sihui / actor_proxy.rb
Created August 6, 2017 23:17
Design Pattern: Proxy and Agent
class ActorProxy
attr_reader :schedule, :preference, :actor
def initialize(schedule, preference, actor)
@schedule = schedule
@preference = preference
@actor = actor
end
def handle(request)
@Sihui
Sihui / lock.rb
Last active November 29, 2017 06:02
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)
@Sihui
Sihui / entered_pin_three_state.rb
Last active August 6, 2017 15:42
Design Pattern: State and Combination Locks
class EnteredPinThreeState
attr_reader :lock, :state_name
def initialize(lock)
@lock = lock
@state_name = 'Entered Pin Three State'
end
def dial_to(number)
lock.state = lock.entered_wrong_pin_state
@Sihui
Sihui / unlocked_state.rb
Created August 6, 2017 15:38
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.'
@Sihui
Sihui / entered_wrong_pin_state.rb
Created August 6, 2017 15:38
Design Pattern: State and Combination Locks
class EnteredWrongPinState
attr_reader :lock, :state_name
def initialize(lock)
@lock = lock
@state_name = 'Entered Wrong Pin State'
end
def dial_to(number)
puts "The lock is in #{state_name}. Dial does nothing."
@Sihui
Sihui / entered_pin_two_state.rb
Last active August 6, 2017 15:38
Design Pattern: State and Combination Locks Raw
class EnteredPinTwoState
attr_reader :lock, :state_name
def initialize(lock)
@lock = lock
@state_name = 'Entered Pin Two State'
end
def dial_to(number)
if number == 9
@Sihui
Sihui / entered_pin_one_state.rb
Last active August 6, 2017 15:38
Design Pattern: State and Combination Locks
class EnteredPinOneState
attr_reader :lock, :state_name
def initialize(lock)
@lock = lock
@state_name = 'Entered Pin One State'
end
def dial_to(number)
if number == 8
@Sihui
Sihui / cleared_state.rb
Last active August 6, 2017 15:38
Design Pattern: State and Combination Locks
class ClearedState
attr_reader :lock, :state_name
def initialize(lock)
@lock = lock
@state_name = 'Cleared State'
end
def dial_to(number)
if number == 7