The following code achieves the most essential function of the prototype, that is, the clone function. For complete, I specially added the prototype manager. This code is mostly an article from the 2001 non-fish of programmer magazine. I have corrected some of these errors to import below the code. In addition, many people like to use prototype to realize the functionality of factory model or in combination with plant mode. In order to highlight the nature of the original shape, the following code did not join these stuff. Interested friends, you can find relevant books yourself. Ok, let me say, the code shows everything, please see the code: / ** * design Pattern in java * name: prototype * Purpose: Use prototype to create a batch of products * prototype: prototyperam * Copy: clonedram * P: Prototype * C: Clone * Author: BlackPhoenix * Modify Date: 2002-08-18 * / Import java.util. *; / ** * Define prototype Product class prototyperam * This class is used back to product a large number of products Templates * /
class PrototypeRam implements Cloneable {String name; public PrototypeRam () {name = "Hi, I am PrototypeRam!";} public Object clone () {Object o = null; try {o = super.clone ();} catch (CloneNotSupportedException e) {system.err.println ("prototyperam is not cloneable!");} returno}}
/ ** * Prototype Manager, is used to facilitate management * You can implement an automatic registration of prototype. * Since the prototype manager is running as long as one instance is run, it implements it * /
class PrototypeManager {private static PrototypeManager pm; Hashtable prototypes = null; private PrototypeManager () {prototypes = new Hashtable ();} public static 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 Object getPrototype (String name) {if ( Prototypes.containskey (name)) {Return prototypes.get (name);} else {Object o = null; try {/ ** * Automatically look for classes in the prototype manager, and dynamically generate it * / o = Class .forname (name) .newinstance (); Register (name, o);} catch (exception e) {system.err.println ("Class" Name "No definition!");} return o;}}} / * ** client code, obtained using a prototype * PrototypeManager PrototypeRam by generating a number of Ram * / public class prototype {PrototypeManager pm = null; public prototype () {pm = PrototypeManager.getManager ();} public static void main (String [ ] args) {string classname = null; classname = a RGS [0]; prototype test = new prototype (); prototyperam pram = (test.pm.getPrototype); if (PRAM! = null) {prototyperam [] cram; system.out.println (" Prototyperam: " PRAM.NAME); CRAM = New Prototyperam [10]; for (int i = 0; i <10; i ) {/ ** * Generate a batch of cloned RAM and compare them different from the prototype * / CRAM [I] = (prototyperam) PRAM.Clone (); System.out.Println ("Cloney RAM" i ":" PRAM.NAME);}} else {system.err.println ("Sorry, CAN 't find the clas! ");}}}
Attachment: Code experience is running, enter Java Prototype Prototyperam to get the correct result