*Factory: ๊ฐ์ฒด ์์ฑ์ ์ฒ๋ฆฌํ๋ ํด๋์ค๋ฅผ ํฉํ ๋ฆฌ๋ผ๊ณ ๋ถ๋ฅธ๋ค.
*Simple Factory: ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์ผ์ ์ ๋ดํ๋ ํด๋์ค.
Simple Factory Pattern ์๋ ์ฃผ์ด์ง ์ ๋ ฅ์ ๊ธฐ๋ฐ์ผ๋ก ๋ค๋ฅธ ์ ํ์ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ๋ฉ์๋๊ฐ ์๋ ํฉํ ๋ฆฌ ํด๋์ค๊ฐ ์๋ค.
Simple Factory ๋ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ํ ๋ ํญ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ผ ํจํด์ผ๋ก ์ทจ๊ธํ์ง๋ ์๋๋ค.
๋ค๋ง, ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ด๋ ์ถ์ ํฉํ ๋ฆฌ ํจํด์ ๊ธฐ๋ณธ์ด ๋๊ธฐ ๋๋ฌธ์ ์์๋์.
using UnityEngine;
public abstract class Unit
{
public abstract void move();
}
public class Marine : Unit
{
public Marine()
{
Debug.Log("Marine ์์ฑ");
}
public override void move()
{
Debug.Log("Marine ์ด๋");
}
}
public class Firebat : Unit
{
public Firebat()
{
Debug.Log("Firebat ์์ฑ");
}
public override void move()
{
Debug.Log("Firebat ์ด๋");
}
}
public class Madic : Unit
{
public Madic()
{
Debug.Log("Madic ์์ฑ");
}
public override void move()
{
Debug.Log("Madic ์ด๋");
}
}
Unit ์ถ์ ํด๋์ค๋ฅผ ์์๋ฐ๊ณ Unit ์ถ์ํด๋์ค์ move ์ถ์ ๋ฉ์๋๋ฅผ ๊ฐ์ง Marine, Firebat, Madic ํด๋์ค๊ฐ ์๋ค.
public enum UnitType
{
Marine,
Firebat,
Madic
}
public class UnityFactory
{
public static Unit createUnit(UnitType unitType)
{
Unit unit = null;
switch (unitType)
{
case UnitType.Marine:
unit = new Marine();
break;
case UnitType.Firebat:
unit = new Firebat();
break;
case UnitType.Madic:
unit = new Madic();
break;
}
return unit;
}
}
UnitFactory ํด๋์ค๋ UnitType ์ ๋ง์ถฐ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๋ฐํํ๋ค.
์ด๋ ๊ฒ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ถ๋ถ๋ง์ ๋ ๋ผ์ด์ ํด๋์ค๋ก ๋ง๋๋ ๋ฐฉ๋ฒ์ด Simple Factory ํจํด์ด๋ค.
using UnityEngine;
public class FactoryUse : MonoBehaviour
{
void Start()
{
Unit unit1 = UnityFactory.createUnit(UnitType.Marine);
Unit unit2 = UnityFactory.createUnit(UnitType.Firebat);
Unit unit3 = UnityFactory.createUnit(UnitType.Madic);
unit1.move();
unit2.move();
unit3.move();
}
}
UnitFactory ํด๋์ค๋ฅผ ์ฌ์ฉํ ๋ค๋ฅธ ํด๋์ค์์ Unit ์ ๋ง๋ค๊ณ move ํจ์๋ฅผ ํธ์ถํด์ค ๋ชจ์ต์ด๋ค.
'๐ Computer Science > Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Unity][๋์์ธ ํจํด] Component Pattern (0) | 2023.09.12 |
---|---|
[C#][Unity][๋์์ธ ํจํด] Strategy Pattern(์ ๋ต ํจํด) (0) | 2023.09.05 |
[C#][Unity][๋์์ธ ํจํด] Command ํจํด (0) | 2023.09.05 |
[Unity][๋์์ธ ํจํด] Observer Pattern (0) | 2023.09.03 |