Created
March 17, 2012 01:37
-
-
Save takaheraw/2054214 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 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 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_organism_factory = OrganismFactory.new(Tree, Tiger) | |
pong_organism_factory = OrganismFactory.new(Algae, Frog) | |
jungle = Habiat.new(1, 4, jungle_organism_factory) | |
jungle.simulate_one_day | |
pond = Habiat.new(2, 4, pong_organism_factory) | |
pond.simulate_one_day |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment