프로그래밍 기술/Escort GoF의 디자인 패턴 C#

[C#] 전략 패턴(Strategy Pattern) - 구현

언제나휴일 2016. 4. 29. 12:36
반응형

   23. 전략 패턴(Strategy Pattern)



전략 패턴(Strategy Pattern) 클래스 다이어그램

 [그림] 전략 패턴(Strategy Pattern) 클래스 다이어그램

Strategy.zip


23.4 구현

 

 전략 패턴에 대한 예제 프로그램을 구현하는 순서는 Picture, Viewer , PictureCollection과 데모 코드 순으로 하겠습니다.

 

23.4.1 Picture

 

 Picture는 단순히 사진 이름과 색조, 명도, 채도를 멤버로 갖는 클래스로 정의할께요. 전략 패턴을 설명하기 위한 시나리오에 의해 필요한 것이지 직접적으로 전략 패턴과 관련 있는 클래스는 아닙니다.

 

Picture.cs

namespace Strategy

{

    class Picture

    {

        public string Name{    get;    private set;    }

        public int Tone{    get;    private set;    }

        public int Brightness{    get;    private set;    }

        public int Saturation{    get;    private set;    }

        public Picture(string name,int tone,int brightness,int saturation)

        {

            Name = name;

            Tone = tone;

            Brightness = brightness;

            Saturation = saturation;

        }

    }

}

 

  

23.4.2 Viewer

 

 Viewer 군은 인터페이스 IView와 자세히 보기를 구현된 VerifyViewer, 간단히 보기를 구현한 SimpleViewer가 있습니다. 이들은 사진 컬렉션 개체에서 다양한 방법으로 사진 정보를 보여주는 부분을 전략 패턴을 적용하여 분리시킨 부분입니다.

 

 IView에서는 공통적으로 제공해야 할 사진 개체의 정보를 보여주는 행위에 대한 약속을 해야 합니다. 그리고, IView를 구현 약속한 각 클래스에서는 구체적으로 사진 개체의 정보를 보여주는 행위를 구현합니다.

 

Viewer.cs

namespace Strategy

{

    interface IView

    {

        void View(Picture picture);

    }

}

 

 

VerifyViewer.cs

using System;

namespace Strategy

{

    class VerifyViewer:IView

    {

        public void View(Picture picture)

        {

            Console.WriteLine("사진 파일명:{0}", picture.Name);

            Console.WriteLine("색조:{0}", picture.Tone);

            Console.WriteLine("명도:{0}", picture.Brightness);

            Console.WriteLine("채도:{0}", picture.Saturation);

        }

    }

}

 

  

SimpleViewer.cs

using System;

namespace Strategy

{

    class SimpleViewer:IView

    {

        public void View(Picture picture)

        {

            Console.WriteLine("사진 파일명:{0}", picture.Name);

        }        

    }

}

 

 

23.4.3 PicureCollection과 데모 코드

 

 PictureCollection에서는 멤버 필드로 전략 패턴을 사용한 VerifyViewer 개체와 SimpleViewer 개체를 알고 있습니다. 그리고, 사용자에 의해 보기 모드를 설정할 수 있게 할 것이며 보관된 사진들에 대한 정보 보기를 요청할 수 있게 합시다. 사용자가 사진에 대한 정보 보기를 요청하면 내부에 설정된 모드에 따라 적절한 알고리즘이 구현된 IView 개체를 이용하면 되겠죠.

 

PictureCollection.cs

using System;

using System.Collections.Generic;

namespace Strategy

{

    class PictureCollection

    {

        List<Picture> pictures = new List<Picture>();

        IView[] viewer = new IView[2];

        int mode;

        public PictureCollection()

        {

            viewer[0] = new VerifyViewer();

            viewer[1] = new SimpleViewer();

            SetMode(0);

        }

 

 

        public void Add(Picture picture)

        {

            pictures.Add(picture);

        }

        public void SetMode(int mode)

        {

            if(mode==1){    this.mode = 1;    }

            else{   this.mode = 0;    }

        }             

        public void View()

        {                          

            ViewMode();

            IView iview = viewer[mode];

            foreach(Picture picture in pictures)

            {

                iview.ViewPicture(picture);

            }

        }

        private void ViewMode()

        {

            if(mode==1)    

            {

                Console.WriteLine("간략 보기 모드");     

            }

            else

            {

                Console.WriteLine("자세히 보기 모드");

            }

        }

    }

}

 

  

 예제 코드에서는 단순히 PictureCollection 개체에 Picture 개체를 보관하여 보기 모드에 따라 적절한 알고리즘을 사용하여 보관된 사진 정보를 보여주는 지를 확인하면 되겠죠.

 

Program.cs

namespace Strategy

{

    class Program

    {

        static void Main(string[] args)

        {

            Picture picture1 = new Picture("천안 각원사", 100, 100, 100);

            Picture picture2 = new Picture("천안 유관순 열사 기념관", 120, 90, 90);

            PictureCollection pc = new PictureCollection();

 

            pc.Add(picture1);

            pc.Add(picture2);

 

            pc.View();

 

            pc.SetMode(1);

            pc.View();

        }

    }

}

 

 


실행 결과

자세히 보기 모드

사진 파일 명:천안 각원사

색조:100

명도:100

채도:100

사진 파일 명:천안 유관순 열사 기념관

색조:120

명도:90

채도:90

간략 보기 모드

사진 파일 명:천안 각원사

사진 파일 명:천안 유관순 열사 기념관




2016/04/29 - [프로그래밍 기술/Escort GoF의 디자인 패턴 C#] - [C#] 전략 패턴(Strategy Pattern) - 개요, 시나리오


2016/04/29 - [프로그래밍 기술/Escort GoF의 디자인 패턴 C#] - [C#] 전략 패턴(Strategy Pattern) - 설계(Design)


반응형