Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added the Factory pattern files

  • Loading branch information...
commit 24e4e54468ccdb468854cfa032be561023a991ed 1 parent fd60f2d
Attila Domokos authored
View
19 Factory/algea.rb
@@ -0,0 +1,19 @@
+class Algea
+ def initialize(name)
+ @name = name
+ end
+
+ def grow
+ puts("The Algea #{@name} soaks up the sun and grows")
+ 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
View
17 Factory/duck.rb
@@ -0,0 +1,17 @@
+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
+end
View
17 Factory/frog.rb
@@ -0,0 +1,17 @@
+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
+end
View
20 Factory/organism_factory.rb
@@ -0,0 +1,20 @@
+require 'tree'
+require 'tiger'
+
+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
+
+jungle_organism_factory = OrganismFactory.new(Tree, Tiger)
+
View
25 Factory/pond.rb
@@ -0,0 +1,25 @@
+require 'duck'
+require 'frog'
+
+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_one_day
+ @plants.each {|plant| plant.grow}
+ @animals.each {|animal| animal.speak}
+ @animals.each {|animal| animal.eat}
+ @animals.each {|animal| animal.sleep}
+ end
+end
View
45 Factory/pond_factory.rb
@@ -0,0 +1,45 @@
+require 'pond'
+require 'algea'
+
+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 DuckWaterLilyPond < Pond
+ def new_animal(name)
+ Duck.new(name)
+ end
+
+ def new_plant(name)
+ Waterlily.new(name)
+ end
+end
+
+class FrogAlgeaPond < Pond
+ def new_animal(name)
+ Frog.new(name)
+ end
+
+ def new_plant(name)
+ Algea.new(name)
+ end
+end
+
+=begin
+pond = FrogPond.new(3)
+pond.simulate_one_day
+
+pond = DuckPond.new(3)
+pond.simulate_one_day
+=end
+
+pond = FrogAlgeaPond.new(3, 2)
+pond.simulate_one_day
View
17 Factory/tiger.rb
@@ -0,0 +1,17 @@
+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
View
9 Factory/tree.rb
@@ -0,0 +1,9 @@
+class Tree
+ def initialize(name)
+ @name = name
+ end
+
+ def grow
+ puts("The tree #{@name} grows tall")
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.