[태그:] <span>Command Pattern</span>

16.4 구현

명령 패턴에 대한 예제 프로그램을 구현하는 순서는 Picture, Command 군, PictureManager, UIPart와 데모 코드 순으로 하겠습니다.

16.4.1 Picture

Picture에서는 단순히 사진의 이름과 소유자 정보를 멤버로 갖는 클래스로 구현을 하겠습니다.

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

16.4.2 Command 군

IExecute에서는 입력된 Picture에 대한 작업을 수행할 수 있다는 약속만 할 것입니다. 그리고, 이를 구현 약속하는 ViewPicute, ViewVerifyPicture에서는 약속한 Execute 행위에 대한 구체적인 구현하면 되겠죠.

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

16.4.3 PictureManager

PictureManager에서는 사진을 추가하는 메서드 외에 사용자에 의해 전달된 명령을 보관된 모든 사진에 적용하는 메서드를 제공할께요.  만약, 명령 패턴을 적용하지 않았다면 보관된 사진에 대한 구체적인 행위에 대한 구현을 이 곳에서 수행을 해야 합니다. 명령 패턴에서는 추상적인 행위에 대한 약속과 구체적인 행위에 대한 구현을 분리하였기 때문에 이 곳에서 보관된 사진에 대한 구체적인 작업을 구현하지 않고 사용하는 곳에서 구체적 구현을 한 개체를 입력 인자로 전달하면 이를 이용하여 사진에 대한 작업을 수행을 하는 것이죠.

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

16.4.4 UIPart와 데모 코드

UIPart에서는 사용자가 로그 인을 하는 메서드와 사진을 저장하는 메서드, 전체 사진에 대해 간략히 보거나 자세히 볼 수 메서드를 제공합니다.

UIPart 생성자에서는 PictureManager 개체를 생성하는 것 이외에도 사용자가 전체 사진을 간략히 보기를 원할 때 Picture 개체의 간략 정보를 보여줄 수 있는 ViewPicture 개체를 생성을 하고 있습니다. 물론, 자세히 보기를 원할 때에 사용할 ViewVerifyPicture 개체도 생성합니다.

그리고, 실제 사용자가 전체 사진을 간략히 보기를 원하거나 자세히 보기를 요청을 하면 PictureManager 개체에게 사진에 대한 특정 작업을 수행할 수 있는 ViewPicture 혹은 ViewVerifyPicture 개체를 입력 인자로 전달하여 수행하게 합니다.

▶ 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);
        }
    }
}
[그림] 명령 패턴실행 결과
[그림] 명령 패턴실행 결과

설계 패턴