没有合适的资源?快使用搜索试试~ 我知道了~
gof patterns in ruby
需积分: 0 8 下载量 189 浏览量
2008-03-22
03:58:11
上传
评论
收藏 360KB PDF 举报
温馨提示
试读
33页
看看ruby中的设计模式吧
资源详情
资源评论
资源推荐
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 Ruby
1/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
def create_button
raise NotImplementedError, "You should implement this method"
end
end
class Win95Factory
include MyAbstractFactory
def create_button
puts "I'm Win95"
"win95button"
end
end
class MotifFactory
include MyAbstractFactory
def create_button
puts "I'm Motif"
"motifbutton"
end
end
class GTKFactory
include MyAbstractFactory
def create_button
puts "I'm GTK"
"gtkbutton"
end
def extra
puts "I'm enhanced"
end
end
class LookAndFeelManager
@@types2classes = {
:motif => [MotifFactory,nil],
:gtk => [GTKFactory,nil],
:win95 => [Win95Factory,nil]
}
def self.create type
if !@@types2classes.include? type
raise NotImplementedError, "I know nothing about type: #{type}"
end
GoF patterns in Ruby
2/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]
end
end
end
if __FILE__ == $0
factory = LookAndFeelManager.create :gtk
puts factory.create_button
factory.extra if factory.respond_to? :extra
end
Output
------
instantiating new factory
I'm GTK
gtkbutton
I'm enhanced
GoF patterns in Ruby
3/33
#
# The GoF Builder pattern
# written by Matthieu Tanguay-Carel
#
# The Director class declares the creation process.
# The Builder classes are the concrete builders.
# The builders are free to implement a method or not, and can be
# customised at will by the client.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
class Director
def initialize
@process = [:create_header, :create_body, :create_footer]
end
def build builder
@process.inject("") {|acc, method|
acc += builder.send method if builder.respond_to? method
acc
}
end
end
class HTMLBuilder
def initialize title
@title = title
end
def create_header
"<html><title>#{@title}</title>"
end
def create_body
"<body>fig leave</body>"
end
def create_footer
"</html>"
end
end
class XMLBuilder
def create_header
"<?xml version='1.0' charset='utf-8'?>"
end
def create_body
"<root>welcome</root>"
end
end
if __FILE__ == $0
director = Director.new
html_builder = HTMLBuilder.new 'xml sucks'
puts director.build(html_builder)
GoF patterns in Ruby
4/33
xml_builder = XMLBuilder.new
puts director.build(xml_builder)
end
Output
------
<html><title>xml sucks</title><body>fig leave</body></html>
<?xml version='1.0' charset='utf-8'?><root>welcome</root>
GoF patterns in Ruby
5/33
剩余32页未读,继续阅读
yautah
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0