0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Builderパターン

Posted at

コンストラクタを分割したメソッドを持つクラスのことを「Builder」
Builderクラスのメソッドを生成手順に沿って呼び出すクラスを「Director」


class Human
  attr_accessor :race, :country
end

class HumanBuilder
  def initialize
    @human = Human.new
  end
  def add_rage(race)
    @human.race = race
  end
  def add_country(country)
    @human.country = country
  end
  def instance
    @human
  end
end

class JapaneseDirector
  def self.create(builder)
    builder.add_race('asian')
    builder.add_country('japan')
    builder.instance
  end
end

builder = HumanBuilder.new
japanese = JapaneseDirector.create(builder)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?