Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created March 17, 2012 01:31
Show Gist options
  • Save takaheraw/2054191 to your computer and use it in GitHub Desktop.
Save takaheraw/2054191 to your computer and use it in GitHub Desktop.
# coding: utf-8
class Algae
def initialize(name)
@name = name
end
def grow
puts "藻 #{@name} は日光浴を浴びて育っています"
end
end
class Frog
def initialize(name)
@name = name
end
def eat
puts "カエル #{@name} は食事中です."
end
def speak
puts "カエル #{@name} はゲロッゲロッと泣きます."
end
def sleep
puts "カエル #{@name} は眠りません."
end
end
class Tree
def initialize(name)
@name = name
end
def grow
puts "樹木 #{@name} が高く育っています"
end
end
class Tiger
def initialize(name)
@name = name
end
def eat
puts "トラ #{@name} は食べたいものを何でも食べます."
end
def speak
puts "トラ #{@name} はガオーとほえています."
end
def sleep
puts "トラ #{@name} は眠たくなったら眠ります."
end
end
class PondOrganismFactory
def new_animal(name)
Frog.new(name)
end
def new_plant(name)
Algae.new(name)
end
end
class JungleOrganismFactory
def new_animal(name)
Tiger.new(name)
end
def new_plant(name)
Tree.new(name)
end
end
class Habiat
def initialize(number_animals, number_plants, organism_factory)
@organism_factory = organism_factory
@animals = []
number_animals.times do |i|
animal = @organism_factory.new_animal("動物#{i}")
@animals << animal
end
@plants = []
number_plants.times do |i|
plant = @organism_factory.new_plant("植物#{i}")
@plants << plant
end
end
def simulate_one_day
@plants.each {|plant| plant.grow}
@animals.each {|animal| animal.speak}
@animals.each {|animal| animal.eat}
@animals.each {|animal| animal.sleep}
end
end
jungle = Habiat.new(1, 4, JungleOrganismFactory.new)
jungle.simulate_one_day
pond = Habiat.new(2, 4, PondOrganismFactory.new)
pond.simulate_one_day
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment