.NET/설계 패턴(C#)

[설계 패턴 C#] 19. 중재자 패턴(Mediator Pattern)

언제나휴일 2016. 12. 8. 00:12
반응형

[설계 패턴 C#] 19. 중재자 패턴(Mediator Pattern)


Mediator.zip


"본문 내용"

[Escort GoF 디자인 패턴 C#] 19. 중재자 패턴(Mediator Pattern)

[Escort GoF 디자인 패턴 C#] 19. 중재자 패턴(Mediator Pattern) 설계

[Escort GoF 디자인 패턴 C#] 19. 중재자 패턴(Mediator Pattern) 구현


     ▶ InnerModule.cs

namespace Mediator

{

    abstract class InnerModule

    {

        IChange mediator;

        public int Mid

        {

            get;

            private set;

        }

        public InnerModule(IChange mediator,int mid)

        {

            Mid = mid;

            this.mediator = mediator;

        }

        public void Changed(int value)

        {

            mediator.Change(Mid,value);

        }

        public abstract void SetValue(int value);

        public abstract void AlramChanged(int value);

    }

}


▶ Shutter.cs

using System;

namespace Mediator

{

    class Shutter:InnerModule

    {

        int speed;

        public Shutter(IChange mediator,int mid):base(mediator,mid)

        {

            speed = 10;

        }

        public override void SetValue(int value)

        {

            this.speed = value;

            Console.WriteLine("셔터 스피드:{0}",speed);

            Changed(speed);

        }

        public override void AlramChanged(int iris)

        {

            Console.WriteLine("조리개 F값 변경 통보 받음");

            SetValue(10 - iris);

        }

    }

}


▶ 
Iris.cs

using System;

namespace Mediator

{

    class Iris : InnerModule

    {

        int iris;

        public Iris(IChange mediator,int mid):base(mediator,mid)

        {

            iris = 0;

        }

        public override void SetValue(int value)

        {

            this.iris = value;

            Console.WriteLine("조리개 F:{0}", iris);

            Changed(iris);

        }

        public override void AlramChanged(int speed)

        {

            Console.WriteLine("셔터 스피드 변경 통보 받음");

            SetValue(10 - speed);

        }

    }

}


▶ IChange.cs

namespace Mediator

{

    interface IChange

    {

        void Change(int mid,int value);

    }

}


▶ InnerMediator.cs

namespace Mediator

{

    class InnerMediator:IChange

    {

        InnerModule[] modules = new InnerModule[2];

        int mode;

        public void SetInnerModule(int mid, InnerModule module)

        {

            modules[mid] = module;

        }

        public void ChangeMode(int mode)

        {

            this.mode = mode;

        }

        public void Change(int mid, int value)

        {

            if (mode != mid)

            {

                return;

            }

            if (modules[0].Mid == mid)

            {

                modules[1].AlramChanged(value);

            }

            else

            {

                modules[0].AlramChanged(value);

            }

        }

    }

}


▶ Camera.cs
using System;

namespace Mediator

{

    class Camera

    {

        InnerMediator mediator = new InnerMediator();

        InnerModule[] modules = new InnerModule[2];

        public Camera()

        {           

            modules[0] = new Iris(mediator,0);

            modules[1] = new Shutter(mediator,1);

            mediator.SetInnerModule(0,modules[0]);

            mediator.SetInnerModule(1,modules[1]);

            mediator.ChangeMode(0);

        }

        public void ChangeMode(int mode)

        {

            if((mode != 0)&&(mode!=1)){    return;    }

            if(mode == 0){    Console.WriteLine("***   조리개 우선 모드  ***");    }

            else{    Console.WriteLine("***   셔터 스피드 우선 모드  ***");    }

            mediator.ChangeMode(mode);

        }

        public void SetIris(int iris){    modules[0].SetValue(iris);    }

        public void SetShutterSpeed(int speed){    modules[1].SetValue(speed);    }

    }

}

▶ Program.cs
using System;

namespace Mediator

{

    class Program

    {

        static void Main(string[] args)

        {

            Camera camera = new Camera();

            camera.ChangeMode(0);

            Console.WriteLine( "1");

            camera.SetShutterSpeed(3);

            Console.WriteLine("2");

            camera.SetIris(3);

            camera.ChangeMode(1);

            Console.WriteLine("3");

            camera.SetShutterSpeed(4);

            Console.WriteLine("4");

            camera.SetIris(4);

        }

    }

}


중재자 패턴 예제 실행 화면






반응형