관리 메뉴

프로그램 소스

[설계 패턴 C#] 25. 방문자 패턴(Visitor Pattern) 본문

설계 패턴(C#)

[설계 패턴 C#] 25. 방문자 패턴(Visitor Pattern)

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

[설계 패턴 C#] 25. 방문자 패턴(Visitor Pattern)


Visitor.zip

"본문 내용"

[Escort GoF 디자인 패턴 C#] 25. 방문자 패턴(Visitor Pattern)

[Escort GoF 디자인 패턴 C#] 25. 방문자 패턴(Visitor Pattern) 설계

[Escort GoF 디자인 패턴 C#] 25. 방문자 패턴(Visitor Pattern) 구현


     ▶ Element.cs

namespace Visitor

{

    abstract class Element

    {

        public string Name

        {

            get;

            private set;

        }

        public Element(string name)

        {

            Name  = name;

        }

        public abstract void Accept(IVisit visitor);

    }

}


▶ Picture.cs

using System;

namespace Visitor

{

    class Picture:Element

    {

        int tone;

        int brightness;

        int saturation;

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

        {

            SetInfo(tone,brightness,saturation);

        }

        public void ViewInfo()

        {

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

        }

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

        {

            this.tone = tone;

            this.brightness = brightness;

            this.saturation = saturation;

        }

        public override void Accept(IVisit visitor)

        {             

            visitor.VisitPicture(this);

        }

    }

}


▶ 
Diary.cs

using System;

namespace Visitor

{

    class Diary:Element

    {

        string content;

        public Diary(string name,string content):base(name)

        {

            SetContent(content);

        }

        public void ViewContent()

        {

            Console.WriteLine(content);

        }

        public void SetContent(string content)

        {

            this.content = content;

        }

        public override void Accept(IVisit visitor)

        {

            visitor.VisitDiary(this);

        }

    }

}


▶ Visitor.cs

namespace Visitor

{

    interface IVisit

    {

        void VisitPicture(Picture picture);

        void VisitDiary(Diary diary);

    }

}


▶ 
Viewer.cs

using System;

namespace Visitor

{

    class Viewer:IVisit

    {

        public void VisitPicture(Picture picture)

        {

            Console.WriteLine("사진 제목:{0}", picture.Name);

            picture.ViewInfo();

        }

        public void VisitDiary(Diary diary)

        {

            Console.WriteLine(diary.Name);

            diary.ViewContent();

        }

    }

}


▶ Initializer.cs

namespace Visitor

{

    class Initializer:IVisit

    {

        public void VisitPicture(Picture picture)

        {

            picture.SetInfo(0, 0, 0);

        }

        public void VisitDiary(Diary diary)

        {

            diary.SetContent("내용 없음");

        }

    }

}


▶ MyTour.cs

using System;

using System.Collections.Generic;

namespace Visitor

{

    class MyTour

    {

        IVisit[] visitors = new IVisit[2];

        List<Element> collection = new List<Element>();

        public MyTour()

        {

            visitors[0] = new Viewer();

            visitors[1] = new Initializer();

        }

        public void AddElement(Element element)

        {

            collection.Add(element);

        }

        public void ViewAll()

        {

            foreach (Element element in collection)

            {

                element.Accept(visitors[0]);

            }

        }

        public void Reset()

        {

            foreach (Element element in collection)

            {

                element.Accept(visitors[1]);

            }

            Console.WriteLine("모든 정보를 초기 상태로 바꾸었습니다.");

        }

    }

}


▶ Program.cs

namespace Visitor

{

    class Program

    {

        static void Main(string[] args)

        {

            MyTour mt = new MyTour();

            Picture e = new Picture("A", 1, 1, 1);

            Diary d = new Diary("2012.6.1", "언제나 휴일");

            mt.AddElement(e);

            mt.AddElement(d);

            mt.ViewAll();

            mt.Reset();

            mt.ViewAll();

        }

    }

}





0 Comments
댓글쓰기 폼