Design Patterns-Prototype Method)

Source: Internet
Author: User

Problem:
InProgramDuring the design, we may copy the structure of an object, the object value, and dynamically obtain the running state of the object due to a specific need, the simplest solution is to create a new object, and assign the attribute values of the object to the new object one by one. In this way, it is too complicated to list the attribute values of the object one by one, in addition, it is prone to errors, and the assignment of a property is dropped. Whether the object itself can provide a self-replication function, the client simply needs to call this method to complete object replication.

Definition:

Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.

Intent:
The prototype mode allows an existing object to create another custom object by copying itself through the clone interface (custom attribute values and then copying them in batches ), there is no need to know how to create details. Clone ).
The clone () implementation is related to the specific implementation language .. Net generally uses the memberwiseclone () method of the object to create a superficial copy of the object for shallow replication.
Participants:
• Prototype (Abstract prototype) role:
Abstract prototype role: This is an abstract role, which is usually implemented by an interface or abstract class. This role provides all the interfaces required for the specific prototype class (. NET provides an icloneable interface that defines the clone () method as a general prototype in the system ).

• Concreteprototype (Abstract prototype) role:
The Copied object. This role must implement the interfaces required by the abstract prototype role. Implements specific object replication methods.

UML:

 

Shallow replication and deep replication:

• Shallow copy: If a field is of the value type (including string), perform a one-on-one copy of the field. If the field is of the reference type, copy the referenced object without making the reference. Therefore, the original object and its counterparts reference the same object. The object can be implemented through the memberwiseclone () method.
• Deep copy: If a field is of the value type, perform a one-on-one copy of the field. If the field is of the reference type, the variables of the copied referenced object point to the copied new object, instead of the original referenced object. This allows subclass to inherit from several interfaces of the prototype role to implement the clone () method. You can use the layer-by-layer clone and assign value to reference type fields.

Instance description:

Nokia mobile phone Factory
Multiple Mobile Phones of the same model do not need to be produced at the production factory every time. You can copy the one that has been produced.

The UML diagram is as follows:

 

Code:

Shallow copy:

///   <Summary>
/// Abstract prototype role
///   </Summary>
Public Interface Iprototypephone
{
Object Clone ();
}
///   <Summary>
/// Prototype role
///   </Summary>
Public Class Concreteprototypephone: iprototypephone
{
Public String Name { Get ; Set ;}
Public String CPU { Get ; Set ;}
Public String MB { Get ; Set ;}
Public Float Memorysize { Get ; Set ;}
Public String System { Get ; Set ;}

Public ObjectClone ()
{
//Use the built-in memberwiseclone () method to clone an object
Return This. Memberwiseclone ();
}
}
Public VoidPrototypetest ()
{

Concreteprototypephone OBJ = New Concreteprototypephone ();
OBJ. CPU = " Arm_27 " ;
OBJ. MB = " N8_1op " ;
OBJ. memorysize = 375 ;
OBJ. Name = " N8 " ;
OBJ. System = " Saipan " ;
VaR N8 = obj. Clone ();
}

Deep replication:

///   <Summary>
/// The specific prototype role implements the general abstract prototype role system. icloneable provided by. net.
///   </Summary>
Public Class Concreteprototypephone: system. icloneable
{
Public String Name { Get ; Set ;}
Public Concreteprototypecpu CPU { Get ; Set ;}
Public String MB { Get ; Set ;}
Public Float Memorysize { Get ; Set ;}
Public String System { Get ; Set ;}

Public Object Clone ()
{
// View cloned parent object
Concreteprototypephone OBJ = (concreteprototypephone) This . Memberwiseclone ();
// Clone the sub-object and point the reference of the sub-object to the new cloned object.
OBJ. CPU = (concreteprototypecpu) This . CPU. Clone ();
Return OBJ;
}
}
Public Class Concreteprototypecpu: system. icloneable
{
Public String Name { Get ; Set ;}
Public Float Frequency {Get ; Set ;}
Public Object Clone ()
{
Return This . Memberwiseclone ();
}
}
Public Void Prototypetest ()
{

Concreteprototypephone OBJ = New Concreteprototypephone ();
OBJ. CPU = New Concreteprototypecpu () {frequency = 2.4f , Name = " Ar700 " };
OBJ. MB = " N8_1op " ;
OBJ. memorysize = 375 ;
OBJ. Name = " N8 " ;
OBJ. System = " Saipan " ;
Concreteprototypephone n8 = (concreteprototypephone) obj. Clone ();
N8.cpu =New Concreteprototypecpu () {frequency = 2.0f , Name = " Ar70 " };
}

 Advantages:
• You can dynamically obtain the running state of an object without re-initializing the object.
• The copy details of objects are shielded. The client does not need to care about the copy details. Copying objects is closed inside objects. How to copy objects depends on the objects themselves.
• In some environments, copying an object is more effective than creating a new object because the object constructor will not execute it.
Disadvantages:
• Implementing deep replication is complicated.

Application Scenario:
• Copy the structure and data of an object.
• Modification to the target object does not affect existing prototype objects.
• When the object creation cost is large (initialization takes a long time and consumes too much CPU resources or network resources)
PS:
• When memberwiseclone is called for shortest replication, the prototype mode is a copy of the memory binary stream, and the constructor will not execute it.

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.