<출처 : 챌린지반 강의노트>
<출처 : https://refactoring.guru/ko/design-patterns/builder>
<출처 : https://refactoring.guru/ko/design-patterns/abstract-factory>
public class Character
{
public string Name { get; set; }
public int Health { get; set; }
public int Strength { get; set; }
public int Agility { get; set; }
public void DisplayCharacterInfo()
{
Debug.Log($"Name: {Name}, Health: {Health}, Strength: {Strength}, Agility: {Agility}");
}
}
public class CharacterBuilder
{
private Character character;
// 생성자부터 컨베이어벨트에 탄다고 생각하면 편함
public CharacterBuilder()
{
character = new Character();
}
//이름을 만들어주기
public CharacterBuilder SetName(string name)
{
character.Name = name;
return this;
}
//체력을 설정하기
public CharacterBuilder SetHealth(int health)
{
character.Health = health;
return this;
}
//STR설정하기
public CharacterBuilder SetStrength(int strength)
{
character.Strength = strength;
return this;
}
//능력설정하기
public CharacterBuilder SetAgility(int agility)
{
character.Agility = agility;
return this;
}
//최종반환
public Character Build()
{
return character;
}
}
using UnityEngine;
public class Game : MonoBehaviour
{
void Start()
{
CharacterBuilder builder = new CharacterBuilder();
Character hero = builder.SetName("Hero")
.SetHealth(100)
.SetStrength(50)
.SetAgility(30)
.Build();
Character villain = builder.SetName("Villain")
.SetHealth(120)
.SetStrength(70)
.SetAgility(20)
.Build();
hero.DisplayCharacterInfo();
villain.DisplayCharacterInfo();
}
}
public class CharacterDirector
{
private CharacterBuilder builder;
public CharacterDirector(CharacterBuilder builder)
{
this.builder = builder;
}
public Character ConstructWarrior()
{
return builder.SetName("Warrior")
.SetHealth(200)
.SetStrength(150)
.SetAgility(50)
.Build();
}
public Character ConstructMage()
{
return builder.SetName("Mage")
.SetHealth(100)
.SetStrength(50)
.SetAgility(100)
.Build();
}
}
public class Game : MonoBehaviour
{
void Start()
{
CharacterBuilder builder = new CharacterBuilder();
CharacterDirector director = new CharacterDirector(builder);
Character warrior = director.ConstructWarrior();
warrior.DisplayCharacterInfo();
Character mage = director.ConstructMage();
mage.DisplayCharacterInfo();
}
}
public interface IEnemy
{
void Attack();
}
public class Goblin : IEnemy
{
public void Attack()
{
Debug.Log("Goblin attacks!");
}
}
public class Orc : IEnemy
{
public void Attack()
{
Debug.Log("Orc attacks!");
}
}
public interface IWeapon
{
void Use();
}
public class Sword : IWeapon
{
public void Use()
{
Debug.Log("Swinging a sword!");
}
}
public class Axe : IWeapon
{
public void Use()
{
Debug.Log("Swinging an axe!");
}
}
public interface IAbstractFactory
{
IEnemy CreateEnemy();
IWeapon CreateWeapon();
}
public class GoblinFactory : IAbstractFactory
{
public IEnemy CreateEnemy()
{
return new Goblin();
}
public IWeapon CreateWeapon()
{
return new Sword();
}
}
public class OrcFactory : IAbstractFactory
{
public IEnemy CreateEnemy()
{
return new Orc();
}
public IWeapon CreateWeapon()
{
return new Axe();
}
}
public class Battle
{
private IEnemy enemy;
private IWeapon weapon;
public Battle(IAbstractFactory factory)
{
this.enemy = factory.CreateEnemy();
this.weapon = factory.CreateWeapon();
}
public void Start()
{
enemy.Attack();
weapon.Use();
}
}
public class TestScript : MonoBehaviour
{
void Start()
{
// Goblin factory
IAbstractFactory goblinFactory = new GoblinFactory();
Battle goblinBattle = new Battle(goblinFactory);
goblinBattle.Start();
// Orc factory
IAbstractFactory orcFactory = new OrcFactory();
Battle orcBattle = new Battle(orcFactory);
orcBattle.Start();
}
}