The realization of prototype design pattern

Source: Internet
Author: User
Tags constructor implement reference shallow copy
Implementation of design prototype design pattern

Implementing the Prototype design pattern



Download this article code

When I create an instance of a class that is complicated, we can use the prototype pattern. Rather than creating instances of many classes, you might as well make the appropriate modifications to use a copy of the original instance. Using the prototype mode, you can reduce the number of subclasses by cloning a prototype. The prototype pattern can reduce the number of instances of a class.

In this pattern, the object is created by cloning. We sometimes create a lot of subclasses, in addition to creating different objects through a lot of subclasses, we can just need a unique subclass that keeps a reference to the base class of each object and creates the object through that subclass. By passing arguments to the constructor of a subclass and cloning the object. Each object implements the Clone method, so it can be cloned. We can use the prototype pattern to reduce the number of subclasses by cloning prototypes.

Clones can be implemented by implementing the ICloneable interface. The only method in the ICloneable interface is clone and returns an instance of the new class.

ICloneable.Clone method signature
[VisualBasic] Function Clone () as Object
[C #] Object Clone ();




We have to understand that the clone () method is just a shallow copy (shallow copy), not a deep copy (Deep copy). So it simply returns a reference instead of creating a replicated instance like Deep copy. We can implement deep copy (Deep copy) by using the ISerializable interface.

Another disadvantage is that each subclass of the prototype must implement the Clone () method, and sometimes it is difficult to add the Clone method.

In this example, I built the Empdata class and implemented the ICloneable interface and the ISerializable interface. The ICloneable interface needs to implement the Clone method so that the class can be replicated. ISerializable interface to achieve deep copy of the Empdata class (Deep copy). The method used is to serialize the Empdata object to a file, or to deserialize the file into a Empdata object.

The Empdata class contains two methods: Getempdata and Changeempdata. The two methods are used to get the Empdata object as a string (a), and to change the Empdata class. Each method can be invoked to verify the difference between shallow copy (shallow copy) and deep copy (Deep copy). In shallow copy (shallow copy), if the Empdata class changes, the change also appears in the Empdata clone object, and in deep copy (Deep copy), if the Empdata object changes, This change does not occur in Empdata cloned objects.

The constructor of the Empdata class reads an XML file and creates an EMP object.




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.