返回顶部

收藏

Ruby策略模式

更多
class Hero
  def kill(bug, skill)
    skill.do(bug)
  end
end

 class Skill
   def do(bug="bug")
     raise "This is abstract method"
   end
 end

 # 雷电
 class Thunder < Skill
   def do(bug="bug")
    puts 'delay for 3 second'
     puts 'thunder 2 times'
     puts 'thunder continuely'
     puts "#{bug} HP -1000"
   end
 end
  # 地震
 class Earthquake < Skill
  def do(bug)
     puts "#{bug} HP -500"
     puts 'earthquick for 2 second'
    puts "#{bug} black out for 5 second"
   end
 end

 # 旋风
 class Whirlwind < Skill
   def do(bug)
     puts 'scope 100 tornado 5 second and move'
    puts "#{bug} HP continuely -150/per second "
   end
 end

 class ChristianStart<Skill
   def do(bug)
     puts "all enemy die!"
     puts "#{bug} die for ever!"
   end
 end

 puts "\r\n"
 puts 'beat pig'
 puts "------------"
 Hero.new.kill('pig', Thunder.new)

 puts "\r\n"
puts 'beat white'
 puts "------------"
 Hero.new.kill('white', Earthquake.new)

 puts "\r\n"
 puts 'beat ox'
 puts "------------"
 Hero.new.kill('ox', Whirlwind.new)
  puts "\r\n"
 puts 'beat all'
 puts "------------"
 Hero.new.kill("all enemies", ChristianStart.new)

标签:ruby

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 博主 发表 2018-07-14 09:46:00 ruby - 一些不错的gem工具,记录一下
  2. 博主 发表 2009-04-06 16:00:00 使用SVN+SSH同步Ruby On Rails代码的脚本
  3. 博主 发表 2014-01-08 14:38:53 Ruby on Rails 中使用 Markdown 语法,及代码高亮
  4. 博主 发表 2014-01-13 10:23:02 用 Nginx 和 Unicorn 部署 Ruby on Rails 应用
  5. 博主 发表 2018-06-19 00:26:00 ruby/rails - 根据浏览器的语言,来自动识别
  6. Alex Wood 发表 2014-10-15 13:06:29 Deploying Ruby on Rails Applications to AWS OpsWor
  7. Alex Wood 发表 2014-10-22 12:04:19 Caching the Rails Asset Pipeline with Amazon Cloud
  8. Trevor Rowe 发表 2014-11-18 19:04:17 Using Resources
  9. Trevor Rowe 发表 2014-11-20 19:04:22 Waiters
  10. Trevor Rowe 发表 2014-11-20 19:04:22 Waiters
  11. Trevor Rowe 发表 2014-11-26 19:04:20 Client Response Stubs
  12. Alex Wood 发表 2014-12-02 17:32:13 AWS re:Invent 2014 Ruby Recap

发表评论