▶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 집 산 |
'.NET > 설계 패턴(C#)' 카테고리의 다른 글
[설계 패턴 C#] 6. 적응자 패턴 (0) | 2016.06.09 |
---|---|
[설계 패턴 C#] 단일체 패턴(Singleton Pattern) (0) | 2016.04.17 |
[설계 패턴 C#] 원형 패턴(Prototype Pattern) (0) | 2016.04.17 |
[설계 패턴 C#] 팩토리 메서드 패턴(Factory Method Pattern) (0) | 2016.04.17 |
[설계 패턴 C#] 추상 팩토리 패턴 (Abstract Factory Pattern) (0) | 2016.04.17 |