관리 메뉴

프로그램 소스

[설계 패턴 C#] 15. 책임 연쇄 패턴(Chain of Responsibility Pattern) 본문

설계 패턴(C#)

[설계 패턴 C#] 15. 책임 연쇄 패턴(Chain of Responsibility Pattern)

Only 프로그램 소스 언제나휴일 2016.12.05 13:06

15. 책임 연쇄 패턴(Chain of Responsibility Pattern)


ChainofResponsibility.zip


"본문 내용"

[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;

        }

    }

}


▶ Program.cs
using System;

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);

        }

    }

}

책임 연쇄 패턴 예제 실행 화면




0 Comments
댓글쓰기 폼