static void Main(string[] args) { //BuilderExample(); //FactoryExample(); //FactoryMethodExample(); //FactoryMethodExample2();
ProtoTypeExample();
//Console.WriteLine("Hello World!"); Console.ReadKey(); }
static void ProtoTypeExample() { ColorManager colorManager = new ColorManager(); colorManager["red"] = new Color(255, 0, 0); colorManager["green"] = new Color(0, 255, 0); colorManager["blue"] = new Color(0, 0, 255);
colorManager["angry"] = new Color(255, 54, 0); colorManager["peace"] = new Color(128, 211, 128); colorManager["flame"] = new Color(211, 34, 20);
Color color1 = colorManager["red"].Clone() as Color; Color color2 = colorManager["peace"].Clone() as Color; Color color3 = colorManager["flame"].Clone() as Color; }
main.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.ProtoType { public class Color : IColorProtoType { public Int32 Red { get; private set; } public Int32 Green { get; private set; } public Int32 Blue { get; private set; }
public Color(Int32 red, Int32 green, Int32 blue) { this.Red = red; this.Green = green; this.Blue = blue; }
public IColorProtoType Clone() { Console.WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}", Red, Green, Blue);
return this.MemberwiseClone() as IColorProtoType; } } }
Color.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.ProtoType { public class ColorManager { private Dictionary<string, IColorProtoType> Dictionary = new Dictionary<string, IColorProtoType>();
public IColorProtoType this[string key] { get { return Dictionary[key]; } set { Dictionary.Add(key, value); } } } }
ColorManager.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Patterns.ProtoType { public interface IColorProtoType { IColorProtoType Clone(); } }
IColorProtoType.cs |
참고 사이트
https://www.dofactory.com/net/prototype-design-pattern
'C# 디자인패턴' 카테고리의 다른 글
08# Adapter 패턴 Structural Patterns (0) | 2019.05.29 |
---|---|
07# Singleton 패턴(thread safe) Creational Patterns (0) | 2019.05.24 |
05# Factory Method 패턴2 Creational Patterns (0) | 2019.05.23 |
04# Factory Method 패턴1 Creational Patterns (0) | 2019.05.23 |
03# 추상 팩토리(Abstract Factory) 패턴 Creational Patterns (0) | 2019.05.22 |