[Design Pattern] 6. ๋นŒ๋” ํŒจํ„ด(builder)

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: ,




builderํŒจํ„ด

builder






๋นŒ๋” ํŒจํ„ด (builder)

image

์ƒŒ๋“œ์œ„์น˜

  • ๋ณต์žกํ•œ ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๊ณผ์ •์„ ๋‹จ๊ณ„๋ณ„ ๋ถ„๋ฆฌ
  • ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ํ‘œํ˜„์„ ๋ถ„๋ฆฌ
  • ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ํ‘œํ˜„์„ ๊ฐ€์ง„ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ
  • ๋ณต์žกํ•œ ๊ฒŒ์ž„์˜ค๋ธŒ์ ํŠธ, ๋‚œ์ด๋„์กฐ์ ˆํ•  ๋•Œ ์‚ฌ์šฉ(๋งตํ˜•ํƒœ, ๋ชน์ˆ˜)
builder ์ฝ”๋“œ๋ณด๊ธฐ
// ๋นŒ๋” ์ธํ„ฐํŽ˜์ด์Šค
public interface IBuilder
{
    void BuildPartA();
    void BuildPartB();
    void BuildPartC();
    GameObject GetResult();
}

// ๋นŒ๋” ํด๋ž˜์Šค
public class ConcreteBuilder : IBuilder
{
    private GameObject gameObject = new GameObject();

    public void BuildPartA()
    {
        // ๊ฐ์ฒด์˜ ์ผ๋ถ€๋ถ„ A๋ฅผ ๊ตฌ์ถ• (์˜ˆ: ์บ๋ฆญํ„ฐ ๋ชจ๋ธ ์ถ”๊ฐ€)
    }

    public void BuildPartB()
    {
        // ๊ฐ์ฒด์˜ ์ผ๋ถ€๋ถ„ B๋ฅผ ๊ตฌ์ถ• (์˜ˆ: ์บ๋ฆญํ„ฐ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์„ค์ •)
    }

    public void BuildPartC()
    {
        // ๊ฐ์ฒด์˜ ์ผ๋ถ€๋ถ„ C๋ฅผ ๊ตฌ์ถ• (์˜ˆ: ์บ๋ฆญํ„ฐ ๋Šฅ๋ ฅ์น˜ ์„ค์ •)
    }

    public GameObject GetResult()
    {
        return gameObject;
    }
}

// ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ ํด๋ž˜์Šค
public class GameObject
{
    // ๊ฒŒ์ž„ ์˜ค๋ธŒ์ ํŠธ ๊ด€๋ จ ์†์„ฑ ๋ฐ ๋ฉ”์†Œ๋“œ
}

// ๋นŒ๋” ์‚ฌ์šฉ
IBuilder builder = new ConcreteBuilder();
builder.BuildPartA();
builder.BuildPartB();
builder.BuildPartC();
GameObject player = builder.GetResult();








์žก๋‹ด, ์ •๋ฆฌ

  • ๋””์ž์ธ ํŒจํ„ด์€ ์• ์ดˆ์— ํŠน์ • ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ๋ ค๋œ ๊ฒƒ.(์„ฑ๋Šฅ, ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ๊ณ ๋ ค)
  • ์ถ”๊ฐ€ ๋‚ด์šฉ ์ •๋ฆฌ




[C#] ๋””์ž์ธ ํŒจํ„ด (Design Pattern)
TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ