Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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