9. 장식자 패턴(Decorator Pattern) 구현

장식자 패턴 프로젝트 다운로드

. 4 구현

 장식자 패턴의 예제 프로그램은 Picture, IChange, 보정기, 데모 순으로 구현할게요.

9.4.1 Picture

Picture 클래스는 장식자 패턴에 포함되는 형식은 아닙니다. 단순히 예제 프로그램을 위해 정의한 형식으로 멤버 필드로 색조, 명도, 채도 값을 갖고 있고 이들 값을 변경하는 메서드와 보여주는 메서드를 갖도록 구현합시다.

▶ Picture.cs

using System;

namespace Decorator
{
    class Picture
    {
        int tone;
        int brightness;
        int saturation;

        public Picture(int tone,int brightness,int saturation)
        {
            this.tone = tone;
            this.brightness = brightness;
            this.saturation = saturation;
        }

        public void ChangeTone(int tone)
        {
            this.tone += tone;
        }
        public void ChangeBrightness(int brightness)
        {
            this.brightness += brightness;
        }
        public void ChangeSaturation(int saturation)
        {
             this.saturation += saturation;
        }

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


9.4.2 IChange

IChange는 사진을 수정하는 기능을 약속한 인터페이스입니다.

▶ IChange.cs

namespace Decorator
{
    interface IChange
    {
        void Change(Picture picture,int tone,int brightness,int saturation);
    }
}

9.4.3 보정기

먼저, 단위 보정을 할 수 있는 색조 보정기, 명도 보정기, 채도 보정기를 구현해 봅시다. 각각의 단위 보정기는 IChange를 기반으로 정의하며 약속된 사진을 수정하는 기능을 구현해야 합니다.

▶ ToneCompensator.cs

namespace Decorator
{
    class ToneCompensator:IChange
    {
        //IChange에서 약속한 사진을 수정하는 메서드 구현
        public void Change(Picture picture, int tone, int brightness, int saturation)
        {
            picture.ChangeTone(tone);
        }
    }
}

▶ BrightnessCompensator.cs

namespace Decorator
{
    class BrightnessCompensator:IChange
    {
        // IChange에서 약속한 사진을 수정하는 메서드 구현
        public void Change(Picture picture, int tone, int brightness, int saturation)
        {
            picture.ChangeBrightness(brightness);
        }
    }
}

▶ SaturationCompensator.cs

namespace Decorator
{
    class SaturationCompensator:IChange
    {
        // IChange에서 약속한 사진을 수정하는 메서드 구현
        public void Change(Picture picture, int tone, int brightness, int saturation)
        {
            picture.ChangeSaturation(saturation);
        }
    }
}

장식자인 다중 보정기는 단위 보정기를 추가하는 기능을 제공할게요. 그리고 사진을 바로잡는 기능은 내부에 포함된 보정기 개체를 이용하여 구현하면 됩니다.

▶ MultiCompensator.cs

using System;
using System.Collections.Generic;
namespace Decorator
{
    class MultiCompensator:IChange
    {
        List<IChange> compensators = new List<IChange>(); 
        public void Change(Picture picture, int tone, int brightness, int saturation)
        {
            // 내부에 보관된 IChange(단위 보정기)를 이용하여 기능 구현
            foreach (IChange ichange in compensators)
            {
                ichange.Change(picture, tone, brightness, saturation);
            }
        }
        public void AddCompensator(IChange compensator)
        {
            compensators.Add(compensator);
        }
    }
}


9.4.4
데모 코드

데모 코드에서는 장식자인 MultiCompensator 개체를 생성하고 변환 대상인 Picture 개체를 먼저 생성할게요. 그리고 단위 기능 보정기들을 생성하여 MultiCompensator 개체에 포함시킨 후에 사진 수정 기능을 수행합니다. 그리고 수정 후의 사진 정보를 표시할게요.

▶ Program.cs

namespace Decorator
{
    class Program
    {
        static void Main(string[] args)
        {
            IChange  compensator = new MultiCompensator();
            Picture picture = new Picture(100, 100, 100);

            IChange tonecom = new ToneCompensator();
            IChange brightcom = new BrightnessCompensator();
            IChange saturcom = new SaturationCompensator();
            MultiCompensator mc = compensator as MultiCompensator;

            if (mc != null)
            {
                mc.AddCompensator(tonecom);
                mc.AddCompensator(brightcom);
                mc.AddCompensator(saturcom);
            }
            compensator.Change(picture, 20, 15, 12);
            picture.View();
        }
    }
}

▶ 실행 결과

색조:120 명도:115 채도:112