Skip to content

Instantly share code, notes, and snippets.

@mgiagante
Last active January 17, 2017 06:37
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 mgiagante/18820dba42e19f1c752e6d19d49373b8 to your computer and use it in GitHub Desktop.
Save mgiagante/18820dba42e19f1c752e6d19d49373b8 to your computer and use it in GitHub Desktop.
class Pie
def make
add_main_ingredient
add_secondary_ingredients
cook
add_toppings
let_cool
end
def add_ingredient(ingredient)
@ingredients << ingredient
end
def oven_time
25 # Default implementation for the oven time to make a generic pie.
end
def add_toppings
# This is a hook method.
# Since your concrete pie might or might not include toppings, this is empty for the subclasses to implement it if needed.
end
def cook
put_in_oven
wait_for_minutes(self.oven_time) # Asks for the oven time for the concrete pie.
take_out_from_oven
end
def let_cool
wait_for_minutes(30)
end
end
class ApplePie < Pie
def add_main_ingredient
add_ingredient(:apples)
end
def add_secondary_ingredients
add_ingredient(:sugar)
end
def add_toppings
add_ingredient(:apple_slices) # This hook method is defined by the ApplePie, but not by the MeatPie since it's not needed.
end
def oven_time
20 # We override the default implementation received by inheriting from Pie.
end
end
class MeatPie < Pie
def add_main_ingredient
add_ingredient(:grounded_meat)
end
def add_secondary_ingredients
add_ingredient(:salt)
add_ingredient(:pepper)
end
def oven_time
35 # We override the default implementation received by inheriting from Pie.
end
end
MeatPie.make
ApplePie.make
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment