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
'C# 디자인패턴' 카테고리의 다른 글
05# Factory Method 패턴2 Creational Patterns (0) | 2019.05.23 |
---|---|
04# Factory Method 패턴1 Creational Patterns (0) | 2019.05.23 |
03# 추상 팩토리(Abstract Factory) 패턴 Creational Patterns (0) | 2019.05.22 |
01# 싱글턴(Singleton) 패턴 (0) | 2016.04.18 |
00# 추상 팩토리(Abstract Factory) 패턴 (0) | 2016.04.18 |