First, the primer
The ancients: books are not read. I understand the ancients, let alone now IT book update, expensive, quality level is not uniform, I can't bear to see the book I bought with my hard-earned money in less than half a year, I don't want to support The translation of the modernization tools such as Jinshan Express, the word tyrants. So I went to the bookstore to have a book card, so I didn't have a worry about it - book is not good, I can change!
However, borrowing books is also unhappy, it is to see useful or more important, and cannot mark it next to the book. Generally, I will copy this page, so as my own thing, I can draw a circle, saved.
In software design, similar or similar issues are often encountered, GOF called this solution as an original mode. Perhaps the prototype mode will give you some new enlightenment.
Second, definition and structure
The prototype mode belongs to the object creation mode, and the GOF is defined as: Specify the type of the object with the prototype instance and create a new object by copying these prototypes.
The clone () method is provided in Java to implement the cloning of the object, so the prototype mode has become simpler. Note: For the use of Clone () methods, refer to "Thinking in Java" or "Effective Java", which is disclosed in many prototypes. This article does not discuss topics.
But in what circumstances, use prototype mode is the most appropriate? Can it give us what benefits? These issues are not to see a two-way teaching code can be solved. Here I will try my best to discuss these two questions.
I think the prototype mode should be generated to make up for the weakness of the factory method model. It has been speaking that the factory method model is relatively weak for adapting to product changes, and adding deleting a product will additionally causing the corresponding changes in factory class. So how is it to adapt to this change in prototype mode? Go down ...
Let's take a look at the structure of the prototype mode.
1) Customer role: Let a prototype clone yourself to get a new object.
2) Abstract prototype Role: Implement your own Clone method, the class that plays this role is usually an abstract class, and it has many specific subclasses.
3) Specific prototype Role: The object being copied, the specific subclass of the abstract prototype character.
Putting a picture to represent the image:
Follow the defined customer role not only responsible for the use of objects, but also responsible for the generation and clone of object prototypes. This is not very clear that the customer role division is not clear, so we will put the object prototype generation and clone function to put it in a prototype manager. The prototype manager maintains a list of existing prototypes. The customer will issue a request to the prototype manager when using, and the list of prototype managers can be modified. This allows customers to achieve system extensions without coding.
The class diagram is as follows:
Third, analysis
For abstract prototype characters and specific prototype characters, they are inherited or realized, nothing to say, remember to achieve a good clone method.
So how come the customer use these roles? Come see two lines of brief code
// First NEW a specific prototype role
Prototype P = New ConcretePrototype ();
......
// Use prototype P to get a new object P1
Prototype p1 = (prototype) p.clone ();
Maybe you have to say: I can't see what is the difference between using the Clone method to generate objects and NEW an object. Let us first look at what prototypes will bring us. The prototype mode uses Clone to dynamically extract the status of the current object run and cloned into the new object, and the new object can operate on this, without damaging the original object; and New can only get a just initialized object. In practical applications, this is often not enough.
Especially when your system needs good scalability, it is also necessary to use prototype mode in the design. For example, your system allows customers to customize their own categories, but this category initialization may need to pass more than have class parameters, which makes it will not know how to initialize it (because already Write dead) unless the class is modified. And this is obvious that it is obviously in the customer. If you use the prototype mode when you consider the above disadvantages, when the customer is customizes your own category, simultaneously register a prototype object to the prototype manager, and the class used only needs to get from the prototype manager according to the needs of the customer. An object is OK. This makes it easy to expand it.
It can be seen that the Clone method cannot be replaced by the constructor.
The prototype mode also reflects the polymorphism in OO - when others need my copy, I only clone myself, but I don't care about my specific type, all this is going to run.
As for the implementation of the prototype manager mentioned above, it is the maintenance of the prototype list. You can consider a few points: To save a list of prototype objects, we can use a HashMap to implement, correct the prototype object and its name; the prototype manager only needs one, so you can use a single case mode to implement Control; implementation, registration, and delete prototype objects are only for HashMap. code show as below:
Class PrototypeManager {Private Static PrototypeManager PM; Private Map Prototypes = NULL; Private PrototypeManager () {prototypes = new hashmap ();
// Use a single example mode to get the unique example of the prototype manager public static synchronized prototypeManager getManager () {if (pm == null) {pm = new prototypeManager ();} return pm;} public void register (String name, Object) prototype) {prototypes.put (name, prototype);} public void unregister (String name) {prototypes.remove (name);} public Prototype getPrototype (String name) {if (prototypes.containsKey (name)) {
// Return the replica of the corresponding prototype in the list to the customer return (prototype) ((prototype) prototypes.get ();} else {prototype object = null; try {object = (prototype) Class. Forname (name) .newinstance (); register (name, object);} catch (exception e) {system.err.println ("Class" Name "Nothing defined!"); . . It has been analyzed so much, and an example of using prototype mode is more classic: Performance appraisal software should be annual analysis of the various assessment data this year, and this set of data is stored in the database. Generally, we will package this set of data in a class, then one instance of such an instance is analyzed as a parameter incoming analysis algorithm, and analyze the results in the corresponding variable of the object. Suppose we decide to make another analysis of this group of data to be evaluated for analysis results. At this time, the CLONE to encapsulate this set of data is better than the connection database is better than the re-connect database.
I analyzed some benefits of using prototype mode, let's take a look at its defect. Prototype mode The main defect is that each prototype must contain a Clone method, which is more difficult to add Clone operations on an existing class; and when the internal consolidation includes some objects that do not support COPY or cyclic references, the implementation is more difficult. Moreover, using Clone methods to deeply understand deep clones, simple clones, otherwise it is likely to fall into the trap and embed the system.
Fourth, summary
Since the Clone method has certain drawbacks and risks in Java implementation, the Clone method is not recommended. So rarely see the use of prototype mode in Java applications. But prototype mode can also give us some enlightenment.