본문 바로가기
C# 디자인패턴

06# Prototype 패턴 Creational Patterns

by NaHyungMin 2019. 5. 23.

 

 

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(25500);

    colorManager["green"= new Color(02550);

    colorManager["blue"= new Color(00255);

 

    colorManager["angry"= new Color(255540);

    colorManager["peace"= new Color(128211128);

    colorManager["flame"= new Color(2113420);

 

    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

 

Prototype .NET Design Pattern in C# and VB - dofactory.com

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.     The classes and objects participating in this pattern are: This structural code demonstrates the Prototype pattern in which new objec

www.dofactory.com