.NET/설계 패턴(C#)

[설계 패턴 C#] 16. 명령 패턴(Command Pattern)

언제나휴일 2016. 12. 6. 00:44
반응형

[설계 패턴 C#] 16. 명령 패턴(Command Pattern)


Command.zip



"본문 내용"

[Escort GoF의 디자인 패턴 C#] 16. 명령 패턴(Command Pattern)

[Escort GoF의 디자인 패턴 C#] 16. 명령 패턴(Command Pattern) 설계

[Escort GoF의 디자인 패턴 C#] 16. 명령 패턴(Command Pattern) 구현

     ▶ Picture.cs

namespace Command

{

    class Picture

    {

        public string Name

        {

            get;

            private set;

        }

        public string User

        {

            get;

            private set;

        }

        public Picture(string name,string user)

        {

            Name = name;

            User = user;                  

        }

    }

}


▶ IExecute.cs

namespace Command

{

    interface IExecute

    {

        void Execute(Picture picture);

    }

}


▶ 
CommandAlgorithm.cs

using System;

 

namespace Command

{

    class ViewPicture:IExecute

    {        

        public void Execute(Picture picture)

        {

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

        }    

    }

    class ViewVerifyPicture:IExecute

    {

        public void Execute(Picture picture)

        {

            Console.WriteLine("[사진 파일명] {0} [소유자] {1}",picture.Name,picture.User);                        

        }

    }

}

▶ PictureManager.cs

using System.Collections.Generic;

namespace Command

{

    class PictureManager

    {

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

        public void AddPicture(string name, string user)

        {

            pictures.Add(new Picture(name, user));

        }

        public void DoItAllPicture(IExecute command)

        {

            foreach(Picture picture in pictures)

            {

                command.Execute(picture);               

            }

        }

    }

}


▶ UIPart.cs

using System;

 

namespace Command

{

    enum CommandEnum

    {

        VIEW,

        VIEW_VERIFY

    }

 

    class UIPart

    {

        PictureManager pm = new PictureManager();

        IExecute[] commands = new IExecute[2];

        string user = string.Empty;

 

        public UIPart()

        {

            commands[0] = new ViewPicture();

            commands[1] = new ViewVerifyPicture();

        }

        public void Login(string user)

        {

            this.user = user;

        }

        public void SavePicture(string name)

        {

            if(user == string.Empty)

            {

                Console.WriteLine("먼저 로그 인을 하십시오.");

            }

            else

            {

                pm.AddPicture(name,user);

            }

        }

        public void DoItCommand(CommandEnum cmd)

        {

            pm.DoItAllPicture(commands[(int)cmd]);      

        }

    }

}


▶ Program.cs
namespace Command

{

    class Program

    {

        static void Main(string[] args)

        {

            UIPart up = new UIPart();

            up.Login("홍길동");

            up.SavePicture("현충사의 봄");

            up.Login("강감찬");

            up.SavePicture("독립기념관의 꽃");

            up.DoItCommand(CommandEnum.VIEW);

            up.DoItCommand(CommandEnum.VIEW_VERIFY);

        }

    }

}






반응형