빌더 패턴이란 복합 객체의 생성 과정과 표현 방법을 분리하여 동일한 생성 절차에서 서로 다른 표현 결과를 만들 있게 하는 패턴이다. '''2 단어 요약 생성자 오버로딩''' == 구조 == 700px == == === 자바 === /** "Product" */ class Pizza private String dough ""; private String sauce ""; private String topping ""; public void setDough(String dough) this.dough dough; public void setSauce(String sauce) this.sauce sauce; public void topping) this.topping topping; /** "Abstract Builder" */ abstract class PizzaBuilder protected Pizza pizza; public Pizza getPizza() return pizza; public void pizza new Pizza(); public abstract void buildDough(); public abstract void buildSauce(); public abstract void buildTopping(); /** */ class extends PizzaBuilder public void buildDough() public void buildSauce() public void buildTopping() /** */ class extends PizzaBuilder public void buildDough() baked"); public void buildSauce() public void buildTopping() /** "Director" */ class Cook private PizzaBuilder pizzaBuilder; public void pizzaBuilder) pizzaBuilder; public Pizza getPizza() return public void /** given type of pizza being constructed. */ public class BuilderExample public static void main(String args) Cook cook new Cook(); PizzaBuilder new PizzaBuilder new Pizza pizza === C# === in C#. class Pizza string dough; string sauce; string topping; public Pizza() {} public void SetDough( string d){ dough d;} public void SetSauce( string s){ sauce s;} public void SetTopping( string t){ topping t;} //Abstract Builder abstract class PizzaBuilder protected Pizza pizza; public public Pizza GetPizza(){ return pizza; public void pizza new Pizza(); public abstract void BuildDough(); public abstract void BuildSauce(); public abstract void BuildTopping(); //Concrete Builder class PizzaBuilder public override void BuildDough() public override void BuildSauce() public override void BuildTopping() //Concrete Builder class PizzaBuilder public override void BuildDough() baked"); public override void BuildSauce() public override void BuildTopping() /** "Director" */ class Waiter private PizzaBuilder pizzaBuilder; public void SetPizzaBuilder (PizzaBuilder pb) pizzaBuilder pb; public Pizza GetPizza() return public void /** customer ordering pizza. */ class BuilderExample public static void Main(string args) Waiter waiter new Waiter(); PizzaBuilder new PizzaBuilder new ); Pizza pizza === C++ === // Implementation in C++. #include #include #include // Product class Pizza private: std::string dough; std::string sauce; std::string topping; public: Pizza() ~Pizza() void SetDough(const std::string& d) dough d; }; void SetSauce(const std::string& s) sauce s; }; void std::string& t) topping t; void ShowPizza() std::cout pizza; public: PizzaBuilder() {} virtual ~PizzaBuilder() {} std::auto_ptr GetPizza() return pizza; void pizza.reset (new Pizza); virtual void buildDough()=0; virtual void buildSauce()=0; virtual void }; // ConcreteBuilder class public PizzaBuilder public: PizzaBuilder() {} void buildDough() void buildSauce() void buildTopping() and pineapple"); }; // ConcreteBuilder class public PizzaBuilder public: PizzaBuilder() {} {} void buildDough() baked"); void buildSauce() void buildTopping() and salami"); }; // Director class Waiter private: PizzaBuilder* pizzaBuilder; public: Waiter() {} ~Waiter() void b) pizzaBuilder b; std::auto_ptr GetPizza() return void }; // customer ordering pizza. int main() Waiter waiter; std::auto_ptr pizza pizza return EXIT_SUCCESS; === 비주얼 프롤로그 === Product interface pizza predicates setDough (string Dough). setSauce (string Sauce). setTopping (string Topping). end interface pizza class pizza pizza end class pizza implement pizza facts dough string := "". sauce string := "". topping string := "". clauses setDough(Dough) :- dough := Dough. clauses setSauce(Sauce) :- sauce := Sauce. clauses :- topping := Topping. end implement pizza Abstract Builder interface pizzaBuilder predicates getPizza () -> pizza Pizza. (). predicates buildDough (). buildSauce (). buildTopping (). end interface pizzaBuilder Visual Prolog does not support abstract classes, but we can create support class instead: interface predicates from pizzaBuilder getPizza, end interface class end class implement facts pizza pizza := erroneous. clauses getPizza() pizza. clauses :- pizza := pizza::new(). end implement ConcreteBuilder #1 class pizzaBuilder end class implement inherits clauses buildDough() :- clauses buildSauce() :- clauses buildTopping() :- end implement ConcreteBuilder #2 class pizzaBuilder end class implement inherits clauses buildDough() :- baked"). clauses buildSauce() :- clauses buildTopping() :- end implement Director interface waiter predicates setPizzaBuilder (pizzaBuilder PizzaBuilder). getPizza () -> pizza Pizza. predicates constructPizza (). end interface waiter class waiter waiter end class waiter implement waiter facts pizzaBuilder pizzaBuilder := erroneous. clauses :- pizzaBuilder := PizzaBuilder. clauses getPizza() clauses :- end implement waiter customer ordering pizza. goal Waiter waiter::new(), Pizza === === ## Product package pizza; sub new return bless dough => undef, sauce => undef, topping => undef }, shift; sub set_dough my( $self, $dough @_; $self->{dough} $dough; sub set_sauce my( $self, $sauce @_; $self->{sauce} $sauce; sub set_topping my( $self, $topping @_; $topping; 1; ## Abstract builder package pizza_builder; sub new return bless pizza => undef }, shift; sub get_pizza my( $self @_; return $self->{pizza}; sub my( $self @_; $self->{pizza} pizza->new; This is what an abstract method could look like in perl... sub build_dough croak("This method must be overridden."); sub build_sauce croak("This method must be overridden."); sub build_topping croak("This method must be overridden."); 1; ## Concrete builder package use base qw{ pizza_builder }; sub build_dough my( $self @_; sub build_sauce my( $self @_; sub build_topping my( $self @_; 1; ## Concrete builder package use base qw{ pizza_builder }; sub build_dough my( $self @_; baked"); sub build_sauce my( $self @_; sub build_topping my( $self @_; 1; ## Director package waiter; sub new return bless pizza_builder => undef }, shift; sub my( $self, $builder @_; $builder; sub get_pizza my( $self @_; return sub construct_pizza my( $self @_; 1; ## Lets order pizza (client of package main my $waiter waiter->new; my $hawaiian_pb my $spicy_pb $hawaiian_pb ); my $pizza print "Serving nice pizza with:\n"; for (keys %$pizza) print $pizza->{$_} $_\n"; 1; === PHP === /** Product **/ class Pizza{ private $dough; private $sauce; private $topping; public function $this->dough $dough; public function $this->sauce $sauce; public function $this->topping $topping; /** Abstract builder **/ abstract class PizzaBuilder{ protected $pizza; public function __construct(){ $this->pizza new Pizza(); public function getPizza(){ return $this->pizza; abstract function buildDough(); abstract function buildSauce(); abstract function buildTopping(); /** Concrete builder **/ class SpicyPizza extends PizzaBuilder{ public function buildDough(){ public function buildSauce(){ public function buildTopping(){ /** Director **/ class Chef{ private $pizza_builder; public function $pizza_builder; public function cookPizza(){ public function getPizza(){ return //Customer orders Pizza. $chef new Chef(); $order new SpicyPizza(); $pizza 분류:소프트웨어 디자인 패턴