Skip to content

Instantly share code, notes, and snippets.

@rachidcalazans
Last active September 29, 2018 03:56
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 rachidcalazans/cbbb0a5855c2a4860995c5bb8a8ee5d3 to your computer and use it in GitHub Desktop.
Save rachidcalazans/cbbb0a5855c2a4860995c5bb8a8ee5d3 to your computer and use it in GitHub Desktop.
Design Patterns in Ruby - Template Method
class Cappuccino
def prepare_recipe
boil_water
brew_cappuccino_grinds
pour_in_cup
add_sugar
end
private
def boil_water
puts 'boil water'
end
def brew_cappuccino_grinds
puts 'brew cappuccino grinds'
end
def pour_in_cup
puts 'pour in cup'
end
def add_sugar
puts 'add sugar'
end
end
class Tea
def prepare_recipe
boil_water
steep_the_bag
pour_in_cup
add_lemon
end
private
def boil_water
puts 'boil water'
end
def steep_the_bag
puts 'steep the bag'
end
def pour_in_cup
puts 'pour in cup'
end
def add_lemon
puts 'add lemon'
end
end
cappuccino = Cappuccino.new
cappuccino.prepare_recipe
# => boil water
# => brew cappuccino grinds
# => pour in cup
# => add sugar
tea = Tea.new
tea.prepare_recipe
# => boil water
# => steep the bag
# => pour in cup
# => add lemon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment