15. 책임 연쇄 패턴(Chain of Responsibility Pattern)
"본문 내용"
[Escort GoF의 디자인 패턴]15. 책임 연쇄 패턴(Chain of Responsibility Pattern)
[Escort GoF의 디자인 패턴] 15. 책임 연쇄 패턴(Chain of Responsibility Pattern) 설계
[Escort GoF의 디자인 패턴] 15. 책임 연쇄 패턴(Chain of Responsibility Pattern) 구현
▶ ChangeHandler.cs
using System.Collections.Generic;
namespace ChainofResponsibility
{
abstract class ChangeHandler
{
public ChangeHandler Successor
{
get;
set;
}
readonly int hid;
public ChangeHandler(int hid)
{
this.hid = hid;
Successor = null;
}
public abstract string ChangeRequest(List<int> mode,string picture);
protected bool IncludeMode(List<int> mode)
{
foreach (int m in mode)
{
if (m == hid)
{
return true;
}
}
return false;
}
}
}
▶ GrayChangeHandler.cs
using System.Collections.Generic;
namespace ChainofResponsibility
{
class GrayChangeHandler:ChangeHandler
{
public GrayChangeHandler(int hid):base(hid)
{
}
public override string ChangeRequest(List<int> mode, string picture)
{
if(IncludeMode(mode))
{
picture = picture.Replace("칼라","흑백");
}
if (Successor != null)
{
picture = Successor.ChangeRequest(mode, picture);
}
return picture;
}
}
}
▶ RedEyeChangeHandler.cs
using System.Collections.Generic;
namespace ChainofResponsibility
{
class RedEyeChangeHandler:ChangeHandler
{
public RedEyeChangeHandler(int hid):base(hid)
{
}
public override string ChangeRequest(List<int> mode, string picture)
{
if (IncludeMode(mode))
{
picture = picture.Replace("빨간눈", "정상눈");
}
if (Successor != null)
{
picture = Successor.ChangeRequest(mode, picture);
}
return picture;
}
}
}
▶ SoftChangeHandler.cs
using System.Collections.Generic;
namespace ChainofResponsibility
{
class SoftChangeHanler:ChangeHandler
{
public SoftChangeHanler(int hid)
: base(hid)
{
}
public override string ChangeRequest(List<int> mode, string picture)
{
if (IncludeMode(mode))
{
picture = picture.Replace("날카로운", "부드러운");
}
if (Successor != null)
{
picture = Successor.ChangeRequest(mode, picture);
}
return picture;
}
}
}
▶ UIPart.cs
using System.Collections.Generic;
namespace ChainofResponsibility
{
class UIPart
{
ChangeHandler head = null;
ChangeHandler tail = null;
public void AddChangeHandler(ChangeHandler handler)
{
if(head != null)
{
tail.Successor= handler;
tail = handler;
}
else
{
head = tail = handler;
}
}
public string ChangeRequest(List<int> mode,string subject)
{
if(head!=null)
{
return head.ChangeRequest(mode,subject);
}
return subject;
}
}
}
using System.Collections.Generic;
namespace ChainofResponsibility
{
class Program
{
static void Main(string[] args)
{
string picture = string.Empty;
List<int> mode = new List<int>();
ChangeHandler[] handlers = new ChangeHandler[3];
handlers[0] = new GrayChangeHandler(0);
handlers[1] = new SoftChangeHanler(1);
handlers[2] = new RedEyeChangeHandler(2);
UIPart uipart = new UIPart();
uipart.AddChangeHandler(handlers[0]);
uipart.AddChangeHandler(handlers[1]);
uipart.AddChangeHandler(handlers[2]);
picture = uipart.ChangeRequest(mode,"칼라 빨간눈 날카로운 몸매");
Console.WriteLine(picture);
mode.Add(0);
picture = uipart.ChangeRequest(mode,"칼라 빨간눈 날카로운 몸매");
Console.WriteLine(picture);
mode.Add(2);
picture = uipart.ChangeRequest(mode,"칼라 빨간눈 날카로운 몸매");
Console.WriteLine(picture);
mode.Add(1);
picture = uipart.ChangeRequest(mode,"칼라 빨간눈 날카로운 몸매");
Console.WriteLine(picture);
}
}
}'.NET > 설계 패턴(C#)' 카테고리의 다른 글
[설계 패턴 C#] 17. 해석자 패턴(Interpreter Pattern) (0) | 2016.12.07 |
---|---|
[설계 패턴 C#] 16. 명령 패턴(Command Pattern) (0) | 2016.12.06 |
[설계 패턴 C#]14. 프락스 패턴(Proxy Pattern) - 보호용 프락시 (0) | 2016.06.24 |
[설계 패턴 C#]13. 프락스 패턴(Proxy Pattern) - 가상 프락시 (0) | 2016.06.24 |
[설계 패턴 C#]12. 프락스 패턴(Proxy Pattern) - 원격지 프락시 (0) | 2016.06.23 |