Prototype mode and factory model definition, this article does not want to talk too much, this article mainly wants to talk about some misunderstandings of prototype mode - put prototype mode is factory mode; why does this misleading? In fact, it is not our fault. The key is that the book is the book and other information like to compare prototypes and factory methods, causing us to misunderstand the essential significance of prototype introduction. According to my understanding, the root cause of prototyping is that it can take advantage of a prototype object (here, I refer to instance, not class), quickly generate a batch of prototype objects. For example, you have an instance A of Class A (a a = new a ()), now you want to generate one as an instance B, then, according to the definition of the prototype, you should be able to do this B = a.clone (). In this way, you can get the same example B (that is, the value of the data member of A and Part B is completely exactly the same. The above is a brief description of the prototype, then what is the benefit of introducing a prototype? According to my understanding, if you want to generate a large number of very similar instances, save each time you do repeated assignment work. Again, if you have a class A, it has ten member variables, now you intend to generate an instance of 100 A, and most of these instances are mostly the same (for example, seven identical), only a small part Different (for example, three), then if there is no prototype, you have to have a new A of a A A, and then assign the value, so that you have to repeat the value of the same seven variables, obviously, this is very trouble. Now you have prototype, then the problem is simple, you only need to generate an instance of A, then generate other instances through Clone, and then modify the different places in other instances. Maybe I said, everyone does not believe, let us take a look at the original model of life in Java. People who have learned Java know that in Java, there is a clone () function, the functionality of this function, is the same example as the object that is currently calling it. So why to introduce this function in Java? In the << Think in Java >>, if you explain: If you want to pass an object's reference as a parameter, you don't want the function to change the value of the object, then what should you do? Since the object does not have a const modifier like C in Java, in order to implement this feature, the Clone function is introduced in Java so that when you use the reference to the object as a parameter, this function can call the object's Clone. Methods A copy of the object is generated, thereby achieving the purpose of not modifying the original object. The reason why there are so many spaces to tell the prototype in the style. The purpose is to hope that you should not mix the functionality of the prototype like me, so that when you really want to use prototypes, you don't know how you can solve the problem. use it. Ok, the essential significance of the prototype - at least I think it is. Then why do many information like to compare prototypes with factories? I don't know if it is a coincidence. Although the original intention of prototype is like I said above, he achieved it, but it can fully reach the outskirts of the factory model (behind, I will use code to implement Mac, Win products that can be implemented in factory model Series generation issues). Moreover, it is more convenient and flexible than the plant mode.
For factory models and prototype patterns, I think maybe because the factory model and prototype mode are created models, so that their basic functions can generate objects, thus making prototype mode functions Instead of plant mode. The same point of these two modes, the article "Non-fish" written in the 11th issue of the programmer 2001, the author understands very clever, ie, if you pay the factory mode UML map, you The UML map for the prototype prototype. Interested in comparing friends from these two modes, you can refer to this article. Next, let's take a look at why the prototype mode can realize the functionality of the model mode (this article is limited to Java language). In Java, for the realization of prototypes, do not need to be done at all, and have defined a Clone function in the Object class, and this function makes us dynamically generate the current copy of the object. Okey this, let's take a look, if we want to realize the function of factory mode, how should we use prototype mode? The function of the production of products implemented in the factory model is to use the inheritance characteristics. That is to say, you generated products must be derived from the same abstract product class. So, in the factory mode, if you want to generate a class of products, you will introduce an abstract product class and then derive specific products. Similarly, in prototype mode, you can simply define a "abstract product-specific product" level, and then use the specific product itself of Clone function to generate specific product itself. Thereby achieving the purpose of realizing the functionality of the factory model. It may be said that everyone is a bit confused. In fact, in prototype mode, each specific product plays the role of specific factories in the factory model (why this is actually very simple, because each specific product has functions that generate its own copy? From this meaning Said, is this not the role of the factory?). In addition, to utilize the functionality of the factory mode in Java, it is simpler because Object has a Clone function that has implemented us, and for the clone method, the default in Java is: if A is a parent class and a enabled Clone Functions, b is subclasses, but B does not need to implement Clone functions, which simply calls the parent class's Clone function, Java will dynamically generate the correct B object when runtime. Understanding this point is that all CLONE operations that are implemented are Clone methods that call Object. That is to say, the parent class A I have mentioned above does not have to implement the Clone method, but only the Clone method of calling the parent class (Object). Ok, it's, the reader may have questions, since all CLOEN operations are implemented by Object, and all custom classes in Java are derived from Object, that's like this, should be all classes Automatically have CLONE's own capabilities? Indeed, if the Object does not declares its Clone function as protect, the situation is true.
But Java is for security reasons, so it does not disclose the Clone method, but declares that the type of protection is not possible to directly call the Clone method of the Object class directly, but must do the following: 1. must Implement a Cloneable interface; 2. You must declare a Clone method to call the Object's Clone function; Java will perform a dynamically check when calling the parent class's Clone function. If you find that the called class does not meet any point above, It will throw an exception. Understand the above reason, then if we want a class to have Clone's ability, then we can do this: 1. Press the above says, implement the clone action; 2. Statement an abstract parent class, implement it Clone operation and declare it as an open method, and then derives a child class, so that all subclasses can copy themselves correctly as long as they call the Clone method of the parent class. Usually, we are all kinds of ways, but in how we discuss the function of the function of plant models now, we should use the second way. Finally, let's take a look at how to use the prototype mode to function in the factory model. Question: The existing two types of products 1-RAM, 2 - CPU, now generate specific products Macram, Maccpu and Winram, WinCPU. The code is as follows: / ** * A: Abstract * c: concrete * / / ** definition Abstract product RAM class APROTYPERAM * At the same time, he is also an abstract factory * / Abstract class aprotyperam imports cloneable {public object clone () {Object o = null; try {o = super.clone (); // call the parent class, ie Object Clone ()} catch (clonenotsupportedexception e) {system.err.println ("AprotoTyperam is not cloneable!");} Return O;}}
/ ** Define abstract product RAM class AprototypeProductCPU * At the same time, he is also an abstract factory * /
abstract class APrototypeCpu implements Cloneable {public Object clone () {Object o = null; try {o = super.clone (); // call the parent, i.e., the Object clone ()} catch (CloneNotSupportedException e) {System.err. Println ("Aprototypecpu Is Not Cloneable!");} Return O;
/ ** Define the specific product Macram class cprototypemacram * and he is also a specific factory * /
Class CprototypeMacram Extends Aprototyperam {Public String Tostring () {Return "macram";}}
/ ** Define the specific product Winram class CPrototypewinram * and he is also a specific factory * /
Class Cprototypewinram EXTENDS APROTYPERAM {PUBLIC STRING TOSTRING () {Return "Winram";}} / ** Define specific product Maccpu class cprototypeMaccpu * At the same time, he is also specific factory * /
Class CPrototypeMaccpu Extends Aprototypecpu {Public String Tostring () {Return "Maccpu";}}
/ ** Define the specific product WinCPU class cprototypewincpu * and he is also a specific factory * /
Class Cprototypewincpu Extends Aprototypecpu {Public String Tostring () {Return "WinCPU";}}
/ ** Client, use cprototyperam and cprototypecpu to generate the following product * Macram, Maccpu, Winram, WinCPU * / public class prototype {public static void main (string [] args) {/ ** * Before generating products, Mr. product, so as to use them later to produce the same product batch * the role equivalent to the plant product * / CPrototypeMacRam prototypeMacRam = new CPrototypeMacRam (); CPrototypeWinRam prototypeWinRam = new CPrototypeWinRam (); CPrototypeMacCpu prototypeMacCpu = new CPrototypeMacCpu (); CPrototypeWinCpu prototypeWinCpu = new CPrototypeWinCpu (); CPrototypeMacRam MacRam = (CPrototypeMacRam) prototypeMacRam.clone (); CPrototypeWinRam WinRam = (CPrototypeWinRam) prototypeWinRam.clone (); CPrototypeMacCpu MacCpu = (CPrototypeMacCpu) prototypeMacCpu.clone (); CPrototypeWinCpu WinCpu = (CPrototypeWinCpu) prototypeWinCpu.clone () System.out.Println ("Print Prototype Products and Its Cloning Products and Difased!"); System.out.Println ("PrototypeMacram:" PrototypeMacram "Clones:" macram); System.Out.println (" Prototypewinram: " Prototypewinram " Clones: " Winram); System.out.Println (" PrototypeMaccpu "CLONED:" MACCPU); systemAcpu; system .out.println ("prototypeincpu: prototypewincpu " cloned: " wincpu);}}
With the above code, we can clearly see that the factory model is simpler, if you match the prototype manager, then prototype mode will become more flexible, limited to space, this article did not talk about prototype management If you are interested in friends, please refer to the references listed later. But at the same time, we also found that when using prototype mode, there is a deficiencies, that is, in the client code, we must display the type conversion, which may result in errors. In order to correct this, I think we can use the real plant mode to package the prototype mode. This feature of the factory model, I am afraid, Prototype prototype mode is not powerful. In summary, the plant mode and prototype mode although the introduction is different, in realization, the prototype mode can realize the same functionality in the factory model. But readers should not be mixed by this, because, in turn, the plant mode cannot replace it when using this function of the original pattern as a copy of the original. references:
1. Design mode - Foundation for Object-oriented Software GOF 2.Design Pattern in Java James W. Cooper 3.think in Java Bruce Eckel 4. Programmer 2001 No. 11 Magazine - Understanding Design Mode (2) Prototype Non-fish