Java Technology _ Daily Master a design pattern (006) _ Use scenarios and simple examples (create: prototype mode) __ design mode

Source: Internet
Author: User

Java Technology _ Daily Master a design pattern (001) _ Design pattern concept and classification

Java Technology _ Daily Master a design pattern (002) _ Use Scenarios and simple examples (create: Single case mode)

Java Technology _ Daily Master a design pattern (003) _ Use Scenarios and simple examples (create: Factory method)

Java Technology _ Daily Master a design pattern (004) _ Use Scenarios and simple examples (create: Abstract Factory)

Java Technology _ every day to master a design pattern (005) _ Use Scenarios and simple examples (create type: construction mode)

Java Technology _ Daily Master a design pattern (006) _ Use scenarios and simple examples (create: prototype mode)


1. Pattern Description

Use a prototype instance to specify the type of object to create and create a new object by copying it. 2. Mode function

Can be decoupled to some extent, the construction process of consumer and object is isolated, and how the object is constructed is completely unrelated to the consumer.

can improve efficiency to some extent, complex object construction often takes a long time (the middle may carry on complex operation or database interaction), clone consumes resources in general will be much less.
Can increase the encapsulation of code to a certain extent, avoid the complicated construction process.

Wait a minute. TODO 3. Applicable scenarios

You need to create a complex, construction-time-consuming object that already has a homogeneous object.

Consumers do not care about the process of object construction.

Wait a minute. Todo

Example: Rebuilding of workflow instances, replication of complex data entities
4. Model Elements

the class is to support cloning. This class implements the Cloneable interface.

an instance of this class already has . This instance is used as a prototype for cloning.
The Clone method for this class should be a deep clone. in this class, except for the basic types (and their encapsulated classes), string (in fact, a shallow clone) in 8, but if you look at a string, you'll find that the result is consistent with the deep clone, which we think is deep clone. Or use a streaming clone method for deep clone. 5. Class Diagram
6. Pattern Instance Code

Prototype:

Package com.demoFound.prototype;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import java.io.Serializable;

Import java.util.List; /** * Prototype Mode _ Prototype class * * * @author POPKIDORC * */public class Myprototypeprocessinstance implements Serializable, Cloneabl e {///only if the Clonealbe interface is implemented, the virtual machine will think that clone is available, or it will throw clonenotsupportedexception//implementation serializable to allow for streaming clones.

	The clone is deep clone private static final long serialversionuid = 1L;

	Private String Processinstanceid;

	Private String Processtitle;

	Private String Processdefinitionid;

	Private String Processstatus;

	Private list<myprototypetaskinstance> taskinstances;
	Public String Getprocessinstanceid () {return processinstanceid;
	} public void Setprocessinstanceid (String processinstanceid) {This.processinstanceid = Processinstanceid;
	Public String Getprocesstitle () {return processtitle; } PUBlic void Setprocesstitle (String processtitle) {this.processtitle = Processtitle;
	Public String Getprocessdefinitionid () {return processdefinitionid;
	} public void Setprocessdefinitionid (String processdefinitionid) {This.processdefinitionid = Processdefinitionid;
	Public String Getprocessstatus () {return processstatus;
	} public void Setprocessstatus (String processstatus) {this.processstatus = Processstatus;
	Public list<myprototypetaskinstance> gettaskinstances () {return taskinstances; } public void Settaskinstances (list<myprototypetaskinstance> taskinstances) {this.taskinstances = TaskInstances
	; @Override public Myprototypeprocessinstance Clone () throws Clonenotsupportedexception {//Shallow clone, which is still

		However, the reference, 2 objects using a taskinstances//return (myprototypeprocessinstance) Super.clone ();
		Deep clone, traditional way//myprototypeprocessinstance Myprototypeprocessinstanceclone =//(myprototypeprocessinstance) Super . clone(); Non-8 base type or string, you need to clone//list<myprototypetaskinstance> by new and then for its copy value
		Myprototypetaskinstancesclone = new//arraylist<myprototypetaskinstance> (); for (myprototypetaskinstance myprototypetaskinstance:this//. Gettaskinstances ()) {//MYPROTOTYPETASKINSTANCESCL
		One.add (Myprototypetaskinstance.clone ());
		///Myprototypeprocessinstanceclone//. Settaskinstances (Myprototypetaskinstancesclone);

		return myprototypeprocessinstanceclone;
		Deep clone, Fluidization way, needs to realize serializable bytearrayoutputstream Bytearrayoutputstream = new Bytearrayoutputstream ();
		ObjectOutputStream ObjectOutputStream;
			try {objectoutputstream = new ObjectOutputStream (bytearrayoutputstream);

			Write Stream Objectoutputstream.writeobject (this);
			Bytearrayinputstream Bytearrayinputstream = new Bytearrayinputstream (Bytearrayoutputstream.tobytearray ());
			ObjectInputStream ObjectInputStream = new ObjectInputStream (bytearrayinputstream); Read out stream returN (myprototypeprocessinstance) objectinputstream.readobject ();
		catch (IOException e) {System.out.println ("==ioexception==");
		catch (ClassNotFoundException e) {System.out.println ("==classnotfoundexception==");
	return null; }
}
Package com.demoFound.prototype;

Import java.io.Serializable;

/** *
 Prototype Mode _ Prototype class
 * * @author POPKIDORC * */public
class Myprototypetaskinstance implements Serializable, cloneable {

	private static final long serialversionuid = 1L;

	Private String Taskinstanceid;

	Private String Taskinstanceuser;

	Public String Gettaskinstanceid () {return
		Taskinstanceid;
	}

	public void Settaskinstanceid (String taskinstanceid) {
		This.taskinstanceid = Taskinstanceid;
	}

	Public String Gettaskinstanceuser () {return
		taskinstanceuser;
	}

	public void Settaskinstanceuser (String taskinstanceuser) {
		this.taskinstanceuser = taskinstanceuser;
	}

	@Override Public
	Myprototypetaskinstance Clone () throws Clonenotsupportedexception {return
		( myprototypetaskinstance) Super.clone ();
	}


Consumers:

Package com.demoFound.prototype;

Import java.util.ArrayList; /** * Prototype Mode _ Consumer class * * * @author POPKIDORC * */public class Myprototypemain {private static Myprototypeprocessinstan

	CE myprototypeprocessinstance; private static void Initmyprototypeprocessinstance () {//Constructs a myprototypeprocessinstance and assigns a value to it myprototypeprocessinst
		ance = new Myprototypeprocessinstance ();
		Myprototypeprocessinstance.setprocessdefinitionid ("PROC001");
		Myprototypeprocessinstance.setprocessinstanceid ("PROCINST00001");
		Myprototypeprocessinstance.setprocessstatus ("S0102");
		Myprototypeprocessinstance.setprocesstitle ("process instance test");
		arraylist<myprototypetaskinstance> taskinstances = new arraylist<myprototypetaskinstance> ();
		Myprototypetaskinstance MyPrototypeTaskInstance1 = new Myprototypetaskinstance ();
		Myprototypetaskinstance1.settaskinstanceid ("TASK00001");
		Myprototypetaskinstance1.settaskinstanceuser ("testUser001");
		Taskinstances.add (MyPrototypeTaskInstance1); There's no clone here., direct new Myprototypetaskinstance MyPrototypeTaskInstance2 = new Myprototypetaskinstance ();
		Myprototypetaskinstance2.settaskinstanceid ("TASK00002");
		Myprototypetaskinstance2.settaskinstanceuser ("testUser002");
		Taskinstances.add (MYPROTOTYPETASKINSTANCE2);
	Myprototypeprocessinstance.settaskinstances (taskinstances);
		
		public static void Main (string[] args) {initmyprototypeprocessinstance ();
		Start clone myprototypeprocessinstance myprototypeprocessinstanceclone = null;
			try {myprototypeprocessinstanceclone = myprototypeprocessinstance. Clone ();
			Only the instance ID, state, and task user changes myprototypeprocessinstanceclone. Setprocessinstanceid ("PROCINST00002");
			Myprototypeprocessinstanceclone.setprocessstatus ("S0101");
			Myprototypeprocessinstanceclone.gettaskinstances (). Get (0). Settaskinstanceuser ("testUser003");
		Myprototypeprocessinstanceclone.gettaskinstances (). Get (1). Settaskinstanceuser ("testUser004"); catch (Clonenotsupportedexception e) {SyStem.out.println ("Clonenotsupportedexception"); ///ratio result System.out.println ("==myprototypeprocessinstance==" + Myprototypeprocessinstance.getprocessinstanceid ())
		; for (Myprototypetaskinstance myprototypetaskinstance:myprototypeprocessinstance. Gettaskinstances ()) {System.out
		. println ("==myprototypeprocessinstance==" + myprototypetaskinstance.gettaskinstanceuser ()); } System.out.println ("==myprototypeprocessinstanceclone==" + Myprototypeprocessinstanceclone.getprocessinstanceid
		()); If the shallow clone can see taskinstances as a memory, only 2 objects all refer to Taskinstances, if deep clone, the memory for the taskinstances of 2 objects is allocated respectively for ( Myprototypetaskinstance Myprototypetaskinstance:myprototypeprocessinstanceclone. GetTaskInstances ()) {System.out
		. println ("==myprototypeprocessinstanceclone==" + myprototypetaskinstance.gettaskinstanceuser ()); }
	}
}


Click to enter Ooppookid's blog

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.