Follow the example learning design pattern (7)-prototype mode prototype (create type)

Source: Internet
Author: User
Tags abstract constructor tostring

Prototype mode is the creation mode.

Design intent: Use the prototype instance to specify the type of object to create and create a new object by copying the prototype.

We use the class diagram of the example that builds the CV to illustrate the prototype pattern.

Class Diagram:


The prototype pattern is mainly used for object replication, and its core is the prototype class prototype in the class diagram. Here we define an abstract prototype interface, declaring a method for cloning itself, where we have created a CV for a concrete prototype class, the prototype class needs to have the following two conditions: Implement the Cloneable interface. In the Java language, there is a cloneable interface, which only works by notifying the virtual machine at runtime that it can safely use the Clone method on classes that implement this interface. In a Java virtual machine, only classes that implement this interface can be copied, or clonenotsupportedexception exceptions will be thrown at run time. Override the Clone method in the object class. In Java, the parent class of all classes is the object class, and the object class has a Clone method that returns a copy of the object, but its scope is protected type, and the generic class cannot be called, so The prototype class needs to modify the scope of the Clone method to the public type.

Prototype mode is a relatively simple mode, it is very easy to understand, implement an interface, rewrite a method to complete the prototype mode. In practical applications, prototype patterns rarely appear alone. Often mixed with other patterns, his prototype class prototype is also used to replace the abstract class, where we add an interface to the prototype class, the interface class inherits the Cloneable interface, so that we can be more flexible in our own interface to control the logic of the specific prototype class.

Package com.prototype;

/**
 * @author Gaoxu
 * Practice a genuine knowledge.
 *
/public Interface Iprototype extends cloneable{public
	
	Prototype clone ();
	public void Show ();



Package com.prototype;

/** prototype abstract class, using the prototype abstract class, can make the concrete prototype class has more reusable property implementation
 * @author Gaoxu
 * Practice of knowledge.
 */Public
abstract class Prototype implements iprototype{
	String name = "";
	String mobile = "";
	Public String GetName () {
		return name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	Public String Getmobile () {
		return mobile;
	}
	public void Setmobile (String mobile) {
		this.mobile = mobile;
	}
	@Override Public
	Prototype Clone () {
		Prototype Prototype = null; 
		try {
			 prototype  = (prototype) super.clone ();
		} catch (Clonenotsupportedexception e) {
			
			E.printstacktrace ();
		}
		return prototype;
	}
	
	Public String toString () {
		return "mobile=" +mobile+ ", name=" +name;	
	}
}

Package com.prototype;

/**
 * CV prototype implementation Class
 * 
 * @author Gaoxu practice.
 *
/public class Resume extends Prototype {

	@Override public
	Void Show () {
		
		System.out.println ("" + ToString ());
	}

	
}

Package com.prototype;

/**
 * @author Gaoxu
 * Practice a genuine knowledge.
 *
/public class Client {

	/**
	 * @param args *
	 /public
	static void Main (string[] args) { C21/>resume Resume = new Resume ();
		Resume.setname ("Gao Xu");
		Resume.setmobile ("1390000111");
		
		Resume resume1 = (resume) resume.clone ();
		Resume resume2 = (resume) resume.clone ();
		
		System.out.println (Resume.tostring ());
		System.out.println (Resume1.tostring ());
		System.out.println (Resume2.tostring ());

	}

}
Advantages and scenarios for the prototype model:

The biggest advantage is that creating objects is better than new, because the Clone method of the object class is a local method that directly operates in-memory binary streams, especially when copying large objects, where performance is significantly different. So the scenario where you need to recreate the object is basically applicable.

Considerations for prototyping patterns copying an object using prototype mode does not invoke the constructor of the class. Because object replication is done by invoking the Clone method of the object class, it replicates the data directly in memory without reinitialization, directly gaining the state of the object when it is run. Not only does the code in the construction method not execute, but even access permissions are not valid for the prototype mode. In singleton mode, a singleton can be implemented as long as the access permission of the construction method is set to private type. However, the Clone method directly ignores the permissions of the constructor method, so the singleton mode is conflicting with the prototype mode, so it is not possible to copy the class object of the singleton pattern, so it is not a single case.

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.