using UnityEngine;
public abstract class Warrior
{
string name = "전사";
public virtual string Name
{
get
{
return name;
}
}
public abstract float Attack();
}
public class RealWarrior : Warrior
{
public override string Name => base.Name; //부모 클래스의 Name 프로퍼티 실행
public override float Attack()
{
return 10;
}
}
public abstract class WarriorDecroator : Warrior //WarriorDecroator 클래스 역시 Warrior의 일부분
{
public Warrior warrior;
}
public class Sword : WarriorDecroator
{
public Sword(Warrior warrior)
{
this.warrior = warrior;
}
public override string Name
{
get
{
return warrior.Name + "with Sword";
}
}
public override float Attack()
{
return warrior.Attack() + 10;
}
}
public class Shield : WarriorDecroator
{
public Shield(Warrior warrior)
{
this.warrior = warrior;
}
public override string Name
{
get
{
return warrior.Name + "with Shield";
}
}
public override float Attack()
{
return warrior.Attack() + 10;
}
}
using Unity.VisualScripting;
using UnityEngine;
public class DecoratorTest : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Warrior warrior = new RealWarrior();
warrior = new Sword(warrior);
Debug.Log(warrior.Name + " " + warrior.Attack());
warrior = new Shield(warrior);
Debug.Log(warrior.Name + " " + warrior.Attack());
warrior = new Sword(warrior);
Debug.Log(warrior.Name + " " + warrior.Attack());
warrior = new Sword(warrior);
Debug.Log(warrior.Name + " " + warrior.Attack());
warrior = new Shield(warrior);
Debug.Log(warrior.Name + " " + warrior.Attack());
}
}
실행 결과
