관리 메뉴

프로그램 소스

[설계 패턴 C#] 빌더 패턴(Builder Pattern) 본문

설계 패턴(C#)

[설계 패턴 C#] 빌더 패턴(Builder Pattern)

Only 프로그램 소스 언제나휴일 2016.04.17 13:46

빌더 패턴(Builder Pattern)


Builder.zip

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


빌더 패턴(Builder Pattern)




Picture.cs

namespace Builder

{

    class Picture

    {

        public static readonly Picture EmptyPicture;

        static Picture()

        {

            EmptyPicture = new Picture(string.Empty, string.Empty);

        }

        public string Subject

        {

            get;

            private set;

        }

        public string Image

        {

            get;

            private set;

        }

        public Picture(string subject, string image)

        {

            Subject = subject;

            Image = image;

        }

    }

}


 PictureBuilder.cs

namespace Builder

{

    abstract class PictureBuilder

    {

        public Picture Picture

        {

            get;

            protected set;

        }

        public abstract void SetSubject(string subject); //상을 맺히는 기능

        public abstract void Change();//이미지를 수정하는 기능

    }

}

 

SmoothBuilder.cs

namespace Builder

{

    class SmoothBuilder:PictureBuilder

    {

        const string sharp = "Sharp";

        const string smooth = "Smooth";

        string subject;  //원본 피사체

        public override void SetSubject(string subject) //상을 맺히는 기능

        {

            this.subject = subject;

            Picture = new Picture(subject, subject);

        }

        public override void Change()//이미지를 수정하는 기능

        {

            string image = subject.Replace(sharp,smooth);

            Picture= new Picture(subject,image);

        }

    }

}

 

REPreventBuilder.cs

namespace Builder

{

    class REPreventBuilder:PictureBuilder

    {

        const string red_eye = "RedEye";

        const string normal_eye = "NomalEye";

        const int max_length = 20;

        string subject;

 

        public override void SetSubject(string subject) //상을 맺히는 기능

        {

            if (subject.Length > max_length)

            {

                subject = subject.Substring(0, max_length);

            }

            this.subject = subject;

        }

       

        public override void Change()//이미지를 바꾸는 기능

        {

            string image = subject.Replace(red_eye, normal_eye);

            Picture = new Picture(subject, image);

        }

    }

}

 

Camera.cs

namespace Builder

{

    class Camera

    {

        PictureBuilder[] builders = new PictureBuilder[2];

        public Picture MyPicture

        {

            get;

            private set;

        }

        public Camera()

        {

            builders[0] = new REPreventBuilder();

            builders[1] = new SmoothBuilder();

            MyPicture = Picture.EmptyPicture;

        }

        public void PressAShutter(string origin,bool night)

        {

            PictureBuilder pb = null;

            if (night) //밤일 때

            {

                pb = builders[0];

            }

            else//밤이 아닐 때

            {

                pb = builders[1];

            }

 

            pb.SetSubject(origin); //상을 맺힌다.

            pb.Change(); //이미지를 수정한다.

            MyPicture = pb.Picture;

        }

    }

}

  

Program.cs

using System;

namespace Builder

{

    class Program

    {

        static void Main(string[] args)

        {

            Camera camera = new Camera();

            camera.PressAShutter("길 마당 RedEyeSharpBody 집 산 ",true);

            Console.WriteLine(camera.MyPicture.Image);

            camera.PressAShutter("길 마당 RedEyeSharpBody 집 산",false);

            Console.WriteLine(camera.MyPicture.Image);

        }

    }

}

 

▶실행 결과

길 마당 NomalEyeSharpBody

길 마당 RedEyeSmoothBody 집 산

 

0 Comments
댓글쓰기 폼