Skip to content

Instantly share code, notes, and snippets.

@joalbertg
Created September 7, 2020 03:34
Show Gist options
  • Save joalbertg/44884e72ec2a9dd2fb3e00034187193b to your computer and use it in GitHub Desktop.
Save joalbertg/44884e72ec2a9dd2fb3e00034187193b to your computer and use it in GitHub Desktop.
ruby: Factory Method Pattern

Factory Method Pattern

Duck Duck0 says Quack!
Duck Duck1 says Quack!
Duck Duck2 says Quack!
Duck Duck0 is eating.
Duck Duck1 is eating.
Duck Duck2 is eating.
Duck Duck0 sleeps quietly.
Duck Duck1 sleeps quietly.
Duck Duck2 sleeps quietly.

==========================================================================================
Template Method

Duck Animal0 says Quack!
Duck Animal1 says Quack!
Duck Animal2 says Quack!
Duck Animal0 is eating.
Duck Animal1 is eating.
Duck Animal2 is eating.
Duck Animal0 sleeps quietly.
Duck Animal1 sleeps quietly.
Duck Animal2 sleeps quietly.
Frog Animal0 says Crooooaaaak!
Frog Animal1 says Crooooaaaak!
Frog Animal2 says Crooooaaaak!
Frog Animal0 is eating.
Frog Animal1 is eating.
Frog Animal2 is eating.
Frog Animal0 doesn't sleep; he croaks all night!
Frog Animal1 doesn't sleep; he croaks all night!
Frog Animal2 doesn't sleep; he croaks all night!

==========================================================================================
Parameterized Factory Method

The water lily Plant0 floats, soaks up the sun, and grows
The water lily Plant1 floats, soaks up the sun, and grows
Duck Animal0 says Quack!
Duck Animal1 says Quack!
Duck Animal2 says Quack!
Duck Animal0 is eating.
Duck Animal1 is eating.
Duck Animal2 is eating.
Duck Animal0 sleeps quietly.
Duck Animal1 sleeps quietly.
Duck Animal2 sleeps quietly.
The Algae Plant0 soaks up the sun and grows
The Algae Plant1 soaks up the sun and grows
Frog Animal0 says Crooooaaaak!
Frog Animal1 says Crooooaaaak!
Frog Animal2 says Crooooaaaak!
Frog Animal0 is eating.
Frog Animal1 is eating.
Frog Animal2 is eating.
Frog Animal0 doesn't sleep; he croaks all night!
Frog Animal1 doesn't sleep; he croaks all night!
Frog Animal2 doesn't sleep; he croaks all night!

==========================================================================================
Parameterized Factory Method, next steps

The water lily Plant0 floats, soaks up the sun, and grows
The water lily Plant1 floats, soaks up the sun, and grows
Duck Animal0 says Quack!
Duck Animal1 says Quack!
Duck Animal2 says Quack!
Duck Animal0 is eating.
Duck Animal1 is eating.
Duck Animal2 is eating.
Duck Animal0 sleeps quietly.
Duck Animal1 sleeps quietly.
Duck Animal2 sleeps quietly.
The Algae Plant0 soaks up the sun and grows
The Algae Plant1 soaks up the sun and grows
Frog Animal0 says Crooooaaaak!
Frog Animal1 says Crooooaaaak!
Frog Animal2 says Crooooaaaak!
Frog Animal0 is eating.
Frog Animal1 is eating.
Frog Animal2 is eating.
Frog Animal0 doesn't sleep; he croaks all night!
Frog Animal1 doesn't sleep; he croaks all night!
Frog Animal2 doesn't sleep; he croaks all night!

==========================================================================================
Factory Method

The water lily Plant0 floats, soaks up the sun, and grows
The water lily Plant1 floats, soaks up the sun, and grows
Duck Animal0 says Quack!
Duck Animal1 says Quack!
Duck Animal2 says Quack!
Duck Animal0 is eating.
Duck Animal1 is eating.
Duck Animal2 is eating.
Duck Animal0 sleeps quietly.
Duck Animal1 sleeps quietly.
Duck Animal2 sleeps quietly.
The Algae Plant0 soaks up the sun and grows
The Algae Plant1 soaks up the sun and grows
Frog Animal0 says Crooooaaaak!
Frog Animal1 says Crooooaaaak!
Frog Animal2 says Crooooaaaak!
Frog Animal0 is eating.
Frog Animal1 is eating.
Frog Animal2 is eating.
Frog Animal0 doesn't sleep; he croaks all night!
Frog Animal1 doesn't sleep; he croaks all night!
Frog Animal2 doesn't sleep; he croaks all night!
module FactoryMethod
module V1
class Duck
def initialize(name)
@name = name
end
def eat
puts("Duck #{name} is eating.")
end
def speak
puts("Duck #{name} says Quack!")
end
def sleep
puts("Duck #{name} sleeps quietly.")
end
private
attr_reader :name
end
class Frog
def initialize(name)
@name = name
end
def eat
puts("Frog #{name} is eating.")
end
def speak
puts("Frog #{name} says Crooooaaaak!")
end
def sleep
puts("Frog #{name} doesn't sleep; he croaks all night!")
end
private
attr_reader :name
end
class Pond
def initialize(number_ducks)
@ducks = []
number_ducks.times do |i|
duck = Duck.new("Duck#{i}")
ducks << duck
end
end
def simulate_on_day
ducks.each { |duck| duck.speak }
ducks.each { |duck| duck.eat }
ducks.each { |duck| duck.sleep }
end
private
attr_reader :ducks
end
end
module V2
class Pond
def initialize(number_animals)
@animals = []
number_animals.times do |i|
animal = new_animal("Animal#{i}")
animals << animal
end
end
def simulate_on_day
animals.each { |animal| animal.speak }
animals.each { |animal| animal.eat }
animals.each { |animal| animal.sleep }
end
private
attr_reader :animals
end
class DuckPond < Pond
def new_animal(name)
V1::Duck.new(name)
end
end
class FrogPond < Pond
def new_animal(name)
V1::Frog.new(name)
end
end
end
module V3
class Algae
def initialize(name)
@name = name
end
def grow
puts("The Algae #{name} soaks up the sun and grows")
end
private
attr_reader :name
end
class WaterLily
def initialize(name)
@name = name
end
def grow
puts("The water lily #{name} floats, soaks up the sun, and grows")
end
private
attr_reader :name
end
class Pond
def initialize(number_animals, number_plants)
@animals = []
number_animals.times do |i|
animal = new_animal("Animal#{i}")
animals << animal
end
@plants = []
number_plants.times do |i|
plant = new_plant("Plant#{i}")
plants << plant
end
end
def simulate_on_day
plants.each { |plant| plant.grow }
animals.each { |animal| animal.speak }
animals.each { |animal| animal.eat }
animals.each { |animal| animal.sleep }
end
private
attr_reader :animals, :plants
end
class DuckWaterLilyPond < Pond
def new_animal(name)
V1::Duck.new(name)
end
def new_plant(name)
WaterLily.new(name)
end
end
class FrogAlgaePond < Pond
def new_animal(name)
V1::Frog.new(name)
end
def new_plant(name)
Algae.new(name)
end
end
end
module V4
class Pond < V3::Pond
def initialize(number_animals, number_plants)
@animals = []
number_animals.times do |i|
animal = new_organism(:animal, "Animal#{i}")
animals << animal
end
@plants = []
number_plants.times do |i|
plant = new_organism(:plant, "Plant#{i}")
plants << plant
end
end
private
attr_reader :animals, :plants
end
class DuckWaterLilyPond < Pond
def new_organism(type, name)
if type == :animal
V1::Duck.new(name)
elsif type == :plant
V3::WaterLily.new(name)
else
raise "Unknown organism type: #{type}"
end
end
end
class FrogAlgaePond < Pond
def new_organism(type, name)
if type == :animal
V1::Frog.new(name)
elsif type == :plant
V3::Algae.new(name)
else
raise "Unknown organism type: #{type}"
end
end
end
end
module V5
class Pond < V4::Pond
def initialize(number_animals, animal_class, number_plants, plant_class)
@animal_class = animal_class
@plant_class = plant_class
@animals = []
number_animals.times do |i|
animal = new_organism(:animal, "Animal#{i}")
animals << animal
end
@plants = []
number_plants.times do |i|
plant = new_organism(:plant, "Plant#{i}")
plants << plant
end
end
def new_organism(type, name)
if type == :animal
animal_class.new(name)
elsif type == :plant
plant_class.new(name)
else
raise "Unknown organism type: #{type}"
end
end
private
attr_reader :animal_class, :plant_class
attr_reader :animals, :plants
end
end
end
number_ducks = 3
number_animals = 3
#FactoryMethod::V1::Duck.new('Donald').eat
#FactoryMethod::V1::Frog.new('Pepe').eat
pond = FactoryMethod::V1::Pond.new(number_ducks)
pond.simulate_on_day
puts <<-EOF
==========================================================================================
Template Method
EOF
pond = FactoryMethod::V2::DuckPond.new(number_animals)
pond.simulate_on_day
pond = FactoryMethod::V2::FrogPond.new(number_animals)
pond.simulate_on_day
puts <<-EOF
==========================================================================================
Parameterized Factory Method
EOF
number_plants = 2
pond = FactoryMethod::V3::DuckWaterLilyPond.new(number_animals, number_plants)
pond.simulate_on_day
pond = FactoryMethod::V3::FrogAlgaePond.new(number_animals, number_plants)
pond.simulate_on_day
puts <<-EOF
==========================================================================================
Parameterized Factory Method, next steps
EOF
pond = FactoryMethod::V4::DuckWaterLilyPond.new(number_animals, number_plants)
pond.simulate_on_day
pond = FactoryMethod::V4::FrogAlgaePond.new(number_animals, number_plants)
pond.simulate_on_day
puts <<-EOF
==========================================================================================
Factory Method
EOF
pond = FactoryMethod::V5::Pond.new(number_animals, FactoryMethod::V1::Duck,
number_plants, FactoryMethod::V3::WaterLily)
pond.simulate_on_day
pond = FactoryMethod::V5::Pond.new(number_animals, FactoryMethod::V1::Frog,
number_plants, FactoryMethod::V3::Algae)
pond.simulate_on_day
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment