Permalink
Browse files

new files

  • Loading branch information...
1 parent 756b3a0 commit 886280ef25fb59574559897d421074d7b9da7b94 @shvets committed Jul 29, 2010
Showing with 0 additions and 355 deletions.
  1. +0 −86 creational/builder.rb
  2. +0 −65 creational/singleton-corrected.rb
  3. +0 −38 creational/singleton.rb
  4. +0 −108 structural/decorator.rb
  5. +0 −58 structural/proxy.rb
View
@@ -1,86 +0,0 @@
-# builder.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
-
- 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(builder)
- builder.build_part1
- builder.build_part2
- builder.build_part3
- end
-
- def construct_with_magic(builder)
- builder.build_part1_and_part2_and_part3
- end
-end
-
-# 4. test
-
-director = Director.new
-
-director.construct(ComputerBuilder.new)
-director.construct(TableBuilder.new)
-
-puts "with magic:"
-director.construct_with_magic(ComputerBuilder.new)
@@ -1,65 +0,0 @@
-# singleton-corrected.rb
-
-# Trying to replcae Singleton pattern with interface inheritance and dependency injection.
-# Now we can mock the singleton.
-
-# - Reduce hidden coupling;
-# - Allow testability;
-# - Allow subclassing;
-# - Make construction and use flexible;
-
-# 1. Rediefine singleton.
-
-class Singleton
- def operation
- end
-end
-
-class MySingleton < Singleton
- def operation
- puts "operation"
- end
-end
-
-# 2. 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
-
-# 3. Mock object for the singleton and the test
-
-class MockSingleton < Singleton
-
- def operation
- puts "mock operation"
- end
-
-end
-
-class SingletonTest
- def test_singleton
- singleton = MockSingleton.new
-
- singleton_user = SingletonUser.new(singleton)
-
- singleton_user.do_something
-
- # assertions
- end
-end
-
-# 4. test
-
-tester = SingletonTest.new
-
-tester.test_singleton
-
-
@@ -1,38 +0,0 @@
-# singleton.rb
-
-# Ensure a class only has one instance and provide a global point of access to it.
-
-# Note: try to avoid singleton (see singleton-corrected.rb).
-# Major question: how to test/mock the singleton?
-
-class MySingleton1
- @@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
-
-require 'singleton'
-
-class MySingleton2
- include Singleton
-end
-
-# MySingleton1.new # error
-
-puts "singleton1: " + MySingleton1.instance.to_s
-puts "singleton1: " + MySingleton1.instance.to_s
-
-puts "singleton2: " + MySingleton2.instance.to_s
-puts "singleton2: " + MySingleton2.instance.to_s
@@ -1,108 +0,0 @@
-# decorator.rb
-
-# Attaches additional responsibilities to an object dynamically. Provides
-# a flexible alternative to subclassing for extending functionality.
-
-# 1. type interface
-
-class Component
- def operation
- end
-end
-
-# 2. type implementation
-
-class ConcreteComponent < Component
- def operation
- puts "my component"
- end
-end
-
-# 3. traditional solution based on inheritance
-
-class TraditionalComponent < ConcreteComponent
- def pre_decoration
- puts "my component pre decoration"
- end
-
- def post_decoration
- puts "my component post decoration"
- end
-
- def operation
- pre_decoration
-
- super
-
- post_decoration
- end
-end
-
-# 4. solution based on decorator
-
-class Decorator < Component
- def initialize(component)
- @component = component
- end
-
- def pre_decoration
- puts "my component pre decoration"
- end
-
- def post_decoration
- puts "my component post decoration"
- end
-
- def operation
- pre_decoration
-
- @component.operation
-
- post_decoration
- end
-end
-
-# 5.
-
-require 'forwardable'
-
-class ForwardableDecorator
- extend Forwardable
-
- def_delegators :@component, :operation
-
- def initialize(component)
- @component = component
- end
-end
-
-# 6. test
-
-component1 = TraditionalComponent.new
-component1.operation
-puts '-------'
-
-component2 = Decorator.new(ConcreteComponent.new)
-component2.operation
-puts '-------'
-
-component3 = ForwardableDecorator.new(ConcreteComponent.new)
-component3.operation
-puts '-------'
-
-component4 = ConcreteComponent.new
-
-class << component4
- alias old_operation operation
-
- def operation
- puts "my component pre decoration"
-
- old_operation
-
- puts "my component post decoration"
- end
-end
-
-component4.operation
-puts '-------'
View
@@ -1,58 +0,0 @@
-# proxy.bsh
-
-# Provides a surrogate or placeholder for another object to control access to it.
-
-# 1. type interface
-
-class Subject
- def process
- end
-end
-
-# 2. type implementation
-
-class MySubject < Subject
- def process
- puts "my process"
- end
-end
-
-# 3.type proxy implementation
-
-class SubjectProxy < Subject
-
- def initialize(subject)
- @subject = subject
- end
-
- def process
- @subject.process
- end
-end
-
-class DynamicSubjectProxy
-
- def initialize(subject)
- @subject = subject
- end
-
-# def process
-# @subject.process
-# end
- def method_missing(name, *args)
- puts("Delegating #{name} message to subject.")
-
- @subject.send(name, *args)
- end
-end
-
-
-# 4. test
-
-proxy = SubjectProxy.new(MySubject.new)
-
-proxy.process
-
-dynamic_proxy = DynamicSubjectProxy.new(MySubject.new)
-
-dynamic_proxy.process

0 comments on commit 886280e

Please sign in to comment.