관리 메뉴

프로그램 소스

[설계 패턴 C#] 22. 상태 패턴(State Pattern) 본문

설계 패턴(C#)

[설계 패턴 C#] 22. 상태 패턴(State Pattern)

Only 프로그램 소스 언제나휴일 2016.12.09 00:19

[설계 패턴 C#] 22. 상태 패턴(State Pattern)


State.zip


"본문 내용"

[Escort GoF 디자인 패턴 C#] 22. 상태 패턴(State Pattern)

[Escort GoF 디자인 패턴 C#] 22. 상태 패턴(State Pattern) 설계

[Escort GoF 디자인 패턴 C#] 22. 상태 패턴(State Pattern) 구현


     ▶ Iris.cs

namespace State

{

    class Iris

    {

        int focal_length = 2;

        public int Up()

        {

            if(focal_length<10)

            {

                focal_length++;                     

            }

            return focal_length;

        }

        public int Down()

        {

            if(focal_length>2)

            {

                focal_length--;

            }

            return focal_length;

        }

    }

}


▶ Shutter.cs

namespace State

{

    class Shutter

    {

        int speed =0;

        public int Up()

        {

            speed++;                      

            return speed;

        }

        public int Down()

        {

            if(speed>0)

            {

                speed--;

            }

            return speed;

        }

    }

}


▶ 
IState.cs

namespace State

{

    interface IState

    {

        void Up();

        void Down();

    }

}


▶ OnState.cs

using System;

namespace State

{

    class OnState:IState

    {

        Shutter shutter;

        public OnState(Shutter shutter)

        {

            this.shutter = shutter;

        }

        public void Up()

        {

            Console.WriteLine("셔터 스피드 올라감. 현재 스피드:{0}",shutter.Up());

        }

        public void Down()

        {

            Console.WriteLine("셔터 스피드 내려감. 현재 스피드:{0}",shutter.Down());

        }

    }

}


▶ OffState.cs

using System;

namespace State

{

    class OffState:IState

    {

        Iris iris;

        public OffState(Iris iris)

        {

            this.iris = iris;

        }

        public void Up()

        {

            Console.WriteLine("F 값 증가되었음. 현재 F:{0}", iris.Up());

        }

        public void Down()

        {

            Console.WriteLine("F 값 감소되었음. 현재 F:{0}", iris.Down());

        }      

    }

}


▶ Camera.cs

namespace State

{

    class Camera

    {

        Shutter shutter = new Shutter();

        Iris iris = new Iris();

        IState[] states = new IState[2];

        int statenum=0;

        public Camera()

        {

            states[0] = new OffState(iris);

            states[1] = new OnState(shutter);

        }

        public void ToggleInfoButton()

        {

            statenum = (statenum+1)%2;

        }

        public void DialUp(){    states[statenum].Up();     }

        public void DialDown(){    states[statenum].Down();    }

    }

}


▶ Program.cs

namespace State

{

    class Program

    {

        static void Main(string[] args)

        {

            Camera camera = new Camera();

            camera.DialUp();

            camera.DialDown();

            camera.ToggleInfoButton();

            camera.DialUp();

            camera.DialDown();

        }

    }

}

상태 패턴 예제 실행 화면





0 Comments
댓글쓰기 폼