class Drink
attr_reader :glass, :alcohol, :additions
def initialize(klass)
# Factory is an implementation of the specific drink
factory = klass.new
@glass = factory.prepare_glass
@alcohol = factory.add_alcohol
@additions = factory.add_additions
end
end
class WhiskyFactory
def prepare_glass
Glass.new(size: 200)
end
def add_alcohol
Whisky.new
end
def add_additions
[Ice.new, Ice.new, Lime.new]
end
end
class VodkaFactory
def prepare_glass
Glass.new(size: 40)
end
def add_alcohol
Vodka.new
end
def add_additions
nil
end
end
# Real implementations of those classes are not important for this example.
class Vodka; end
class Whisky; end
class Ice; end
class Lime; end
class Glass
attr_reader :size
def initialize(size: nil)
@size = size
end
end
class Book
include Visitable
attr_accessor :title, :price
def initialize(title:, price:)
@title = title
@price = price
end
end
vodka = Drink.new(VodkaFactory)
whisky = Drink.new(WhiskyFactory)
vodka.glass.size # => 40