Skip to content

Instantly share code, notes, and snippets.

@Sihui
Last active April 23, 2017 16:32
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/b5d233c5d86be41efd0ed6547a3b5e5e to your computer and use it in GitHub Desktop.
Save Sihui/b5d233c5d86be41efd0ed6547a3b5e5e to your computer and use it in GitHub Desktop.
# Concrete Strategy
class GrilledChickenStuffing
def cook
stuffing = []
stuffing << 'sliced tomato'
stuffing << 'lettuce'
stuffing << 'grilled chicken breast'
stuffing
end
end
class BeefPattyStuffing
def cook
stuffing = []
stuffing << 'cheese'
stuffing << 'grilled beef patty'
stuffing
end
end
# Context
class Burger
attr_reader :top_bun, :bottom_bun
attr_accessor :stuffing
def initialize(stuffing)
@top_bun = TOP_BUN
@stuffing = stuffing
@bottom_bun = BOTTOM_BUN
end
def cook
burger = []
burger << top_bun.cook
burger << stuffing.cook
burger << bottom_bun.cook
burger
end
end
chicken_burger = Burger.new(GrilledChickenStuffing.new)
cheese_burger = Burger.new(BeefPattyStuffing.new)
# change the chicken_burger to a cheese burger
chicken_burger.stuffing = BeefPattyStuffing.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment