[quote]
http://www.cnblogs.com/7in10/articles/587233.html
[/quote]
[img]http://dl.iteye.com/upload/attachment/168007/905c876b-bef3-3824-9bd7-d451c8eb845c.jpg[/img]
http://www.cnblogs.com/7in10/articles/587233.html
[/quote]
[img]http://dl.iteye.com/upload/attachment/168007/905c876b-bef3-3824-9bd7-d451c8eb845c.jpg[/img]
class Environment
attr_accessor :constipation
def initerlizer
end
def do
constipation.do
end
end
class Constipation
def initerlizer
end
def do ; end
end
class SeeMovie < Constipation
def do
puts "看泰坦尼克吧!"
end
end
class EatSnack < Constipation
def do
puts "吃火锅!"
end
end
class GotoSea < Constipation
def do
puts "沙滩漫步!"
end
end
env = Environment.new
puts "早上了"
env.constipation = SeeMovie.new
env.do
puts "中午了"
env.constipation = EatSnack.new
env.do
puts "晚上了"
env.constipation = GotoSea.new
env.do
本文通过一个具体的 Ruby 代码示例展示了如何使用设计模式来组织一天中的活动,包括看电影、吃零食和海边散步等场景。该示例运用了环境类 (Environment) 和行为类 (如 SeeMovie, EatSnack, GotoSea) 的组合来动态改变对象的行为。
2425

被折叠的 条评论
为什么被折叠?



