Java design pattern --- prototype mode
Definition: Use the prototype instance to create the type of object, and create a new object by copying these prototypes. Prototype mode Allows an object to create another customable object, try to know any details of anything, the working principle is: A prototype object is passed to the object to be created. This object to be created is created by requesting prototype objects to create itself. Applicability: When the class to be instantiated is specified at runtime, for example, by dynamic loading; or to avoid creating a factory class hierarchy parallel to the product class level; or when a class is only a few different One of the state combination. Establish a corresponding number of prototypes and clone them may be more convenient than manually modified each time you use a suitable state. Code can explain everything: code section / ************************ * Define Interface ************* *********** / package meconsea;
/ ** * *
Title: design in java p> * *
Description: Create a batch of Clone products p> *
for CLONE, please refer to another blog < / p> * *
Copyright: Copyright (c) 2005 p> * *
Company: www.hzskj.com p> * * @Author meconsea * @version 1.0 * /
Import java.io.serializable;
Public interface iprototyperam extends cloneable, serializable {
/ ** * Used in bean * @return string * / public string getname ();
/ ** * Used in bean * @Param name string * /
Public void setname (String name);
}
/ ×××××××××××××××××××××××××××××××× ××××××××××××××××××××××××× / package meconsea;
/ ** * *
Title: design in java p> * *
Description: Create a batch of Clone products p> *
for CLONE, please refer to another blog < / p> * *
Copyright: Copyright (c) 2005 p> * *
Company: www.hzskj.com p> * * @Author meconsea * @version 1.0 * /
Public class prototyperam imports iprototyperam {
PRIVATE STRING NAME;
Public string getname () {return this.name;} public void setname {this.name = name;}
Public prototyperam () {this.name = "this is propotype!";
/ ** * overwriting Clone method * @Return Object * / public object clone () {Object o = null; try {o = super.clone ();} catch (clonenotsupportedException e) {system.out.println ("prototyperam IS not cloneable ");} returno}} / ×××××××××××××××××××××××××××××××××××× ×× × prototype manager parts ×××××××××××××××××××××××××××××××××××× / package meconsea;
/ ** * *
Title: Prototype Manager p> * *
Description: Implementing the function of automatic registration prototype p> *
Prototype Manager runs as long as one instance is run, Implement with Singleton mode. P> *
About Singleton's reference other blog. P> *
Copyright: Copyright (c) 2005 p> * *
Company: www.hhzskj .COM p> * * @Author meconsea * @version 1.0 * / import java.util.hashmap;
Public class prototypeManager {
/ ** * About HashMap and Hashtable Reference Reference Other Blog * / Private HashMap HM = NULL;
Private statinoPemanager prototypemanager = null;
Private prototypemanager () {hm = new hashmap ();
Public static synchronized protoypemanager getprototypeManager () {if (prototypemanager == null) {prototypeManager = new prototypemanager ();} rettototypemanager;}
/ ** * Register * @Param Name string * @Param prototype object * / public void register (string name, object prototype) {hm.put (name, prototype);}
/ ** * Releases * @Param Name string * / public void unregister (String name) {hm.remove (name);}
/ ** * Get prototype example * @Param name string * @Return Object * / public object getPrototype (String name) {Object O = null; if (hm.containskey (name)) {o = hm.get (name); } else {try {/ ** * Automatically looks for the classes in the prototype manager, and dynamically generates it * / o = class.Forname (name) .newinstance (); this.register (name, o);} catch (Exception E) {System.out.println ("Class" Name "Don't Define" E.GetMessage ()); E.PrintStackTrace ();}} Return O;}}} Return O;}} / ×××××× ××××××××××××××××××××××× × client realization ××××××××××××××××××××××× ×××××××× / package meconsea;
/ ** * *
Title: p> * *
Description: Client Using prototype mode p> * *
Copyright: CopyRight (c) 2005 p> * *
Company: www.hhzskj.com p> * * @author meconsea * @version 1.0 * / public class PrototypeClient {PrototypeManager pm = null; public PrototypeClient () {pm = PrototypeManager.getPrototypeManager ();} public static void main (String args []) {PrototypeClient pc = new PrototypeClient (); String className = null; className = "meconsea.PrototypeRam"; PrototypeRam pr = null; pr = (PrototypeRam) (pc.pm.getPrototype (className)); if (Pr! = null) {prototyperam [] pram; system.out.println ("for loop before prototyperam name ==" pr.getName ()); PRAM = new prototyperam [10]; for (int i = 0; I <10; i ) {/ ** * Generate a batch of cloned RAM and compare the difference between them and prototypes * / PRAM [i] = (prototyperam) pr.clone (); system.out.println ("Loop for Prototyperam name == " PRAM [i] .GetName ()); PRAM [i] .setname (PRAM [i] .Getname () i); system.out.println (" CLONE CHANGES AFTER " PRAM [i] .Getname () " OLD " pr.getname ());}}}} / ********************* *********** * code shows everything! ^ ... ^ ***************************** /