.Net/디자인패턴
C# Strategy Pattern
동구밖과수원
2013. 10. 22. 17:06
알고리즘군을 정의하고 각각을 캡슐화하여 교환해서 사용할 수 있도록 만든다.
스트래티지를 활용하면 알고리즘을 사용하는 클라이언트와는 독립적으로 알고리즘을 변경 할 수 있다.
static void Main(string[] args) { Man badGuy = CreateBadGuy();
Man superMan = CreateSuperMan();
badGuy.Attack(); superMan.Attack();
Console.ReadKey(); }
private static Man CreateSuperMan() { SuperMan superMan = new SuperMan(); superMan.AttackBehavior = new SuperManAttackBehavior(); return superMan; }
private static Man CreateBadGuy() { BadGuy badGuy = new BadGuy(); badGuy.AttackBehavior = new BadGuyBehavior(); return badGuy; }
|