C # Design Patterns --- prototype mode

Prototype Model Description

Designated prototype object types and instances created by copying the prototype to create new objects, for creating duplicate objects, while ensuring performance. This interface is used to create a clone of the current object. When the direct cost of creating objects of relatively large, using this model.
Here Insert Picture Description
Prototype mode:

  1. Obtaining a dynamic operating state of the class, to avoid the time-consuming new instantiation;
  2. Do copy the object
    prototype model copy just a shallow copy, it does not copy a reference type
  3. Achieve deep copy
    make reference type implements a Clone method to achieve this manually deep copy;
    C # through the BinaryFormatter FIG binary object serialization evolution;
And constructed by instantiating a new object class is different, a new object prototype model is generated by copying an existing object. Achieve the Cloneable shallow copy, rewriting, a deep copy is read by implementing the binary stream Serializable.

C # prototype model Demo

Here Insert Picture Description

using System;
using System.Collections.Generic;

namespace Prototype
{
    class Program
    {
        static void Main(string[] args)
        {
            var person = new Person()
            {
                Name = "jack",
                Age = 20,
                Address = new Address()
                {
                    Province = "陕西",
                    City = "渭南"
                }

            };

            var person2 = (Person)person.Clone();
            person2.Address = (Address)person.Address.Clone();
        }
    }

    /// <summary>
    /// Prototype抽象类
    /// </summary>
    public abstract class Prototype
    {
        public abstract object Clone();
    }

    /// <summary>
    /// Person类
    /// </summary>
    public class Person : Prototype
    {
        private string name;
        private int age;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }

        public Address Address
        {
            get;set;
        }

        public Person()
        {
            Console.WriteLine("当前构造函数被执行:{0}", DateTime.Now);
        }

        public override object Clone()
        {
            return this.MemberwiseClone();
        }
    }

    /// <summary>
    /// 地址类
    /// </summary>
    public class Address : Prototype
    {
        public string City
        {
            get => default;
            set
            {
            }
        }

        public string Province
        {
            get => default;
            set
            {
            }
        }

        public override object Clone()
        {
            return this.MemberwiseClone();
        }
    }
}

Published 18 original articles · won praise 17 · views 2691

Guess you like

Origin blog.csdn.net/chasinghope/article/details/104225567