* 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 ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
'๐ฎ 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 |