Prototype: Use a prototype instance to specify the object type and copy the prototype to create a new object.
Prototype, Prototype class, declares an interface for cloning itself
ConcretePrototype1 and ConcretePrototype2 are specific prototype classes to implement a clone operation.
Client, call, let a prototype clone itself to create a new object
In fact, the prototype mode is to create another custom object from an object without knowing any details of the Creation.
Prototype design code
ID {; Prototype (. ID =
ConcretePrototype
ConcretePrototype1 (Prototype)
This. MemberwiseClone () of the Clone method in its implementation class creates a superficial copy of the current object.
You can create a new object and copy the non-static fields of the current object to the new object. If the field is of the value type, perform a one-on-one copy of the field. If the field is of the reference type, copy the referenced object but do not copy the referenced object. Therefore, the original object and its copy reference the same object.
Client call
Main (= ConcretePrototype1 (=
Running Effect display
But. NET, Prototype is unnecessary for the Prototype abstract class, because cloning is too common, so. NET provides the ICloneable interface in the System namespace, which is the only method to Clone (). In this way, you only need to implement this interface to complete the prototype mode.