Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
63Activity
0 of .
Results for:
No results containing your search query
P. 1
gof patterns in ruby

gof patterns in ruby

Ratings:

4.88

(17)
|Views: 8,472 |Likes:
Published by matthieutc
Implementation of the GoF patterns in Ruby
Implementation of the GoF patterns in Ruby

More info:

Published by: matthieutc on Oct 14, 2007
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

11/24/2014

pdf

text

original

 
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

Activity (63)

You've already reviewed this. Edit your review.
Petru Hincu liked this
1 thousand reads
1 hundred reads
Andrii Ponomarov liked this
Andrii Ponomarov liked this
Andrii Ponomarov liked this
Andrii Ponomarov liked this
Andrii Ponomarov liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->