.NET/설계 패턴(C#)

[설계 패턴 C#] 20. 메멘토 패턴(Memento Pattern)

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

[설계 패턴 C#] 20. 메멘토 패턴(Memento Pattern)


Memento.zip


"본문 내용"

[Escort GoF 디자인 패턴 C#] 20. 메멘토 패턴(Memento Pattern)

[Escort GoF 디자인 패턴 C#] 20. 메멘토 패턴(Memento Pattern) 설계

[Escort GoF 디자인 패턴 C#] 20. 메멘토 패턴(Memento Pattern) 구현


     ▶ Snapshot.cs

namespace Memento

{

    class Snapshot

    {

        public int Tone{    get;    private set;    }

        public int Brightness{    get;    private set;    }

        public int Saturation{    get;    private set;    }

        public Snapshot(int tone, int brightness, int saturation)

        {

            Tone = tone;

            Brightness = brightness;

            Saturation = saturation;

        }

    }

}


▶ Picture.cs

using System;

namespace Memento

{

    class Picture

    {

        string name;

        int tone;

        int brightness;

        int saturation;

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

        {

            this.name = name;

            this.tone = tone;

            this.brightness = brightness;

            this.saturation = saturation;

        }

        public void Change(int tone,int brightness,int saturation)

        {

            this.tone += tone;

            this.brightness += brightness;

            this.saturation += saturation;

        }

        public void View()

        {

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

            Console.WriteLine("    색조:{0} 명도:{1} 채도:{2}",tone,brightness,saturation);

        }

        public Snapshot MakeSnapshot()

        {

            return new Snapshot(tone,brightness,saturation);

        }

        public void SetBySnapshot(Snapshot snapshot)

        {

            this.tone = snapshot.Tone;

            this.brightness = snapshot.Brightness;

            this.saturation = snapshot.Saturation;

        }

    }

}


▶ 
App.cs

using System;

namespace Memento

{

    class App

    {

        Picture picture;

        Snapshot snapshot = null;

        public App(Picture picture)

        {           

            SetPicture(picture);

        } 

 

        public void SetPicture(Picture picture)

        {

            End();

            Console.WriteLine("사진을 설정합니다.");

            this.picture = picture;     

            Start();

        }

        void Start()

        {

            picture.View();

            snapshot = picture.MakeSnapshot();            

        }

        public void Change(int tone,int brightness,int saturation)

        {

            picture.Change(tone,brightness,saturation);

            picture.View();

        }

        public void Cancel()

        {

            if(snapshot!=null)

            {

                picture.SetBySnapshot(snapshot);          

            }

            End();

        }

        public void End()

        {

            if(snapshot!=null)

            {                    

                picture.View();

            }

            snapshot = null;            

        }

    }

}


▶ Program.cs

namespace Memento

{

    class Program

    {

        static void Main(string[] args)

        {

            Picture picture = new Picture("천호지의 들풀", 100, 100, 100);

            Picture picture2 = new Picture("외도 가는 길", 100, 100, 100);

            App app = new App(picture);

            app.Change(10, 20, -20);

            app.Change(10, 20, -20);

            app.Cancel();

            app.SetPicture(picture2);

            app.Change(10, 20, -20);

            app.Change(10, 20, -20);

            app.End();        

        }

    }

}


메멘토 패턴 예제 실행 화면





반응형