Object cloning-prototype (4)

Source: Internet
Author: User
7.5 introduction and implementation of the prototype Manager

Prototype Manager stores multiple prototype objects in one collection for clients. It is a factory dedicated to object cloning, A set is defined to store prototype objects. If you need to clone a prototype object, you can obtain it by copying the corresponding prototype objects in the set. Program the abstract prototype in the prototype manager for extension. Its structure is 7-8:

Figure 7-8 prototype mode with prototype Manager

The following describes the design and implementation of the prototype manager by simulating a simple document Manager:

Sunny software company has many documents to create, submit and approve in its daily work, for example, the feasibility analysis report, project proposal, Software Requirement Specification statement, and project progress report. In order to improve work efficiency, in the OA system, templates are created for all types of documents. You can use these templates to quickly create new documents. These document templates need to be managed in a unified manner, the system generates different new documents based on different user requests.


We use the prototype with the prototype manager to design the Document Manager. Its structure is 7-9:

 

Figure 7-9 Document Manager Structure

The following are some core code that implements this function. Considering the readability of the code, we have simplified all the classes:

Import java. util .*;

 

// The abstract document interface can also be defined as an abstract class. It provides the implementation of the clone () method and declares the business method as an abstract method.

Interface officialdocument extends cloneable

{

Public officialdocument clone ();

Public void display ();

}

 

// Feasibility Analysis Report class

Class far implements officialdocument

{

Public officialdocument clone ()

{

Officialdocument far = NULL;

Try

{

Far = (officialdocument) Super. Clone ();

}

Catch (clonenotsupportedexception E)

{

System. Out. println ("copying is not supported! ");

}

Return far;

}

Public void display ()

{

System. Out. println ("Feasibility Analysis Report");

}

}

 

// Software requirements specification

Class SRS implements officialdocument

{

Public officialdocument clone ()

{

Officialdocument SRS = NULL;

Try

{

SRS = (officialdocument) Super. Clone ();

}

Catch (clonenotsupportedexception E)

{

System. Out. println ("copying is not supported! ");

}

Return SRS;

}

Public void display ()

{

System. Out. println ("Software Requirement Specification");

}

}

 

// Prototype Manager (implemented using the hungry Chinese Singleton)

Class prototypemanager

{

// Define a hashtable for storing prototype objects

Private hashtable ht = new hashtable ();

Private Static prototypemanager PM = new prototypemanager ();


// Add a Document Object for hashtable

Private prototypemanager ()

{

Ht. Put ("far", new far ());

Ht. Put ("SRS", new SRS ());

}

// Add a new document object

Public void addofficialdocument (string key, officialdocument DOC)

{

Ht. Put (Key, DOC );

}

 

// Obtain the new document object through the shortest clone.

Public officialdocument getofficialdocument (string key)

{

Return (officialdocument) Ht. Get (key). Clone ();

}


Public static prototypemanager getprototypemanager ()

{

Return PM;

}

}

The client code is as follows:

Class Client

{

Public static void main (string ARGs [])

{

// Obtain the prototype manager object

Prototypemanager PM = prototypemanager. getprototypemanager ();

Officialdocument doc1, doc2, doc3, doc4;

Doc1 = PM. getofficialdocument ("far ");

Doc1.display ();

Doc2 = PM. getofficialdocument ("far ");

Doc2.display ();

System. Out. println (doc1 = doc2 );

Doc3 = PM. getofficialdocument ("SRS ");

Doc3.display ();

Doc4 = PM. getofficialdocument ("SRS ");

Doc4.display ();

System. Out. println (doc3 = doc4 );

}

}

Compile and run the program. The output result is as follows:

Feasibility Analysis Report

Feasibility Analysis Report

False

Specification for software requirements

Specification for software requirements

False

A hashtable collection object is defined in prototypemanager, and the "key-Value Pair" is used to store the prototype object. The client can use the key (such as "far" or "SRS ") to obtain the clone object of the corresponding prototype object. The prototypemanager class provides the getofficialdocument () method similar to the factory method to return a cloned object. In this example code, prototypemanager is designed as a singleton class and is implemented using a hungry Chinese Singleton to ensure that there is only one prototypemanager object in the system, which is conducive to saving system resources, and can better control the prototype manager objects.

 

Thoughts

If you need to add a new type of documents, such as the project progress report (PPR), how to modify the source code of the Document Manager System, and practice your Modification Scheme.

7.6 prototype mode Summary

Prototype is a method for quickly creating a large number of identical or similar objects. It is widely used in software development. Many software products provide copy (CTRL + C) and paste (CTRL + V) the operation is a typical application of the prototype mode. The following briefly summarizes the effects and applicability of the mode.

1. Main advantages

The main advantages of the prototype mode are as follows:

(1)
When creating a new object instance is complex, you can use the prototype mode to simplify the object creation process and copy an existing instance to improve the efficiency of creating a new instance.

(2)
Good scalability. Because the prototype is provided in the prototype mode, you can program the prototype on the client and write the prototype in the configuration file, adding or removing product categories has no impact on the original system.

(3)
The prototype mode provides a simplified creation structure. The factory method mode usually requires a factory level structure that is the same as the product class level structure, which is not required in the prototype mode, in prototype mode, product replication is implemented by cloning methods encapsulated in the prototype class, and no special factory class is required to create the product.

(4)
The object state can be saved by means of deep cloning, and the object state can be copied and saved in prototype mode, it can be used as needed (such as restoring to a certain historical State) to assist in undo operations.

2. Main disadvantages

The main disadvantages of the prototype mode are as follows:

(1)
You need to configure a clone method for each class, And the clone method is located inside a class. When modifying an existing class, you need to modify the source code, which violates the "Open and Close principle ".

(2)
Complex code is required for deep cloning. When multiple nested references exist between objects, in order to achieve deep cloning, the classes corresponding to each layer of objects must support deep cloning, it may be difficult to implement.

3. Applicable scenarios

You can consider using the prototype mode in the following cases:

(1)
Creating a new object is costly (for example, initialization takes a long time and occupies too many CPU resources or network resources ), the new object can be obtained through copying existing objects in prototype mode. If it is a similar object, You Can slightly modify its member variables.

(2)
If the system wants to save the object state, but the object state changes little, or the object itself occupies less memory, you can use the prototype mode and the memorandum mode to implement it.

(3)
Avoid using hierarchical factory classes to create hierarchical objects, and the instance objects of the class have only one or a few combined states, it is easier to get a new instance by copying the prototype object than creating a new instance by using the constructor.

 

Exercise

Design and implement a customer class, which contains a member variable named customer address. The customer address type is address, we use shortest cloning and deep cloning to replicate the customer object and compare the similarities and differences between the two methods.

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.