Permalink
Browse files

new files

  • Loading branch information...
1 parent 19cd383 commit 756b3a06dfe124e30afd23e2f8b6d3ec00d633c5 @shvets committed Jul 29, 2010
View
@@ -17,8 +17,8 @@ def operation()
# 2. test
-command_user = CommandUser.new {
+command_user = CommandUser.new do
puts "command1"
-}
+end
command_user.operation
@@ -3,8 +3,8 @@
# Provides an interface for creationg families of related or dependent objects without specifying
# their concrete class.
#
-# Provides one level of interface higher than the factory pattern. It is used to return one
-# of several factories.
+# Provides one level of interface higher than the Factory Method pattern.
+# It is used to return one of the several factories.
# 1. common interface for type and it's specializations
View
@@ -0,0 +1,66 @@
+# builder1.rb
+
+# Separates the construction of a complex object from its representation so that the same
+# construction process can create different representations.
+
+# 1. type builder interface
+
+class ProductBuilder
+ def build_part1
+ end
+
+ def build_part2
+ end
+
+ def build_part3
+ end
+
+end
+
+# 2. type builder implementations
+
+class ComputerBuilder < ProductBuilder
+ def build_part1
+ puts "Building part1: motherboard"
+ end
+
+ def build_part2
+ puts "Building part2: CPU"
+ end
+
+ def build_part3
+ puts "Building part3: display"
+ end
+end
+
+class TableBuilder < ProductBuilder
+ def build_part1
+ puts "Building part1: legs"
+ end
+
+ def build_part2
+ puts "Building part2: top"
+ end
+
+ def build_part3
+ puts "Building part3: mounting"
+ end
+end
+
+# 3. director
+
+class Director
+ def construct(builder)
+ builder.build_part1
+ builder.build_part2
+ builder.build_part3
+ end
+end
+
+# 4. test
+
+director = Director.new
+
+director.construct(ComputerBuilder.new)
+director.construct(TableBuilder.new)
+
View
@@ -0,0 +1,77 @@
+# builder2.rb
+
+# This example uses ruby "method_missing" to implement so-called "magic method"
+
+# 1. type builder interface
+
+class ProductBuilder
+ def build_part1
+ end
+
+ def build_part2
+ end
+
+ def build_part3
+ end
+
+ def method_missing(name, *args)
+ words = name.to_s.split("_")
+
+ return super(name, *args) unless words.shift == 'build'
+
+ words.each do |word|
+ next if word == 'and'
+
+ build_part1 if word == 'part1'
+ build_part2 if word == 'part2'
+ build_part3 if word == 'part3'
+ end
+ end
+
+end
+
+# 2. type builder implementations
+
+class ComputerBuilder < ProductBuilder
+ def build_part1
+ puts "Building part1: motherboard"
+ end
+
+ def build_part2
+ puts "Building part2: CPU"
+ end
+
+ def build_part3
+ puts "Building part3: display"
+ end
+end
+
+class TableBuilder < ProductBuilder
+ def build_part1
+ puts "Building part1: legs"
+ end
+
+ def build_part2
+ puts "Building part2: top"
+ end
+
+ def build_part3
+ puts "Building part3: mounting"
+ end
+end
+
+# 3. director
+
+class Director
+ def construct_with_magic(builder)
+ builder.build_part1_and_part2_and_part3
+ end
+end
+
+# 4. test
+
+director = Director.new
+
+puts "with magic:"
+director.construct_with_magic(ComputerBuilder.new)
+director.construct_with_magic(TableBuilder.new)
View
@@ -0,0 +1,27 @@
+# singleton1.rb
+
+# Ensure a class only has one instance and provide a global point of access to it.
+
+class MySingleton
+ @@instance = nil
+
+ def self.instance()
+ if @@instance == nil
+ public_class_method :new # enables instance creation for this class
+
+ @@instance = new() unless @@instance
+
+ private_class_method :new # disable instance creation for this class
+ end
+
+ @@instance
+ end
+
+ private_class_method :new # disable instance creation for this class
+end
+
+
+# MySingleton1.new # error
+
+puts "singleton: " + MySingleton.instance.to_s
+puts "singleton: " + MySingleton.instance.to_s
View
@@ -0,0 +1,12 @@
+# singleton2.rb
+
+# This example does the same qith the help of 'singleton' library.
+
+require 'singleton'
+
+class MySingleton
+ include Singleton
+end
+
+puts "singleton: " + MySingleton.instance.to_s
+puts "singleton: " + MySingleton.instance.to_s
View
@@ -0,0 +1,64 @@
+# singleton3.rb
+
+# Usually you have to avoid using Singeton, because it's not possible to mock (and consequently test) it.
+#
+# This example is trying to replace Singleton pattern with interface inheritance and dependency injection.
+# Now we can mock the singleton.
+
+# 1. Define singleton.
+
+class Singleton
+ def operation
+ end
+end
+
+# 2. Singleton implementation.
+
+class MySingleton < Singleton
+ def operation
+ puts "operation"
+ end
+end
+
+# 3. Class that uses the singleton object (we should be able to set it up from outside).
+
+class SingletonUser
+ def initialize(singleton)
+ @singleton = singleton
+ end
+
+ def do_something
+ @singleton.operation
+ end
+
+end
+
+# 4. Create mock object for the singleton.
+
+class MockSingleton < Singleton
+
+ def operation
+ puts "mock operation"
+ end
+
+end
+
+
+# 5. test
+
+class SingletonTest
+ def test_singleton
+ singleton = MockSingleton.new
+
+ singleton_user = SingletonUser.new(singleton)
+
+ singleton_user.do_something
+
+ # assertions
+ end
+end
+
+tester = SingletonTest.new
+
+tester.test_singleton
+
Oops, something went wrong.

0 comments on commit 756b3a0

Please sign in to comment.