I, Original (prototype) mode structure
Specify the type of the object to be created by providing a prototype object, and then create more objects by copying the prototype object. The original model has two forms: the first is the simple form, and the second is the registration form.
The simple structure is as follows:
Registration Form:
Two forms of comparison:
If the number of created prototype objects is small and relatively fixed, you can adopt the original model mode in simple form.
If the number of created prototype objects is not fixed, you can use the register prototype. In this case, the client does not save the reference to the prototype object. This task is handed over to the Administrator object.
Shortest replication and deep Replication
The shortest replication only takes care of the objects, instead of copying the objects it references.
In deep replication, variables that reference other objects will point to the new object that has been copied, that is, deep replication copies all the objects referenced by the object to be copied.
II, Application of the original mode in Java
JavaObject replication in
The component model of the Java language directly supports the original model mode. Although the object class provides a clone () method that can copy a JavaBean object, this JavaBean must implement an interface named cloneable.
Generally, the clone () method meets the following requirements:
1,
For any object X, there are: X. Clone ()! = X. In other words, the cloned object is not the same as the original object.
2,
For any object, X. Clone (). getclass = x. getclass (). In other words, the cloned object is of the same type as the original object.
3,
If the equals () method of object X is well defined, X. Clone (). Equals (x) should be true.
III, Advantages of original model Mode
1,
The original model mode allows you to dynamically add or remove product classes. Because the method for creating a product instance is internal to the product class, adding a new product has no impact on the entire structure.
2,
The original model mode provides a simplified creation structure. The factory method mode often requires a structure with the same grade as the product class, which is not required in the original model mode.
3,
Ability to dynamically load new functions for an application.
4,
Product classes do not need to have any pre-determined level structure, because the original model mode applies to any level.