strategy模式,对java有一定了解的JE友,都应该熟悉,这里不作过多的介绍.
下面是ruby版本的strategy模式体现,参考Head first design 设计模式中文版本第一章策略模式.由于ruby相对于java的不同,没有了接口的概念,也没有抽象类的概念,但是我闷可以变相的实现抽象和接口的应用,在ruby中
#
#鸭子抽象类
class Duck
attr_accessor :fly_behavior, :quack_behavior
def display
raise "this is a abstract method"
end
def perform_fly
if fly_behavior.is_a?(FlyBehavior)
fly_behavior.fly
end
end
def perform_quack
if quack_behavior.kind_of?(QuackBehavior)
quack_behavior.quack
end
end
end
#
#具体鸭子类
class MallarDuck < Duck
def initialize
@fly_behavior = FlyWithWings.new
@quack_behavior = Quack.new
end
def display
puts "I'm a real Mallard duck"
end
end
#
#具体鸭子类
class ModelDuck < Duck
def initialize
@fly_behavior = FlyNoWay.new
@quack_behavior = Quack.new
end
def display
puts "I'm a real Model duck"
end
end
#
#抽象类,具体方法让子类去实现
class FlyBehavior
def fly
raise "Abstract method call"
end
end
class QuackBehavior
def quack
raise "Abstract method call"
end
end
#
#实现抽象类行为的子类
class FlyWithWings < FlyBehavior
def fly
puts "I'm flying!!!"
end
end
class FlyNoWay < FlyBehavior
def fly
puts "I can't fly!!"
end
end
class FlyRocketPowered < FlyBehavior
def fly
puts "I'm flying with a rocket"
end
end
class Quack < QuackBehavior
def quack
puts "Quack!!!"
end
end
#
#测试方法
require 'benchmark'
puts "strategry pattern start!!!"
mallard = MallarDuck.new
mallard.display
mallard.perform_fly
mallard.perform_quack
modeld = ModelDuck.new
modeld.display
modeld.perform_fly
modeld.fly_behavior = FlyRocketPowered.new
modeld.perform_fly
输出---------------------
strategry pattern start!!!
I'm a real Mallard duck
I'm flying!!!
Quack!!!
I'm a real Model duck
I can't fly!!
I'm flying with a rocket
多多交流谢谢!
分享到:
- 2008-07-31 15:28
- 浏览 1702
- 评论(8)
- 论坛回复 / 浏览 (8 / 2888)
- 查看更多
相关推荐
Design Patterns in Ruby, Addison-wesley (2008). The Addison-Wesley Professional Ruby Series provides readers with practical, people-oriented, and in-depth information about applying the Ruby platform ...
Design Patterns in C# A Hands-on Guide with Real-World Examples的例子源码 注意: 所有例子没有经过上机运行验证。
Apply design patterns to modern C++ programming Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and ...
Design Patterns in Modern C++.pdf
Addison.Wesley.Design.Patterns.in.Ruby.Dec.2007 高清PDF英文版
The topic of Design Patterns sounds dry, academically constipated and, in all honesty, done to death in almost every programming language imaginable—including programming languages such as JavaScript...
GOF-设计模式-Design Patterns-英文原版-高清-有目录-有页码
关于设计模式的英文教材,包含详细例子,值得一读
design patterns elements of reusable object-oriented software.pdf 经典的书籍,比较完美的版本了
Design Patterns in Ruby Dec 2007.rar
Design Patterns Elements of Reusable Object-Oriented Software 英文版 (chm)
Practical-Python-Design-Patterns-Pythonic-Solutions-to-Common-Problems.pdf
Design Patterns: Elements of Reusable Object-Oriented Software.doc (无水印)
Implement the design patterns in real-world applications Select an alternative to these patterns by comparing their pros and cons Use Visual Studio Community Edition 2017 to write code and generate...
to apply the 23 Gang of Four (GoF) design patterns in Go and also learn about CSP concurrency patterns, the "killer feature" in Go that has helped Google develop software to maintain thousands of ...
• Design Patterns in Smalltalk MVC • Describing Design Patterns • The Catalog of Design Patterns • Organizing the Catalog • How Design Patterns Solve Design Problems • How to Select a ...
Design_Patterns Elements of Reusable Object-Oriented Software
Java codes to study patterns
★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、TemplatMethod、Iterator、Composite、State、Proxy。 ★第12章介绍...
Laracasts - design -patterns -in- php 课程资源