방문자 패턴은 무엇인가?
각각의 방문자별로 다른 기능을 수행하는 것이다.
의사가 한 명 있을 때 다른 유형의 학생들을 각각 다르게 대해야 한다
그럴 때 사용하는 것이 방문자 패턴이다
public abstract class Patient
{
public abstract void Accept(Doctor doctor);
}
public class AdultPatient : Patient
{
public override void Accept(Doctor doctor)
{
doctor.GiveShot(this);
}
}
public class AdolesentPatient : Patient
{
public override void Accept(Doctor doctor)
{
doctor.GiveShot(this);
}
}
public class Doctor : MonoBehaviour ,IDoctor
{
List<Patient> patients = new List<Patient>();
Patient adolentPatient = new AdolesentPatient();
Patient adultPatient = new AdultPatient();
public void GiveShot(AdultPatient patient)
{
Console.WriteLine("Adult Shot");
}
public void GiveShot(AdolesentPatient patient)
{
Console.WriteLine("Adolesent Shot");
}
}
위에 예시와 관련해서 방문자 패턴을 작성 해 보았다.