Work & Programming/System Design

[Creational Pattern] - Builder

자전거통학 2024. 5. 15. 10:10

https://refactoring.guru/design-patterns/builder

 

Builder

Say you have a constructor with ten optional parameters. Calling such a beast is very inconvenient; therefore, you overload the constructor and create several shorter versions with fewer parameters. These constructors still refer to the main one, passing s

refactoring.guru

 

특정 단계가 필요한 절차대로 생성되는 어떤 대상들에 대해 각 상황에 걸맞는 생성코드 제작. 

어떤 특정 제작단계가 반드시 정의되어야 한다.

예시 

Pizza Builder 

Coffee Builder 

House Builder 

Dialog Builder

 

 

예제 코드

더보기
public interface IDialogBuilder
{
    void BuildTitle();
    void BuildContent();
    void BuildButtons();
}

public class Dialog
{
}

public abstract class ADialogBuilder : IDialogBuilder
{
    protected Dialog _dlgBase = null;

    public virtual void Init() { _dlgBase = new Dialog(); }
    public abstract void BuildTitle();
    public abstract void BuildContent();
    public abstract void BuildButtons();
}

public class MessageDialogBuilder : ADialogBuilder
{
    public override void BuildTitle() {}
    public override void BuildContent() {}
    public override void BuildButtons() {}
}
public class WarningDialogBuilder : ADialogBuilder
{
    public override void BuildTitle() {}
    public override void BuildContent() {}
    public override void BuildButtons() {}
}



public class BuilderDemo
{
    public void Main()
    {
        ADialogBuilder dialog = new MessageDialogBuilder();
        dialog.Init();
        dialog.BuildTitle();
        dialog.BuildContent();
        dialog.BuildButtons();
    }
}