.NET/설계 패턴(C#)

[설계 패턴 C#] 23. 전략 패턴(Strategy Pattern)

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

[설계 패턴 C#] 23. 전략 패턴(Strategy Pattern)


Strategy.zip


"본문 내용"

[Escort GoF 디자인 패턴 C#] 23. 전략 패턴(Strategy Pattern)

[Escort GoF 디자인 패턴 C#] 23. 전략 패턴(Strategy Pattern) 설계

[Escort GoF 디자인 패턴 C#] 23. 전략 패턴(Strategy Pattern) 구현


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

        }

    }

}


▶ IView.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);

        }       

    }

}


▶ 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("자세히 보기 모드");

            }

        }

    }

}


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

        }

    }

}

전략 패턴 예제 실행 화면





반응형