`

原型模式(Prototype)

阅读更多
名字: 原型模式(Prototype)
意图: 用原型实例指定创建对象的种类, 并且通过拷贝这些原型创建新的对象.
动机: 替换较复杂的等级结构的工厂方法.

class ScreenPrototype
  attr_accessor :width, :height

  def initialize(width, height)
    self.width = width
    self.height = height
  end

  def display(name)
    puts "#{name} screen: #{self.width} x #{self.height}"
  end
end

class Client
  def self.run
    h = {}
    h[:normal] = ScreenPrototype.new(1024, 768)
    h[:tft_lcd] = ScreenPrototype.new(1280, 800)

    name = :normal
    new = h[name].clone
    new.display(name)
  end
end

Client.run




  • 大小: 30 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics