10. 퍼사드 패턴(Facade Pattern) 구현

10. 4 구현

예제 프로그램은 Picture, Compensator, PictureManager, SmartManager, 데모 코드 순으로 설명하겠습니다.

10.4.1 Picture

Picture 형식에서는 멤버 필드로 색조, 명도, 채도가 있고 멤버 속성으로 이름을 제공하겠습니다. 그리고 사진을 수정하는 메서드, 이름이 같은지를 비교하는 메서드, 정보를 보여주는 메서드를 제공할게요.

▶ Picture.cs

using System;
namespace Facade
{
    class Picture
    {
        int tone;
        int brightness;
        int saturation;
        public string Name
        {
            get;
            private set;
        }
        public Picture(string name,int tone,int brightness,int saturation)
        {
            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;
        }

        bool IsEqual(string name)
        {
            return Name == name;
        }

        public void View()
        {
            Console.WriteLine("사진 이름:{0}",Name);
            Console.WriteLine("색조:{0} 명도:{1} 채도:{2}",tone,brightness,saturation);
        }
    }
}

10.4.2 Compensator

Compensator에서는 사진의 색조, 명도, 채도를 수정하는 메서드를 제공하겠습니다.

▶ Compensator.cs

namespace Facade
{
    class Compensator //하위 계층 서비스
    {
        public void Change(Picture picture, int tone, int brightness, int saturation)
        {
            picture.Change(tone, brightness, saturation);
        }
    }
}


10.4.3 PictureManager

PictureManager에서는 사진을 추가, 존재하는지 확인, 이름으로 사진 검색과 전체 사진을 보여주는 기능을 제공하겠습니다.

▶ PictureManager.cs

using System.Collections.Generic;
namespace Facade
{
    class PictureManager //하위 계층 서비스
    {
        List<Picture> pictures = new List<Picture>();
        public bool Exist(string name)
        {
            return FindPicture(name)!=null;
        }
        public bool AddPicture(Picture picture)
        {
            if(Exist(picture.Name))
            {
                return false;
            }
            pictures.Add(picture);
            return true;
        }

        public Picture FindPicture(string name)
        {
            foreach(Picture picture in pictures)
            {
                if(picture.Name == name)
                {
                    return picture;
                }
            }
            return null;
        }
        public void View()
        {
            foreach (Picture picture in pictures)
            {
                picture.View();
            }
        }
    }
}

10.4.4 SmartManager

SmartManager는 사용자에게 사진 관리와 수정을 쉽게 할 수 있는 기능을 제공합니다. 사용자가 사진 관리나 수정 요청을 하면 SmartManager는 실제 사진을 관리하고 수정하는 PictureManager와 Compensator 개체를 이용하여 작업을 수행합니다.

▶ SmartManager.cs

namespace Facade
{
    class SmartManager // 사용자가 하위 계층의 기능을 쉽게 사용할 수 있게 제공
    {
        Compensator compensator = new Compensator(); //사진을 수정하는 개체
        PictureManager pic_manager = new PictureManager();//사진을 관리하는 개체

        public bool Exist(string name)
        {
            return pic_manager.Exist(name); //내부 개체 이용
        }

        public bool AddPicture(Picture picture)
        {
            return pic_manager.AddPicture(picture); //내부 개체 이용
        }

        Picture FindPicture(string name)
        {
            return pic_manager.FindPicture(name); //내부 개체 이용
        }

        public bool Change(string name,int tone,int brightness,int saturation)
        {
            Picture picture = FindPicture(name);
            if(picture ==null)
            {
                return false; 
            }
            picture.Change(tone,brightness,saturation); //내부 개체 이용
            return true;
        }

        public void View()
        {
            pic_manager.View(); //내부 개체 이용
        }
    }
}

10.4.5 데모 코드

데모 코드에서는 사진 개체들을 SmartManager 개체에 보관, 변환, 정보 보기를 할 것입니다. 퍼사드 패턴을 제공하지 않았다면 사용자는 PictureManager 개체를 통해 사진을 보관하고 변환하기 위해서는 PictureManger 개체로 사진을 검색 후에 Compensator 개체를 통해 변환해야 합니다. 예제처럼 퍼사드 패턴을 적용하면 사용자는 SmartManger 개체를 통해 이와 같은 작업을 쉽게 할 수 있습니다.

▶ Program.cs

namespace Facade
{
    class Program
    {
        static void Main(string[] args)
        {
            Picture picture = new Picture("언제나 휴일", 100, 100, 100);
            Picture picture2 = new Picture("언제나 휴일2", 100, 100, 100);
            SmartManager sm = new SmartManager();
            sm.AddPicture(picture);
            sm.AddPicture(picture2);
            sm.Change("언제나 휴일", 20, 20, -50);
            sm.View();
        }
    }
}

▶ 실행 결과

사진 이름:언제나 휴일
색조:120 명도:120 채도:50
사진 이름:언제나 휴일2
색조:100 명도:100 채도:100