Factory pattern

1612 ワード


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