Design Pattern Learning: Prototype model

Source: Internet
Author: User
Tags constructor shallow copy

The prototype pattern belongs to the creation pattern of the object, by giving a prototype object to indicate the type of object to create, and then creating more objects of the same type with the method of copying the prototype object. is to give you an existing object that you want to get a new object of the same type as this object, then copy the object and you can get it.

Replication of Java objects:

All classes in Java are inherited from the Java.lang.Object class, and the object class provides the following method for copying an object: Protected Object Clone ()

Subclasses can replace this method, and each class has a different case, and the permutation provides a clone method that satisfies itself. It is worth noting that there is a problem with object replication, where there is usually a reference to other objects within an object, that is, that other objects are part of the object, and that other object references to the object are copied at the same time when the object is copied using the Clone method of the object class.

The cloneable interface provided by Java is a function of telling the Java virtual machine that it is safe to use the Clone method on that class. However, the object itself does not implement the Cloneable interface, so invoking the Clone method throws a Clonenotsupportedexception exception if the class to be replicated does not implement the Cloneable interface.

Class Prototype implements Cloneable {//Implementation cloneable interface public
         Prototype clone () {//Replace the Clone method
                   according to your requirements Prototype Prototype = null;
                   try{
                            prototype= (prototype) Super.clone ();
                   } catch (Clonenotsupportedexception e) {
                            e.printstacktrace ();
                   }
                   Return prototype
         }
}

Prototype mode:

We generally say that the prototype, is a primary product, according to it, we have transformed our own products. It can be said that the prototype is a reference frame. Just like the plane model. Similarly, we have an object, this object is a prototype, hope according to it, get a new object, processing perfect, get a mature product. We want to use prototypes because we don't want to do something that we've already done. Prototype mode is not as complex as other creation patterns, it is simpler, the prototype class implements the interface, overrides the method, the specific subclass inherits the prototype class, and then the subclass is developed and perfected. The following is a UML class diagram of the prototype pattern

UML class Diagram :

See more Highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Code implementation

Interface Prototype extends cloneable
{//Implementation cloneable interface public
	Prototype clone ()//to the specific prototype subclass to implement
}

Class Concreteprototype implements Prototype
{public
	Prototype clone ()
	{//subclass implements its own clone method
		Prototype prototype = NULL;
		Try
		{
			prototype = (prototype) super.clone ();
		} catch (Clonenotsupportedexception e)
		{
			E.printstacktrace ();
		}
		SYSTEM.OUT.PRINTLN ("Successfully obtained a prototype copy");
		Return prototype
	}
}

Class Client
{
	Prototype Prototype = new Concreteprototype ();

	void Getprototype ()
	{
		Prototype pt = Prototype.clone ();//through prototype, get a new prototype
	}
} public

class Main
{public
	static void Main (string[] args)
	{
		new Client (). Getprototype ();
	}
}

When to use prototype mode:

When the process of creating an instance of a given class is cumbersome, complex, or expensive to consume, we need to use the prototype pattern.

Because the prototype pattern creates a new object, you want to copy and paste as easily as you like.

Precautions:

1 copying an object using prototype mode does not invoke the constructor method of the class. Because the replication of an object is done by invoking the Clone method of the object class, it replicates the data directly in memory, so the constructor method for the class is not invoked, and the code in the constructor is not executed.

2 deep copy and shallow copy issues. Deep replication is the object inside the object is still need to replicate, rather than simply want to shallow copy the address copy. Deep replication, Java can be implemented using serialization.

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.