Slide 32
Slide 32 text
☕ Refactor: Extract factory class
class CoffeeMachine
def vend(drink: :coffee, options: {})
BeverageFactory.build(drink, options).prepare
end
end
class BeverageFactory
def self.build(drink, options)
if drink == :coffee
Coffee.new(options: options)
elsif drink == :tea
Tea.new(options: options)
elsif drink == :cocoa
Cocoa.new(options: options)
end
end
end
class Coffee
def prepare
dispense_cup
heat_water
prepare_grounds
dispense_water
dispense_sweetener if options[:sweet]
dispense_creamer if options[:creamy]
dispense_whipped_cream if options[:fluffy]
dispose_of_grounds
end
end
class Tea
def prepare
dispense_cup
heat_water
dispense_tea_bag
dispense_water
dispense_sweetener if options[:sweet]
dispense_creamer if options[:creamy]
end
end
class Cocoa
def prepare
dispense_cup
heat_water
dispense_cocoa_mix
dispense_water
dispense_whipped_cream if options[:fluffy]
end
end