Design Patterns (II) - prototype model

Prototype model is used to create the duplicate object, while ensuring performance, are created schema, provides the best way to create the object.

Intent: to create a prototype instance of the specified types of objects and create new objects by copying the prototype.

The main solution: create and delete prototype during operation.

Advantages: improved performance, avoid the constraint constructor.

        #region 原型模式
        public abstract class PrototypeClass {
            public PrototypeClass(string Name)
            {
                this.Name = Name;
            }

            public string Name { set; get; }

            public abstract PrototypeClass Clone();
        }

        public class Prototype1 : PrototypeClass
        {
            public Prototype1(string Name) : base(Name)
            {

            }
            public override PrototypeClass Clone()
            {
                return (PrototypeClass)this.MemberwiseClone();
            }
        }
        public class Prototype2 : PrototypeClass
        {
            public Prototype2(string Name) : base(Name)
            {

            }
            public override PrototypeClass Clone()
            {
                return (PrototypeClass)this.MemberwiseClone();
            }
        }
        #endregion
static void Main(string[] args)
        {
            Prototype1 prototype1 = new Prototype1("a");
            var PrototypeClass1 = prototype1.Clone();
            //PrototypeClass1.Name = "a"
            Prototype2 prototype2 = new Prototype2("b");
            var PrototypeClass2 = prototype2.Clone();
            //PrototypeClass2.Name = "b"
            Console.ReadKey();
        }

 You are can only feel when you can create objects of abstract prototype hide it, did not feel much effect ah, I may be too shallow to understand it, to find time to continue to look at tomorrow

October 25, 2019 00:30:31

Three weeks later, a design review mode every day, Go

Guess you like

Origin www.cnblogs.com/yuchenghao/p/11735889.html