## 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
Add a Comment