返回顶部

收藏

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

发表评论