.NET/설계 패턴(C#)

[설계 패턴 C#] 9. 장식자 패턴

언제나휴일 2016. 6. 9. 22:52
반응형

9. 장식자 패턴


Decorator.zip


"본문 내용"은 언제나 휴일 본 사이트에 있습니다.

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

        }

    }

}

 

IChange.cs

namespace Decorator

{

    interface IChange

    {

        void Change(Picture picture,int tone,int brightness,int saturation);

    }

}

 

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

        }

    }

}

 

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();

        }

    }

}

 

반응형