Design Mode (4)-object Creation Mode-Prototype mode

Source: Internet
Author: User

1. Object Creation Mode 1.4 requirements for Protoype mode 1.4.1

Create a new object by copying the original object.

1.4.2 Structure

? P r o t y p e (Gr a p h I c)

-Declare an interface for cloning itself.

? C o n c r e t e P r o t y p e (S t a ff, W h o l e N o t e, H a l fN o t e)

-Perform a clone operation.

? C l I e n t (G r a p h I c To o l)

-Let a prototype clone itself to create a new object.

1.4.3 example-C ++

// Prototype. h
# Ifndef _ PROTOTYPE_H _
# Define _ PROTOTYPE_H _

Class Prototype
{
Public:
Virtual ~ Prototype ();
Virtual Prototype * Clone () const = 0;
Protected:
Prototype ();
Private:
};

Class ConcretePrototype: public Prototype
{
Public:
ConcretePrototype ();
ConcretePrototype (const ConcretePrototype & cp );
~ ConcretePrototype ();
Prototype * Clone () const;
Protected:
Private:
};

# Endif //~ _ PROTOTYPE_H _

 

Code snippet 2: Prototype. cpp

// Prototype. cpp

# Include "Prototype. h"
# Include
Using namespace std;
Prototype: Prototype ()
{
}

Prototype ::~ Prototype ()
{
}

Prototype * Prototype: Clone () const
{
Return 0;
}

 

ConcretePrototype: ConcretePrototype ()
{
}

ConcretePrototype ::~ ConcretePrototype ()
{
}

ConcretePrototype: ConcretePrototype (const ConcretePrototype & cp)
{
Cout <"ConcretePrototype copy..." < }

Prototype * ConcretePrototype: Clone () const
{
Return newConcretePrototype (* this );
}

// Main. cpp
# Include "Prototype. h"
# Include
Using namespace std;

Int main (int argc, char * argv [])
{
Prototype * p = newConcretePrototype ();
Prototype * p1 = p-> Clone ();
Return 0;
}

Note: This document only describes the concept and does not involve C ++'s common deep copy problems.

1.4.4 example-JAVA

In Java, the prototype mode can be easily implemented. As long as the Cloneable flag interface is implemented, the clone () method in the interface is overwritten, you can "clone" any object of this implementation class.

Class ConcretePrototype02 implements Cloneable {
Private String name;
Private ArrayList NameList = new ArrayList ();

Public ConcretePrototype02 (String name ){
This. name = name;
This. nameList. add (this. name );
}
// Add an object in nameList
Public void setName (String name ){
This. nameList. add (name );
}

Public ArrayList GetNameList (){
Return this. nameList;
}

// Overwrite the clone () method in the Object base class, expand the access permission of the method, and return the specific type
Public ConcretePrototype02 clone (){
ConcretePrototype02self = null;
Try {
Self = (ConcretePrototype02) super. clone ();
// The following sentence is the key to deep copy.
// Self. nameList = (ArrayList ) This. nameList. clone ();
} Catch (CloneNotSupportedException e ){
E. printStackTrace ();
}
Return self;
}
}

// Test class
Public class Client {
Public static void main (String [] args ){
ConcretePrototype02prototype02 = new ConcretePrototype02 ("ant ...");
System. out. println (prototype02.getNameList ());

// Obtain a copy Through clone
ConcretePrototype02fromClone02 = prototype02.clone ();
FromClone02.setName ("little ant ...");
System. out. println (fromClone02.getNameList ());
System. out. println (prototype02.getNameList ());
}
}

Test results:

Copy the previous prototype: [Ant...]

The Copied object: [Ant..., Ant...]

Prototype after copying: [Ant..., Ant...]

The ArrayList held by the original object after the copy is found. The nameList reference of the type changes with the execution of the setName () method of the copied fromClone object. This is not the expected result, this means that the prototype and the Copied object share the same referenced variable, which is not thread-safe. After removing the annotated statement in the clone () method above, we can test the result as follows:

Copy the previous prototype: [Ant...]

The Copied object: [Ant..., Ant...]

Prototype after copying: [Ant...]

 

It is quite simple to use Prototype in Java. You only need to remember a few points of attention and you can easily implement this mode. Since the clone () method is used to copy an object for IO read/write from the binary stream in the memory, copying an object will not execute the constructor of the class corresponding to the object. Summary:

1. the constructor will not be executed;

2. If a referenced variable (an array is also an object) exists in the member variable of the class, the default clone () will not copy it, and you need to provide your own deep copy;

 

Related Article

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.