Quote Encyclopedia:
Use the prototype instance to specify the type of object to create and create new objects by copying them. The prototype prototype pattern is a creation design pattern that allows an object to create another customizable object without having to know any details of how it was created, working by passing a prototype object to the object to be created. The object to launch is created by requesting the prototype object to copy themselves. What to solve: Its main problem is the creation of "some complex objects"; These objects often face drastic changes due to changes in demand, but they have a more stable and consistent interface.
How to Solve: Use an existing prototype object to quickly generate the same instance as the prototype object.
Key code: 1, implement cloning operation, inherit cloneable in JAVA, rewrite clone (). 2. The prototype pattern is also used to isolate the coupling between the user of the class object and the specific type (variable Class), and it also requires the "variable class" to have a stable interface.
Advantages: 1, improve performance. 2, evade the constraint of the constructor function.
Disadvantages: 1, equipped with the cloning method requires a comprehensive consideration of the function of the class, which is not very difficult for the new class, but it is not necessarily easy for the existing class, especially when a class reference does not support serialization of indirect objects, or the reference contains a looping structure. 2, must implement Cloneable interface. 3, evade the constraint of the constructor function.
Usage Scenarios:
1, the resource optimization scene.
2, class initialization needs to digest a lot of resources, this resource includes data, hardware resources and so on.
3, performance and security requirements of the scene.
4. Creating an object through new requires very tedious data preparation or access, and you can use prototype mode.
5, the scene of a number of modified objects.
6. When an object needs to be accessible to other objects, and each caller may need to modify its value, consider using a prototype pattern to copy multiple objects for use by the caller.
7. In actual projects, prototype patterns rarely appear alone, typically with factory method patterns, creating an object by means of a clone and then being supplied to the caller by the factory method. Prototype model has been merged with Java for Seamless, we can use it handy.
Note: Unlike constructing a new object by instantiating a class, the prototype pattern is to generate a new object by copying an existing object. Shallow copy implements Cloneable, rewrite, and deep copy is read binary stream by implementing Serializable.
Specific implementation:
We will create an abstract class prototype and extend the entity classes of the prototype class. The next step is to define the class Cachemap, which stores the prototype objects in a map and returns their clones at the time of the request.
Client, the test class uses the client class to get the cloned object of the prototype. As follows:
The specific code is as follows:
----------------------------------------------------------------------------------------------------------- --------
1, the definition prototype abstract class prototype (also may be the interface) implements Cloneable interface
Public abstract class Prototype implements cloneable {
private String ID;
protected String type;
abstract void Show ();
Public String GetType () {return
type;
}
Public String GetId () {return
ID;
}
public void SetId (String id) {
this.id = ID;
}
public Object Clone () {
object clone = null;
try {
clone = Super.clone ();
} catch (Clonenotsupportedexception e) {
e.printstacktrace ();
}
return clone;
}
2. Define specific custom Creation instance classes:
public class Instance1 extends Prototype {public
Instance1 () {
type = ' Instance1 ';
}
@Override
Void Show () {
System.out.println ("Inside Rectangle::d Raw () method.");
}
public class Instance2 extends Prototype {public
Instance2 () {
type = ' Instance2 ';
}
@Override
Void Show () {
System.out.println ("Inside Square::d Raw () method.");
}
3. Create client-side test:
public class Client {//Set instance object cache private static map<string, prototype> Cachemap = new HashMap
<string, prototype> ();
Test public static void main (string[] args) {//Initialize the object to be cloned to create loadcache ();
Each object instance is created Instance1 ClonedInstance1 = (Instance1) getproto ("1");
System.out.println ("Prototype:" + clonedinstance1.gettype ());
Instance2 ClonedInstance2 = (Instance2) Getproto ("2");
System.out.println ("Prototype:" + clonedinstance2.gettype ()); /** * @Description: Cloning based on different instance IDs * @param ShapeID Instance number * @return/public static Prototype Getproto (String s
Hapeid) {Prototype Cachedproto = Cachemap.get (ShapeID);
Return (Prototype) Cachedproto.clone ();
//For example, we want to add three shapes public static void Loadcache () {Instance1 obj1 = new Instance1 ();
Obj1.setid ("1");
Cachemap.put (Obj1.getid (), obj1);
Instance2 obj2 = new Instance2 ();
Obj2.setid ("2");
Cachemap.put (Obj2.getid (), obj2); }
}
By using the above code, you can implement an existing prototype object to quickly generate the same instance as the prototype object.
After running the above code, you can generate the
Prototype:instance1
Prototype:instance2