`

泡妞之策略模式

阅读更多
引用

http://www.cnblogs.com/7in10/articles/587233.html




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

  • 大小: 36 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics