/  33
 
GoF patterns in Ruby
Matthieu Tanguay-Carel - 2007 
Creational Patterns
Abstract Factory
Builder
Factory Method
# can be deduced from Abstract Factory
Prototype
Singleton
# available in standard lib (doc in singleton.rb)
Structural Patterns
Adapter
Bridge
# can be deduced from Abstract Factory
Composite
Decorator
Facade
# boring and trivial
Flyweight
Proxy
Behavioral Patterns
Chain of Responsibility
Command
Interpreter
# skipped
Iterator
# built-in (module Enumerable)
Mediator
# skipped
Memento
Observer
# built-in (doc in observer.rb)
State
# nice implementation by maurice codik
Strategy
Template Method
# the simplest is the block yielded to
Visitor
GoF patterns in Ruby1/33
 
## The GoF Abstract Factory pattern# written by Matthieu Tanguay-Carel## Factories behave in effect like singletons.# Extra functionality can be tested for with "Object#respond_to? :extra"# if needed (See GTKFactory).## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 module
MyAbstractFactory
defcreate_buttonraise
NotImplementedError,"You should implement this method"
endendclassWin95Factory
include MyAbstractFactory
defcreate_button puts
"I'm Win95""win95button"
endendclassMotifFactory
include MyAbstractFactory
defcreate_button puts
"I'm Motif""motifbutton"
endendclassGTKFactory
include MyAbstractFactory
defcreate_button puts
"I'm GTK""gtkbutton"
enddefextra puts
"I'm enhanced"
endendclassLookAndFeelManager
@@types2classes=
{
:motif=>
[
MotifFactory,
nil]
,:gtk=>
[
GTKFactory,
nil]
,:win95=>
[
Win95Factory,
nil]}defself.create
type
if
!@@types2classes.include? type
raise
NotImplementedError,"I know nothing about type:#{type}"
end
GoF patterns in Ruby2/33
 
factory_and_instance=@@types2classes
[
type
]if
factory_and_instance
[
1
]
.
nil
?
 puts
'instantiating new factory'factory_and_instance
[
1
]
=factory_and_instance
[
0
]
.new#mutex this
else puts
'returning already instantiated factory'factory_and_instance
[
1
]endendendif
__FILE__==
$0
factory=LookAndFeelManager.create:gtk
 puts
factory.create_buttonfactory.extra
if
factory.respond_to?:extra
endOutput------
instantiating new factoryI'm GTKgtkbuttonI'm enhanced
GoF patterns in Ruby3/33

Share & Embed

More from this user

Add a Comment

Characters: ...