Last active
September 29, 2018 03:56
-
-
Save rachidcalazans/cbbb0a5855c2a4860995c5bb8a8ee5d3 to your computer and use it in GitHub Desktop.
Design Patterns in Ruby - Template Method
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
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