미처 완성되지 못한 알고리즘
옵저버 패턴의 간단한 예들 본문
상태 체크를 하여, 변경 시 결과에 따라 수행시킬 수 있다. 주로 UI에 쓰인다.
abstract class Observer
{
public abstract void Update();
}
abstract class Subject
{
private List<Observer> m_observers = new List<Observer>();
public void Attach(Observer observer)
{
m_observers.Add(observer);
}
public void Detach(Observer observer)
{
m_observers.Remove(observer);
}
public void Notify()
{
foreach (Observer o in m_observers)
o.Update();
}
}
class ConcreteSubject : Subject
{
private string m_state;
public string SubjectState
{
get { return m_state; }
set { m_state = value; }
}
}
class ConcreteObserver : Observer
{
private string m_name;
private string m_state;
private ConcreteSubject m_subject;
public ConcreteObserver(ConcreteSubject subject, string name)
{
m_subject = subject;
m_name = name;
}
public override void Update()
{
m_state = m_subject.SubjectState;
Console.WriteLine("Observer {0}'s state is {1}", m_name, m_state);
}
public ConcreteSubject Subject
{
get
{
return m_subject;
}
set
{
m_subject = value;
}
}
}
class Program
{
static void Main(string[] args)
{
ConcreteSubject s = new ConcreteSubject();
s.Attach(new ConcreteObserver(s, "Observer1"));
s.Attach(new ConcreteObserver(s, "Observer2"));
s.Attach(new ConcreteObserver(s, "Observer3"));
s.SubjectState = "Move";
s.Notify();
}//main
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
abstract class Unit
{
private string m_name;
private int m_health;
private List<UnitViewer> unitViewers = new List<UnitViewer>();
public Unit(string name, int health)
{
m_name = name;
m_health = health;
}
public void Attach(UnitViewer investor)
{
unitViewers.Add(investor);
}
public void Detach(UnitViewer investor)
{
unitViewers.Remove(investor);
}
public void Notify()
{
foreach (UnitViewer unitvewr in unitViewers)
unitvewr.Update(this);
}
public int Health
{
get { return m_health; }
set {
m_health = value;
Notify();
}
}
public string Name { get { return m_name; } }
}
class Marine : Unit
{
public Marine(string name, int health) : base(name, health) { }
}
interface UnitViewer
{
void Update(Unit unit);
}
class MainScreen : UnitViewer
{
private Unit m_unit;
public void Update(Unit _unit)
{
m_unit = _unit;
Console.WriteLine("메인 화면 {0} 상태 변경 : 체력 {1}", m_unit.Name, m_unit.Health.ToString());
}
public Unit unit { get { return m_unit; } set { m_unit = value; } }
}
class StatusScreen : UnitViewer
{
private Unit m_unit;
public void Update(Unit _unit)
{
m_unit = _unit;
Console.WriteLine("상태창 {0} 상태 변경 : 체력 {1}", m_unit.Name, m_unit.Health.ToString());
}
public Unit unit { get { return m_unit; } set { m_unit = value; } }
}
class EnemyScreen : UnitViewer
{
private Unit m_unit;
public void Update(Unit _unit)
{
m_unit = _unit;
Console.WriteLine("메인 화면 {0} 상태 변경 : 체력 {1}", m_unit.Name, m_unit.Health.ToString());
}
public Unit unit { get { return m_unit; } set { m_unit = value; } }
}
class Program
{
static void Main(string[] args)
{
Marine marine = new Marine("마린1", 100);
marine.Attach(new MainScreen());
marine.Attach(new StatusScreen());
marine.Attach(new EnemyScreen());
marine.Health = 60;
marine.Health = 40;
}//main
}
'IT > 디자인 패턴' 카테고리의 다른 글
command 패턴의 간단한 예들 (0) | 2017.10.02 |
---|---|
어댑터 패턴의 간단한 예들 (0) | 2017.09.29 |
팩토리 메소드 패턴 간단한 예 (0) | 2017.09.28 |
간단한 싱글톤 예제들 (0) | 2017.09.25 |