.NET/설계 패턴(C#)

[설계 패턴 C#] 10. 퍼샤드 패턴

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

10. 퍼샤드 패턴


Facade.zip

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

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

        }

    }

}

 

Compensator.cs

namespace Facade

{

    class Compensator //하위 계층 서비스

    {

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

        {

            picture.Change(tone, brightness, saturation);

        }

    }

}

 

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

            }

        }

    }

}

 

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(); //내부 개체 이용

        }

    }

}

 

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

        }

    }

}


반응형