Last active
April 23, 2017 16:32
-
-
Save Sihui/b5d233c5d86be41efd0ed6547a3b5e5e to your computer and use it in GitHub Desktop.
For [Design Pattern: Strategy and Burger](https://medium.com/@sihui/design-pattern-strategy-and-burger-d31541d295a9)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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