原型模式

The Prototype design pattern specifies the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

To simplify the above definition, we can say that, the Prototype Design Pattern gives us a way to create new or cloned objects from the existing object of a class. That means it clones teh existing object with its data into a new object. If we do any changes to the cloned object(i.e. new object) then it does not affect the original object.

Note: The Prototype Design Pattern is unique among the other Creational Design Pattern as it doesn't require a class instead it requires an object.

Points to Remember about Memberwiseclone Method in C#?

  • The MemberwiseClone method is part of the System.Object class and creates a shallow copy of the given object.
  • MemberwiseClone Method only copies the non-static fields of the object to the new object.
  • In the process of copying, if a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the reference object is not.

UML Class Diagram

 Prototype: This is going to be an interface or abstract class that is used for the types of objects that can be cloned itself.

 ConcretePrototyp: This is going to be the class that implements the Prototype abstract class or interface for cloning itself.

 Client: The client is nothing but the class that creates a new object by asking a prototype to clone itself.

原型模式(Prototype):使用原型实例创建指定类型的对象,通过拷贝原型创建新对象。

 1 abstract class Prototype
 2 {
 3      private string id;
 4      public string Id
 5      {
 6           get{return id;}
 7      }
 8 
 9      public Prototype(string id)
10      {
11              this.id = Id;
12      }
13 
14      public abstract Prototype Clone();
15 }
16 
17 
18 //具体原型类
19 class ConcreatePrototype: Prototype
20 {
21      public ConcreatePrototype(string id):base (id)
22      {}
23      public override Prototype Clone()
24      {
25           /*创建当前对象的浅表副本。方法是创建一个新对象,然后将当前对象的非静态字段复制到该对象。如果字段是值类型的,则对该对象执行逐位复制。如果字段是引用类型,则复制引用但不复制引用的对象;因此原始对象及其副本引用同一个对象*/
26           return (Prototype)this.MemberwiseClone();
27      }
28 }
View Code

克隆隐藏了对象创建的细节,同时大大的提高了性能。不用重新初始化对象,而是动态地获得对象运行时状体。

     MemberwiseClone()方法是这样的,如果字段是值类型的,则对该字段执行逐位复制,如果字段是引用类型,则复制引用但不复制引用的对象;因此原始对象及其副本引用同一对象。

In the case of Shallow copy, it will create the new object from the existing object and then copy the value type field of the current object to he new object. But in the case of reference type, it will only copy the refrence, not the reference object itself.

Therefore the orignial and clone refer to the same object in the case of reference type. 

浅表复制:被复制对象的所有变量都含有原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。

In the case of deep copy, it will create a new object from the existing object and then copy the filed of the current object to the newly created object. If the field is a value type, then a bit-by-bit copy of the filed will be peformed. If the field is a reference type, then a new copy of the refered object is going to be created.

深表复制:把引用对象的变量指向复制过来的新对象,而不是原来被引用的对象。

Differences Between  Shallow Copy and Deep Copy in C#

 Both Shallow copy and deep copy are used to create clone object from an exising object. The difference between them is that , in case of Shallow copy , it will create the clone object from the exiting object and then copy the value type field of the existing object to hte new object. But for the reference type property, it will only copy the reference, not the actual object itself. Therefore the exising and clone objects refer to the same memory locatlion in the case of reference type.

  On the other hand, in the case of Deep copy, it will create the clone object from teh existing object and then copy the fileds of the existing object to the newly created object. if the field is a value type, then a bit-by-bit copy of the field will be peformed, if the field is a reference type ,then a new copy of teh referred object is going to be created. That means, unlike the Shallo copy, in case of the Deep copy, both existing and clone objects have different memory locations for the refference type properties.

  public class Employee
    {
        public string Name { get; set; }
        public string Department { get; set; }
        public Address EmpAddress { get; set; }
        //Creating a Clone Object of the Current Object using Shallow Copy
        public Employee GetShallowCopy()
        {
            Employee employee = (Employee)this.MemberwiseClone();
            return employee;
        }
        //Creating a Clone Object of the Current Object using Deep Copy
        public Employee GetDeepCopy()
        {
            Employee employee = (Employee)this.MemberwiseClone();
            employee.EmpAddress = EmpAddress.GetClone();
            return employee;
        }
    }
    public class Address
    {
        public string address { get; set; }
        public Address GetClone()
        {
            return (Address)this.MemberwiseClone();
        }
    }
Shallow & Deep copy

 

深表复制的实现:

  1、提供Clone方法调用的私有构造函数,以便克隆数据

  2、在克隆对象的方法中调用私有构造函数,浅表复制其他变量

 

实例:Dataset提供Clone和Copy方法,Clone是浅表复制,只复制数据结构;Copy既复制结构,也复制数据。

 

posted @ 2019-05-19 12:09  云霄宇霁  阅读(97)  评论(0编辑  收藏  举报