.NET/설계 패턴(C#)

[설계 패턴 C#] 6. 적응자 패턴

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

6. 적응자 패턴


BeforeAdapter.zip

Adapter.zip



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

 

*적응자 패턴 적용 전

PImageProcessor.cs

namespace BeforeAdapter

{

    class PImageProcessor

    {

        public string Subject

        {

            get;

            set;

        }

        public string Picture

        {

            get;

            private set;

        }

 

        public PImageProcessor()

        {

            Subject = string.Empty;

            Picture = string.Empty;

        }

 

        public void ImageProcessing()

        {

            Picture = Subject.Replace("people","PEOPLE");

        }

    }

}

 

Camera.cs

namespace BeforeAdapter

{

    class Camera

    {

        PImageProcessor pi_processor;

 

        public Camera(PImageProcessor pi_processor)

        {

            this.pi_processor = pi_processor;

        }

 

        public string TakeAPicture(string subject)

        {

            pi_processor.Subject = subject;

            pi_processor.ImageProcessing();

            return pi_processor.Picture;

        }

    }

}

 

Program.cs

using System;

 

namespace BeforeAdapter

{

    class Program

    {

        static void Main(string[] args)

        {

            PImageProcessor pi_processor = new PImageProcessor();

            Camera ca = new Camera(pi_processor);

            string picture = ca.TakeAPicture("people animal people plant");

            Console.WriteLine(picture);

        }

    }

}

 

*적응자 패턴 적용 후

AImageProcessor.cs

namespace Adapter

{

    class AImageProcessor

    {

        public string Image

        {

            get;

            set;

        }

        public string Picture

        {

            get;

            private set;

        }

 

        public AImageProcessor()

        {

            Image = string.Empty;

            Picture = string.Empty;

        }

        public void Processing()

        {

            Picture = Image.Replace("animal","ANIMAL");

        }

    }

}

 

IImageProcessing

namespace Adapter

{

    interface IImageProcessing

    {

        string Subject

        {

            get;

            set;

        }

        string Picture

        {

            get;

        }

        void ImageProcessing();

    }

}

 

WrapAImageProcessor.cs

namespace Adapter

{

    class WrapAImageProcessor:IImageProcessing

    {

        AImageProcessor ai_processor = new AImageProcessor();// 영상 처리하는 실개체

 

        public string  Subject //약속한 속성에 대한 구현

        {

            get

            {

                return ai_processor.Image; //감싸고 있는 개체를 이용함

            }

            set

            {

                ai_processor.Image = value; //감싸고 있는 개체를 이용함

            }

        }

        public string  Picture//약속한 속성에 대한 구현

        {

            get

            {

                return ai_processor.Picture; //감싸고 있는 개체를 이용함

            }

        }

        public void ImageProcessing()//약속한 메서드 구현

        {

            ai_processor.Processing(); //감싸고 있는 개체를 이용함

        }

    }

}

 

PImageProcessor.cs

namespace Adapter

{

    class PImageProcessor:IImageProcessing //인터페이스 기반의 클래스로 변경

    {

        public PImageProcessor()

        {

            Subject = string.Empty;

            Picture = string.Empty;

        }

 

        public string Subject

        {

            get;

            set;

        }

        public string Picture

        {

            get;

            private set;

        }

 

        public void ImageProcessing()

        {

            Picture = Subject.Replace("people", "PEOPLE");

        }

    }

}

 

Camera.cs

namespace Adapter

{

    class Camera

    {

        IImageProcessing img_processor;

 

        public Camera(IImageProcessing img_processor)

        {

            this.img_processor = img_processor;

        }

 

        public string TakeAPicture(string subject)

        {

            img_processor.Subject = subject;

            img_processor.ImageProcessing();

            return img_processor.Picture;

        }

 

        public void UpgradeFirmWare(IImageProcessing img_processor)

        {

            this.img_processor = img_processor;

        }

    }

}

 

Program.cs

using System;

namespace Adapter

{

    class Program

    {

        static void Main(string[] args)

        {

            IImageProcessing[] ips = new IImageProcessing[2];

            ips[0] = new PImageProcessor();

            ips[1] = new WrapAImageProcessor();

           

            Camera ca = new Camera(ips[0]);

            string picture = ca.TakeAPicture("people animal people plant");

            Console.WriteLine(picture);

            ca.UpgradeFirmWare(ips[1]);

            picture = ca.TakeAPicture("people animal people plant");

            Console.WriteLine(picture);

        }

    }

}

 

반응형