어댑터 패턴(Adapter Pattern)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterManager : MonoBehaviour
{
[SerializeField] private Zombie _zombie;
[SerializeField] private Alien _alien;
private List<IMonster> _monsters = new List<IMonster>();
public void Start()
{
_monsters.Add(new ZombieAdapter(_zombie));
_monsters.Add(new AlienAdapter(_alien));
foreach (var mon in _monsters)
{
mon.Attack();
}
}
}
▲ 몬스터 매니저
public class Zombie : MonoBehaviour
{
public void Bite()
{
Debug.Log("Zombie 물기");
}
}
▲ 좀비
public class Alien : MonoBehaviour
{
public void Shoot()
{
Debug.Log("Alien 발사");
}
}
▲ 외계인
public interface IMonster
{
public void Attack();
}
public abstract class Adapter<T> : IMonster
{
protected T Adaptee;
public Adapter(T t)
{
Adaptee = t;
}
public abstract void Attack();
}
public class ZombieAdapter : Adapter<Zombie>
{
public ZombieAdapter(Zombie z) : base(z)
{
}
public override void Attack()
{
Adaptee.Bite();
}
}
public class AlienAdapter : Adapter<Alien>
{
public AlienAdapter(Alien a) : base(a)
{
}
public override void Attack()
{
Adaptee.Shoot();
}
}
▲ 어뎁터 패턴의 활용