Skip to content

Commit d9943e2

Browse files
committed
Factory method Ruby
1 parent f98f314 commit d9943e2

22 files changed

+284
-0
lines changed

FactoryMethod/abstract_factory.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
class AbstractFactory
3+
4+
@@instance = nil
5+
6+
def self.register(instance)
7+
@@instance = instance
8+
end
9+
10+
def self.instance
11+
return @@instance
12+
end
13+
14+
end

FactoryMethod/client.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# ABSTRACT FACTORY EXERCISE
3+
#
4+
require_relative 'abstract_factory.rb'
5+
require_relative 'concrete_factory_normal.rb'
6+
require_relative 'concrete_factory_cool.rb'
7+
require_relative 'concrete_factory_deadly.rb'
8+
9+
class Client
10+
11+
def initialize(productType)
12+
13+
if productType == :normal
14+
ConcreteFactoryNormal.register
15+
elsif productType == :cool
16+
ConcreteFactoryCool.register
17+
elsif productType == :deadly
18+
ConcreteFactoryDeadly.register
19+
else
20+
raise "Unknown product type: #{productType}"
21+
end
22+
@factory_product_type = AbstractFactory.instance
23+
24+
25+
@prod_A = @factory_product_type.create_product_A
26+
@prod_B = @factory_product_type.create_product_B
27+
@prod_C = @factory_product_type.create_product_C
28+
@prod_D = @factory_product_type.create_product_D
29+
end
30+
31+
def foo
32+
@prod_A.do_your_stuff
33+
@prod_B.do_it
34+
@prod_C.perform
35+
@prod_D.do_cool_stuff
36+
end
37+
38+
end
39+
40+
41+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require_relative 'abstract_factory.rb'
2+
require_relative 'product_a_cool.rb'
3+
require_relative 'product_b_cool.rb'
4+
require_relative 'product_c_cool.rb'
5+
require_relative 'product_d_cool.rb'
6+
7+
class ConcreteFactoryCool < AbstractFactory
8+
9+
def self.register
10+
superclass.register(ConcreteFactoryCool.new)
11+
end
12+
13+
def create_product_A
14+
ProductACool.new
15+
end
16+
17+
def create_product_B
18+
ProductBCool.new
19+
end
20+
21+
def create_product_C
22+
ProductCCool.new
23+
end
24+
25+
def create_product_D
26+
ProductDCool.new
27+
end
28+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require_relative 'abstract_factory.rb'
2+
require_relative 'product_a_deadly.rb'
3+
require_relative 'product_b_deadly.rb'
4+
require_relative 'product_c_deadly.rb'
5+
require_relative 'product_d_deadly.rb'
6+
7+
class ConcreteFactoryDeadly < AbstractFactory
8+
9+
def self.register
10+
superclass.register(ConcreteFactoryDeadly.new)
11+
end
12+
13+
def create_product_A
14+
ProductADeadly.new
15+
end
16+
17+
def create_product_B
18+
ProductBDeadly.new
19+
end
20+
21+
def create_product_C
22+
ProductCDeadly.new
23+
end
24+
25+
def create_product_D
26+
ProductDDeadly.new
27+
end
28+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require_relative 'abstract_factory.rb'
2+
require_relative 'product_a_normal.rb'
3+
require_relative 'product_b_normal.rb'
4+
require_relative 'product_c_normal.rb'
5+
require_relative 'product_d_normal.rb'
6+
7+
class ConcreteFactoryNormal < AbstractFactory
8+
9+
def self.register
10+
superclass.register(ConcreteFactoryNormal.new)
11+
end
12+
13+
def create_product_A
14+
ProductANormal.new
15+
end
16+
17+
def create_product_B
18+
ProductBNormal.new
19+
end
20+
21+
def create_product_C
22+
ProductCNormal.new
23+
end
24+
25+
def create_product_D
26+
ProductDNormal.new
27+
end
28+
end

FactoryMethod/main.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require_relative 'client.rb'
2+
3+
my_client = Client.new(:cool)
4+
my_client.foo
5+

FactoryMethod/product_a.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
class ProductA
3+
4+
def do_your_stuff
5+
raise "Abstract method"
6+
end
7+
8+
end

FactoryMethod/product_a_cool.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_a.rb'
2+
3+
class ProductACool < ProductA
4+
5+
def do_your_stuff
6+
puts "I'm a #{self.class.name}, doing my stuff"
7+
end
8+
9+
end

FactoryMethod/product_a_deadly.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_a.rb'
2+
3+
class ProductADeadly < ProductA
4+
5+
def do_your_stuff
6+
puts "I'm a #{self.class.name}, doing my stuff"
7+
end
8+
9+
end

FactoryMethod/product_a_normal.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_a.rb'
2+
3+
class ProductANormal < ProductA
4+
5+
def do_your_stuff
6+
puts "I'm a #{self.class.name}, doing my stuff"
7+
end
8+
9+
end

FactoryMethod/product_b.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
class ProductB
3+
4+
def do_it
5+
raise "Abstract method"
6+
end
7+
8+
end

FactoryMethod/product_b_cool.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_b.rb'
2+
3+
class ProductBCool < ProductB
4+
5+
def do_it
6+
puts "I'm a #{self.class.name}, doing it"
7+
end
8+
9+
end

FactoryMethod/product_b_deadly.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_b.rb'
2+
3+
class ProductBDeadly < ProductB
4+
5+
def do_it
6+
puts "I'm a #{self.class.name}, doing it"
7+
end
8+
9+
end

FactoryMethod/product_b_normal.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_b.rb'
2+
3+
class ProductBNormal < ProductB
4+
5+
def do_it
6+
puts "I'm a #{self.class.name}, doing it"
7+
end
8+
9+
end

FactoryMethod/product_c.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
class ProductC
3+
4+
def perform
5+
raise "Abstract method"
6+
end
7+
8+
end

FactoryMethod/product_c_cool.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_c.rb'
2+
3+
class ProductCCool < ProductC
4+
5+
def perform
6+
puts "I'm a #{self.class.name}, performing"
7+
end
8+
9+
end

FactoryMethod/product_c_deadly.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_c.rb'
2+
3+
class ProductCDeadly < ProductC
4+
5+
def perform
6+
puts "I'm a #{self.class.name}, performing"
7+
end
8+
9+
end

FactoryMethod/product_c_normal.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_c.rb'
2+
3+
class ProductCNormal < ProductC
4+
5+
def perform
6+
puts "I'm a #{self.class.name}, performing"
7+
end
8+
9+
end

FactoryMethod/product_d.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
class ProductD
3+
4+
def do_cool_stuff
5+
raise "Abstract method"
6+
end
7+
8+
end

FactoryMethod/product_d_cool.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_d.rb'
2+
3+
class ProductDCool < ProductD
4+
5+
def do_cool_stuff
6+
puts "I'm a #{self.class.name}, doing a very cool stuff"
7+
end
8+
9+
end

FactoryMethod/product_d_deadly.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_d.rb'
2+
3+
class ProductDDeadly < ProductD
4+
5+
def do_cool_stuff
6+
puts "I'm a #{self.class.name}, doing a very cool stuff"
7+
end
8+
9+
end

FactoryMethod/product_d_normal.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'product_d.rb'
2+
3+
class ProductDNormal < ProductD
4+
5+
def do_cool_stuff
6+
puts "I'm a #{self.class.name}, doing a very cool stuff"
7+
end
8+
9+
end

0 commit comments

Comments
 (0)