.NET/설계 패턴(C#)

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

언제나휴일 2016. 4. 17. 13:46
반응형





본문 바로가기

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 집 산

 

반응형