Java Design Pattern---prototype (prototype) mode

Source: Internet
Author: User
Tags define interface return string version client
Design
Java Design Pattern---prototype (prototype) mode

Definition: Specifies the type of object to create with a prototype instance, and creates a new object by copying the prototypes.      The prototype mode allows one object to create another customizable object without having to know any details of how it was created, working by passing a prototype object to the object to be created, and creating the object by requesting the prototype object to copy themselves. Applicability: When the class to be instantiated is specified at run time, for example, by dynamic loading, or to avoid creating a factory class hierarchy that is parallel to the product class hierarchy, or when an instance of a class can have only one of several different state combinations. Creating a corresponding number of prototypes and cloning them may be more convenient than manually instantiating the class each time with the appropriate state. Code can explain everything: code part/************************* * Define interface *************************/package Meconsea;

/** * * <p>title:design in Java </p> * * <p>description: Create a batch of clone products </p> * with prototype <p> For information about cloning please refer to another blog</p> * * <p>copyright:copyright (c) 2005</p> * * <p>company:www .hhzskj.com</p> * * @author Meconsea * @version 1.0 * *

Import java.io.Serializable;

Public interface Iprototyperam extends Cloneable, serializable{

/** * for Bean * @return string/public string getName ();

/** * for Bean * @param name String * *

public void SetName (String name);

}

The realization of/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX interface xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/package Meconsea;

/** * * <p>title:design in Java </p> * * <p>description: Create a batch of clone products </p> * with prototype <p> For information about cloning please refer to another blog</p> * * <p>copyright:copyright (c) 2005</p> * * <p>company:www .hhzskj.com</p> * * @author Meconsea * @version 1.0 * *

public class Prototyperam implements iprototyperam{

private String name;

Public String GetName () {return this.name;  public void SetName (String name) {this.name = name; }

Public Prototyperam () {this.name = ' this is propotype! '; }

/** * Overwrite Clone method * @return Object */public Object Clone () {object o = null;    try{o = Super.clone ();    }catch (clonenotsupportedexception e) {System.out.println ("Prototyperam is not cloneable");  return o; }}

/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX prototype Manager part xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/package Meconsea;

/** * * <p>title: Prototype Manager </p> * * <p>description: Implements the function of automatic registration prototype </p> * <p> prototype manager shipping A row is an instance, so you can use the singleton mode to implement .</p> * <p> references to singleton other blog.</p> * <p>copyright:co Pyright (c) 2005</p> * * <p>company:www.hhzskj.com </p> * * @author Meconsea * @version 1.0 */import A.util.hashmap;

public class Prototypemanager {

/** * Reference to the difference between HashMap and hashtable other blogs * * Private HashMap HM = NULL;

private static Prototypemanager Prototypemanager = null;

Private Prototypemanager () {HM = new HashMap (); }

public static synchronized Prototypemanager Getprototypemanager () {if (Prototypemanager = = null) {Prototypemanager    = new Prototypemanager ();  return prototypemanager; }

/** * Registration * @param name String * @param prototype Object */public void Register (String name, Object prototype) {  Hm.put (Name,prototype); }

/** * Registration * @param name String/public void unregister (String name) {hm.remove (name); }

/** * Get prototype instance * @param name String * @return Object */public object Getprototype (String name) {Object o = null    ;    if (Hm.containskey (name)) {o = hm.get (name);      }else{try{/** * Automatically finds classes that do not exist 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; }}

/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Client realizes xxxxxxxxxxxxxxxxxxxxxxxxxxxxx/package Meconsea;

/** * * <p>title: </p> * * <p>description: Client uses prototype mode </p> *  * <p>copyright:copyright (c) 2005</p> * * <p>Company:www.hhzskj.com</p>  * * @author meconsea * @version 1.0 */public class Prototypeclient {  prototypemanager pm = nul l;  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 < i++) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N bsp;   /**         * Generates a batch of cloned ram and compares them with the prototype's different           */        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 ());      }   } }}

/******************************* * The code explains everything! ^...^ *******************************/


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.