๐ŸŽฎ Unity Study/C#

[Unity][๋””์ž์ธ ํŒจํ„ด] Builder Pattern(๋นŒ๋” ํŒจํ„ด)

ibelieveinme 2023. 9. 21. 23:58
728x90

* Builder Pattern: ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๊ทธ ๊ฐ์ฒด๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ถ€๋ถ„ ๋ถ€๋ถ„์„ ๋จผ์ € ์ƒ์„ฑํ•˜๊ณ , ์ด๋ฅผ ์กฐํ•ฉํ•จ์œผ๋กœ์จ ๊ฐ์ฒด ์ „์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

* Builder Pattern ๊ณผ  Abstract Factroy Pattern ๊ณผ์˜ ์ฐจ์ด์ 

1) Builder Pattern: ๋ณต์žกํ•œ ๊ฐ์ฒด์˜ ๋‹จ๊ณ„๋ณ„ ์ƒ์„ฑ์— ์ค‘์ ์„ ๋‘๊ณ  ์žˆ๋Š” ํŒจํ„ด. ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์—์„œ ์ƒ์„ฑํ•œ ์ œํ’ˆ๋ฐ˜ํ™˜.

2) Abstract Factroy Pattern: ์ œํ’ˆ์˜ ์œ ์‚ฌ๊ตฐ๋“ค์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์—ฐํ•œ ์„ค๊ณ„์— ์ค‘์ ์„ ๋‘๋Š” ํŒจํ„ด. ๋‹จ๊ณ„๋งˆ๋‹ค ๋งŒ๋“œ๋Š” ์ œํ’ˆ์„ ์ฆ‰์‹œ ๋ฐ˜ํ™˜.


Vehicle(Car, MotorCycle) ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” Builder ํŒจํ„ด์„ ๊ตฌํ˜„ํ•ด๋ณด์ž.

ํƒˆ ๊ฒƒ์˜ ๊ณตํ†ต ๋ณ€์ˆ˜์™€ ๊ธฐ๋Šฅ์„ ๊ฐ–๊ณ  ์žˆ๋Š” Vehicle ํด๋ž˜์Šค์™€ Vehicle ์„ ์ง์ ‘ ๋งŒ๋“œ๋Š” ๋ฐ ํ•„์š”ํ•œ Interface ๋ฅผ ๋งŒ๋“œ๋Š”๊ฒŒ ๊ด€๊ฑด.

 

1. Vehicle Class

using System.Collections.Generic;

namespace BuilderPattern
{
    class Vehicle
    {
        public VehicleType vehicleType;
        private List<string> _parts = new List<string>();

        public Vehicle(VehicleType vehicleType)
        {
            this.vehicleType = vehicleType;
        }

        public void AddPart(string desc)
        {
            _parts.Add(desc);
        }

        public string GetPartsList()
        {
            string partsList = vehicleType.ToString() + "parts:\n\t";
            foreach (string part in _parts)
            {
                partsList += part + " ";
            }
            return partsList;
        }
    }
}

 

Vehicle ์ด ๊ณตํ†ต์ ์œผ๋กœ ๊ฐ€์ ธ์•ผ ํ•  Type ๊ณผ ๋ถ€ํ’ˆ List ๋ณ€์ˆ˜๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค.

Vehicle ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” Vehicle ์ƒ์„ฑ์ž์™€ ๋ถ€ํ’ˆ List์— ๋ถ€ํ’ˆ๋“ค์„ ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜, ๋ถ€ํ’ˆ List ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

 

2. IVehicleBuilder ์ธํ„ฐํŽ˜์ด์Šค

namespace BuilderPattern
{
    interface IVehicleBuilder
    {
        Vehicle GetVehicle();

        void BuildFrame();
        void BuildEngine();
        void BuildWheels();
    }

    public enum VehicleType
    {
        Car,
        MotorCycle
    }
}

Vehicle ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” Property,

Frame ๋งŒ๋“ค๊ธฐ, Engine ๋งŒ๋“ค๊ธฐ, Wheel ๋งŒ๋“ค๊ธฐ ํ•จ์ˆ˜๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค.

 

3. CarBuilder Class

namespace BuilderPattern
{
    class CarBuilder : IVehicleBuilder
    {
        private Vehicle _vehicle;

        public Vehicle GetVehicle() { return _vehicle; }

        public CarBuilder()
        {
            _vehicle = new Vehicle(VehicleType.Car);
        }

        public void BuildFrame()
        {
            _vehicle.AddPart("๊ฐ•์ฒ ");
        }

        public void BuildEngine()
        {
            _vehicle.AddPart("100๋งˆ๋ ฅ");
        }

        public void BuildWheels()
        {
            _vehicle.AddPart("์•ž ์™ผ์ชฝ ๋ฐ”ํ€ด");
            _vehicle.AddPart("์•ž ์˜ค๋ฅธ์ชฝ ๋ฐ”ํ€ด");
            _vehicle.AddPart("๋’ค ์™ผ์ชฝ ๋ฐ”ํ€ด");
            _vehicle.AddPart("๋’ค ์˜ค๋ฅธ์ชฝ ๋ฐ”ํ€ด");
        }
    }
}

์ž๋™์ฐจ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” Class. VehicleType์ด Car ์ธ ์ž๋™์ฐจ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด CarBuilder ์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค.

IVehicleBuilder ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์— GetVehicle Property์™€ BuildFrame, BuildEngine, BuildWheels ํ•จ์ˆ˜๋ฅผ ์ž๋™์ฐจ ๊ฐ์ฒด์— ๋งž๊ฒŒ ์žฌ๊ตฌํ˜„ํ•œ๋‹ค.

 

4. MotorCycleBuilder Class

namespace BuilderPattern
{
    class MotorCycleBuilder : IVehicleBuilder
    {
        private Vehicle _vehicle;

        public Vehicle GetVehicle() { return _vehicle; }

        public MotorCycleBuilder()
        {
            _vehicle = new Vehicle(VehicleType.MotorCycle);
        }

        public void BuildFrame()
        {
            _vehicle.AddPart("์•Œ๋ฃจ๋ฏธ๋Š„");
        }

        public void BuildEngine()
        {
            _vehicle.AddPart("50๋งˆ๋ ฅ");
        }

        public void BuildWheels()
        {
            _vehicle.AddPart("์•ž ๋ฐ”ํ€ด");
            _vehicle.AddPart("๋’ท ๋ฐ”ํ€ด");
        }
    }
}

์˜คํ† ๋ฐ”์ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” Class.

VehicleType์ด MotorCycle ์ธ ์ž๋™์ฐจ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด MotorCycleBuilder ์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค.

Car์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ IVehicleBuilder ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์— GetVehicle Property์™€ BuildFrame, BuildEngine, BuildWheels ํ•จ์ˆ˜๋ฅผ ์˜คํ† ๋ฐ”์ด ๊ฐ์ฒด์— ๋งž๊ฒŒ ์žฌ๊ตฌํ˜„ํ•œ๋‹ค.

 

5. Engineer ๊ฐ์ฒด

namespace BuilderPattern
{
    class Engineer
    {
        public void Construct(IVehicleBuilder vehicleBuilder)
        {
            vehicleBuilder.BuildFrame();
            vehicleBuilder.BuildEngine();
            vehicleBuilder.BuildWheels();
        }
    }
}

์ž๋™์ฐจ๋ฅผ ๋งน๋“œ๋Š” Engineer ํด๋ž˜์Šค.

Construct ํ•จ์ˆ˜์— IVehicleBuilder ๋ฅผ ์ƒ์†๋ฐ›์€ CarBuilder or MotorCycleBuilder ํด๋ž˜์Šค๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๊ทธ์— ๋งž๋Š” ๊ฐ์ฒด์˜ BuildFrame(), BuildEngine(), BuildWheels() ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

 

6. BuilderUse ํด๋ž˜์Šค

using UnityEngine;

namespace BuilderPattern
{
    class BuilderUse : MonoBehaviour
    {
        void Start()
        {
            Engineer engineer = new Engineer();
            CarBuilder carBuilder = new CarBuilder();
            MotorCycleBuilder motorCycleBuilder = new MotorCycleBuilder();

            engineer.Construct(carBuilder);
            engineer.Construct(motorCycleBuilder);

            Vehicle car = carBuilder.GetVehicle();
            Debug.Log(car.GetPartsList());

            Vehicle motorCycle = motorCycleBuilder.GetVehicle();
            Debug.Log(motorCycle.GetPartsList());
        }
    }
}

์ด ๋ชจ๋“  Builder Pattern ์„ ๊ด€๋ฆฌํ•˜๋Š” ๋งค๋‹ˆ์ € ํด๋ž˜์Šค.

Engineer ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  CarBuilder, MotorCycleBuilder ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ Engineer ์—๊ฒŒ ์ „๋‹ฌํ•˜์—ฌ Vehicle ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

728x90

'๐ŸŽฎ Unity Study > C#' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[C#] using ๋ฌธ  (0) 2023.12.13
[C#] Boxing, Unboxing -> Generic  (0) 2023.09.08
C# & Unity Code Convention  (0) 2023.08.29
[C#] Generic, Template  (0) 2023.08.21
[C#] ๋ฌธ๋ฒ• Keyword  (0) 2023.08.19