Skip to content

Instantly share code, notes, and snippets.

@neerajkumar
Created June 6, 2018 20:25
Show Gist options
  • Save neerajkumar/7f7acfbae9b3e543afa654b9293726fd to your computer and use it in GitHub Desktop.
Save neerajkumar/7f7acfbae9b3e543afa654b9293726fd to your computer and use it in GitHub Desktop.
Design Pattern: Abstract Factory Pattern - Ruby Solution
# class Duck
# def initialize(name)
# @name = name
# end
# def eat
# puts "Duck #{@name} eats"
# end
# def speak
# puts "Duck #{@name} speaks Quack!"
# end
# def sleep
# puts "Duck #{@name} sleep silently"
# end
# end
class Frog
def initialize(name)
@name = name
end
def eat
puts "Frog #{@name} eats"
end
def speak
puts "Frog #{@name} says Crooooooooaaakkk!"
end
def sleep
puts "Frog #{@name} doesn't sleep; he croaks all night!"
end
end
# class Pond
# ## Deciding the responsible class at runtime while calling. Thats why its Factory Method Pattern
# def initialize(number_animals)
# @animals = []
# number_animals.times { |i| @animals << new_animal("Animal#{i}") }
# end
# def simulate_one_day
# @animals.each { |animal| animal.eat }
# @animals.each { |animal| animal.speak }
# @animals.each { |animal| animal.sleep }
# end
# end
# class DuckPond < Pond
# def new_animal(name)
# Duck.new(name)
# end
# end
# class FrogPond < Pond
# def new_animal(name)
# Frog.new(name)
# end
# end
class Tree
def initialize(name)
@name = name
end
def grow
puts "Tree #{@name} grows tall"
end
end
class Tiger
def initialize(name)
@name = name
end
def eat
puts "Tiger #{@name} eats anything it wants"
end
def speak
puts "Tiger #{@name} Roars!"
end
def sleep
puts "Tiger #{@name} sleeps anywhere it wants"
end
end
class WaterLily
def initialize(name)
@name = name
end
def grow
puts "The Water Lily #{@name} floats, soaks up the sun, and grows"
end
end
class OrganismFactory
def initialize(plant_class, animal_class)
@plant_class = plant_class
@animal_class = animal_class
end
def new_animal(name)
@animal_class.new(name)
end
def new_plant(name)
@plant_class.new(name)
end
end
# class JungleOrganismFactory
# def new_animal(name)
# Tiger.new(name)
# end
# def new_plant(name)
# Tree.new(name)
# end
# end
# class PondOrganismFactory
# def new_animal(name)
# Frog.new(name)
# end
# def new_plant(name)
# WaterLily.new(name)
# end
# end
class Habitat
def initialize(number_animals, number_plants, organism_factory)
@organism_factory = organism_factory
@animals = []
number_animals.times do |i|
@animals << @organism_factory.new_animal("Animal#{i}")
end
@plants = []
number_plants.times do |i|
@plants << @organism_factory.new_plant("Plant#{i}")
end
end
def simulate_one_day
@plants.each { |plant| plant.grow }
@animals.each { |animal| animal.eat }
@animals.each { |animal| animal.speak }
@animals.each { |animal| animal.sleep }
end
end
# duckpond = DuckPond.new(3)
# duckpond.simulate_one_day
# frogpond = FrogPond.new(3)
# frogpond.simulate_one_day
# jungle = Habitat.new(1, 4, JungleOrganismFactory.new)
jungle_organism_factory = OrganismFactory.new(Tree, Tiger)
jungle = Habitat.new(1, 4, jungle_organism_factory)
jungle.simulate_one_day
# pond = Habitat.new(2, 4, PondOrganismFactory.new)
pond_organism_factory = OrganismFactory.new(WaterLily, Frog)
pond = Habitat.new(2, 4, pond_organism_factory)
pond.simulate_one_day
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment