본문 바로가기
C# 디자인패턴

02# Builder 패턴 Creational Patterns

by NaHyungMin 2019. 5. 22.

 

using System;

using DesignPatterns.Patterns.Builder;

 

namespace DesignPatterns

{

    class Program

    {

        static void Main(string[] args)

        {

            BuilderExample();

 

            //Console.WriteLine("Hello World!");

        }

 

        static void BuilderExample()

        {

            //Director 생성, Builder 데이터 입력자.

            Director director = new Director();

            

            Builder b1 = new ConcreteBuilder1();

            Builder b2 = new ConcreteBuilder2();

 

            //Product 데이터 입력

            director.Construct(b1);

            //Product 데이터 호출

            Product p1 = b1.GetResult();

            p1.Show();

 

            director.Construct(b2);

            Product p2 = b2.GetResult();

            p2.Show();

 

            Console.ReadKey();

        }

    }

}

 

Program.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Patterns.Builder

{

    public abstract class Builder

    {

        public abstract Product Product { get; }

        public abstract void BuildPartA();

        public abstract void BuildPartB();

        public abstract Product GetResult();

    }

}

 

Builder.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Patterns.Builder

{

    public class Director

    {

        public void Construct(Builder builder)

        {

            builder.BuildPartA();

            builder.BuildPartB();

        }

    }

}

 

Director.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Patterns.Builder

{

    public class Product

    {

        private List<string> parts = new List<string>();

 

        public void Add(string part)

        {

            parts.Add(part);

        }

 

        public void Show()

        {

            Console.WriteLine("Product Parts");

 

            foreach(string part in parts)

            {

                Console.WriteLine(part);

            }

        }

    }

}

 

Product.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Patterns.Builder

{

    public class ConcreteBuilder1 : Builder

    {

        private Product product;

 

        public override Product Product

        {

            get => product ?? (product = new Product());

        }

 

        public override void BuildPartA()

        {

            Product.Add("PartA");

        }

 

        public override void BuildPartB()

        {

            Product.Add("PartB");

        }

 

        public override Product GetResult()

        {

            return Product;

        }

    }

}

 

ConcreteBuilder1.cs

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace DesignPatterns.Patterns.Builder

{

    public class ConcreteBuilder2 : Builder

    {

        private Product product;

 

        public override Product Product

        {

            get => product ?? (product = new Product());

        }

 

        public override void BuildPartA()

        {

            Product.Add("PartX");

        }

 

        public override void BuildPartB()

        {

            Product.Add("PartY");

        }

 

        public override Product GetResult()

        {

            return Product;

        }

    }

}

 

ConcreteBuilder2.cs

 

참고 사이트

https://www.dofactory.com/net/builder-design-pattern

 

Builder .NET Design Pattern in C# and VB - dofactory.com

Separate the construction of a complex object from its representation so that the same construction process can create different representations.     The classes and objects participating in this pattern are: This structural code demonstrates the Builder p

www.dofactory.com