There are 23 modes in the design pattern. This is just for one purpose: decoupling + decoupling + decoupling ... ( cohesion poly-low coupling satisfies the opening and closing principle)
Introduced:
Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.
Why use prototype:
1. The root cause of the prototype introduction is that it can take advantage of a prototype object (in this case, I mean an instance, not a class) and quickly generate a batch of instances like the prototype object. For instance,
If you have a class A and it has 10 member variables, now you're going to generate 100 instances of a, and the values for those instances are mostly the same (say seven identical), and only a few are different (say three),
So if there's no prototype, then you have to do a pair of new aces each time, then assign values, so you have to repeat the assignment of the same seven variables 100 times, which is obviously cumbersome. Now that you have a prototype,
So the problem is simple, you just have to generate an instance of a, and then clone to generate other instances, and then modify the other instances of different places.
2. For example, the mail copy function.
Pattern Analysis:
All Java classes inherit the Java.lang.Object, and the Object class provides a clone () method. Therefore, you can invoke the Clone () method of the object class directly in Java to implement the cloning of the objects.
Let's talk about how to do this before we start to understand a concept. Set is shallow copy with deep copy.
Shallow copy and deep copy:
1. Shallow copy (Shallow clone):
All the variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object.
In other words, a shallow copy simply duplicates the object being considered, not the object it refers to.
2. Deep copy (Deep clone):
All the variables of the copied object contain the same values as the original object, removing the variables that refer to other objects. Variables that refer to other objects will point to new objects that have been copied, not those that are already referenced.
In other words, a deep copy copies the objects that are referenced by the object being copied again.
1. Shallow copy (Shallow clone) UML:
This example is when we are in charge of the mail. Do not copy attachments (Attachment)
Again, all Java classes inherit the Java.lang.Object, and do not write extends Object in the code.
Super.clone () is required to invoke the Clone () method;
2. deep Copy (Deep clone) UML:
"Shallow Clone"
Public Object Clone () { null; Try { = (Husband)Super. Clone (); } Catch (clonenotsupportedexception e) { e.printstacktrace (); } finally { return husband; } }
"Deep Clone"
PublicObject Deepclone ()throwsioexception,classnotfoundexception {//write an object into the streamBytearrayoutputstream BOS =NewBytearrayoutputstream (); ObjectOutputStream Oos=NewObjectOutputStream (BOS); Oos.writeobject ( This); //read it back from the stream.Bytearrayinputstream bis =NewBytearrayinputstream (Bos.tobytearray ()); ObjectInputStream Ois=NewObjectInputStream (bis); returnOis.readobject (); }
Shallow copy and deep copy specific code reference: http://www.cnblogs.com/xing901022/p/4051097.html
The prototype of design pattern