Old saying: Prototype

Source: Internet
Author: User

Intent:

Specify the object type to be created by providing a prototype object, and then create more objects of the same type by copying the prototype object. The Prototype mode allows an object to create another custom object without any details about how to create it.

Prototype mode classification:

1: simple form.

The structure is as follows:

 

Structure Description:

1: customer role: the customer class initiates a request to create an object.

Prototype _ Prototype = new ConcretePrototype ();
// The object obtained through the prototype mode
// Some parts are shortest copies.

ConcretePrototype _ ConcretePrototypeCloned =

(ConcretePrototype) _ Prototype. PrototypeClone ();

 

2: Abstract prototype role: This is an abstract role, usually implemented by a C # interface or abstract class. This role provides all the interfaces required for the specific prototype class. In C #, the abstract prototype role usually implements the ICloneable interface.

/// <Summary>
/// Prototype role
/// Defines a clone method
/// </Summary>
Public interface Prototype
{
Prototype PrototypeClone ();
}

 

3: Specific prototype role: the Copied object. This role must implement the interface required by the abstract prototype role.

/// <Summary>
/// A Prototype role
/// Cloned object
/// </Summary>
[Serializable]
Public class ConcretePrototype: Prototype
{
/// <Summary>
/// Modify attributes
/// </Summary>
Public string sName
{
Get;
Set;
}
/// <Summary>
/// Shallow copy
/// </Summary>
/// <Returns> </returns>
Public Prototype PrototypeClone ()
{
// Use MemberwiseClone, the Object's protected method, to implement shallow copy.
Return (Prototype) this. MemberwiseClone ();
}
/// <Summary>
/// Deep copy
/// </Summary>
/// <Returns> </returns>
Public Prototype PrototypeDeepClone ()
{
// Clone object
ConcretePrototype _ ConcretePrototype;
MemoryStream memoryStream = new MemoryStream ();
BinaryFormatter formatter = new BinaryFormatter ();
// Serialize the original object
Formatter. Serialize (memoryStream, this );
MemoryStream. Position = 0;
// Assign the deserialization of the serialized original object to the cloned object

_ ConcretePrototype =

(ConcretePrototype) formatter. Deserialize (memoryStream );

 

Return _ ConcretePrototype;
}
}

 

2: Registration Form

The structure is as follows:

 

Structure Description:

1: Client role: the Client class sends a request to the prototype manager to create an object.

Prototype _ Prototype = new ConcretePrototype ();
// The object obtained through the prototype mode
// Some parts are shortest copies.

ConcretePrototype _ ConcretePrototypeCloned =

(ConcretePrototype) _ Prototype. PrototypeClone ();

 

// Instantiate the prototype Manager

PrototyManager <ConcretePrototype> _ PrototyManager

= New PrototyManager <ConcretePrototype> ();

 

// Add a copied prototype to the prototype manager.
_ PrototyManager. add (_ ConcretePrototypeCloned );
String sName = _ PrototyManager. get (0). sName;

 

2: Prototype role: This is an abstract role, usually implemented by a C # interface or abstract class. This role provides all the interfaces required for the specific prototype class. It is the same as a simple method.

3: The Prototype role is the Copied object. This role must implement the interfaces required by the abstract prototype role. It is the same as a simple method.

4: Prototype Manager role: Creates objects of a specific Prototype class and records each created object.

/// <Summary>
/// Prototype Manager
/// </Summary>
/// <Typeparam name = "T"> clone object </typeparam>
Public class PrototyManager <T>
{
// Clone Object List
Private List <T> _ list = new List <T> ();
/// <Summary>
/// Add a cloned object to the prototype Manager
/// </Summary>
/// <Param name = "t"> </param>
Public void add (T t)
{
_ List. Add (t );
}
/// <Summary>
/// Read a cloned object
/// </Summary>
/// <Param name = "I"> index of the cloned object in the prototype manager </param>
/// <Returns> </returns>
Public T get (int I)
{
Return _ list [I];
}
}

 

 

C # application of the prototype:

1: The MemberwiseClone () method of the Object class is used to copy objects in a superficial manner. For the sample code, refer to the Code in the previous prototype role.

2: Deep copy is achieved through serialization. For the sample code, refer to the Code in the previous prototype role.

3: implement the ICloneable interface. The ICloneable interface has a Clone method. You can override the custom Clone method.

Shallow copy and deep copy:

1: The light copy means that all the variables of the Copied object are the same as those of the original object, and all references to other objects still point to the same variable, that is to say, the shortest copy only copies all value types and all reference types, but does not copy the referenced object. For example, an original object contains a struct and a class field, only copy the references of struct and class. The class fields in the cloned object and the class fields in the original object direct to the same object. They are in a shared relationship and any party may modify the object, the struct is different. The clone object and the original object are allowed to have different values.

2: Deep copy means that all the variables of the Copied object are the same as those of the original object. Except for all the referenced variables, the cloned object copies all the referenced objects of the original object once, the reference variable in the cloned object and the reference variable in the original object do not point to the same object, but to the new object. The deep copy is like a new object.

Relationship between the prototype and the Copied object:

1: The Copied object is not the same as the original object. The running result of the following code is false;

// Whether the prototype is equal to the copied Product
// The result is false.
Bool required sobject = _ PrototyManager. get (0). Equals (_ ConcretePrototype );

2: The Copied object is of the same type as the original object. The running result of the following code is true;

// Whether the prototype and the replication product type are equal
// The result is true.

Bool connector stype =

_ PrototyManager. get (0). GetType (). Equals (_ ConcretePrototype. GetType ());

Applicability:

1: the client does not care how the called object is created, but what is the internal structure.

2: to avoid creating a factory class level parallel to the product class level (a factory class can have multiple factory methods to create different products, when a factory class has only one factory method, the factory class and its product interface are in a parallel layer. To avoid creating too many factory classes, resulting in complicated structure, it can be in the prototype mode ).

3: the product category of a system is dynamically loaded;

Advantages of the Prototype mode include:

1. The Prototype mode allows you to dynamically add or remove product classes. Because the method used to create a product-class instance is internal to the production and batch class, adding a new product does not affect the entire structure.

2. The Prototype mode provides a simplified creation structure. The factory method mode usually requires a hierarchical structure that is the same as the product class level structure, but the Prototype mode does not.

3. The Portotype mode can dynamically load new functions for an application. Because Prototype is highly independent, it is easy to dynamically load new features without affecting the old system.

4. Product classes do not need to have any pre-determined level structure, because the Prototype mode applies to any level structure.

Disadvantages of Prototype mode:

Each class must have a clone method. In addition, this cloning method requires a comprehensive consideration of the functions of the class, which is not very difficult for the brand-new class, but it is not always easy to transform the existing class.

Summary:

This article analyzes the two prototype modes: simple mode and registration form. The application of generic type in the prototype manager in the Registration Form solves the performance problems brought about by the disassembly box. By knowing the prototype, you can easily understand the essence of deep copy and shallow copy in C.
Note:
Reference: <Java and mode>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.