Factory pattern

module Hair
  def render
    raise "Abstract method"
  end
end

module Eye
  def render
    raise "Abstract method"
  end
end

class LongHair
  include Hair

  def render
    puts '长头发'
  end
end

class ShortHair
  include Hair

  def render
    puts '短头发'
  end
end

class ShiningEye
  include Eye

  def render
    puts '迷人的眼睛'
  end
end

class CrossEye
  include Eye

  def render
    puts '斗鸡眼'
  end
end

module Factory
  def create_eye
    raise "Abstract method"
  end

  def create_hair
    raise "Abstract method"
  end
end

class BeautyFactory
  include Factory

  def initialize
    puts "美女是: "
  end

  def create_eye
    ShiningEye.new.render
  end

  def create_hair
    LongHair.new.render
  end
end

class UglyFactory
  include Factory

  def initialize
    puts "丑女是: "
  end

  def create_eye
    CrossEye.new.render
  end

  def create_hair
    ShortHair.new.render
  end
end

# console
beauty = BeautyFactory.new
beauty.create_hair
beauty.create_eye

ugly = UglyFactory.new
ugly.create_hair
ugly.create_eye

你可能感兴趣的:(Pattern)