Talk about good design patterns-prototypes

Source: Internet
Author: User

Talk about good design patterns-prototypes Talk about good design patterns-prototypes (Prototype)

2016-1-8 by Damon

Talk about the prototype (Prototype) a sentence summary

Instead of using a class to produce an object instance, copying an object instance produces a new object instance. Metaphor: When buying a cake, point to a cake to say the same, and then get the same cake as the new one.

What good is it?
    1. For scenes that are not easy to produce object instances with classes;

    2. The framework and the resulting object instances are decoupled;

A simple small example

The Product interface inherits Cloneable, declaring that cloning can be performed (clone)

Public interface Product extends cloneable {public abstract void use (String s); Public abstract Product Createclone ();}

The manager class uses the product interface for replication

public class Manager {private hashtable<string, product> showcase = new hashtable<string, product> ();    public void Register (String name, Product Proto) {showcase.put (name, Proto);        Public Product Create (String name) {Product p = showcase.get (name);    return P.createclone (); }}

Product interface Implementation Class MessageBox

Public class messagebox implements product {    private char  decochar;    public messagebox (Char decochar)  {         this.decochar = decochar;    }     /*  (Non-javadoc)      * <p>use</p>      * <p></p>     *  @param  s      *  @see  pattern.creational.prototype.framework.product#use (java.lang.String)       */     @Override     public void use (string s )  {        // todo auto-generated method stub         int length = s.getbytes () .length;          for (int i = 0; i < length + 4; i++) {             system.out.print (Decochar);         }        system.out.println ("");         system.out.println (decochar +  " "  + s  +  " "  + decochar);         for (int i =  0; i < length + 4; i++) {             system.out.print (Decochar);         }         system.out.println ("");    }     /*  (Non-javadoc)      * <p>createClone</p>      * <p></p>     *  @return      *  @see   Pattern.creational.prototype.framework.product#createclone ()      */      @Override     public product createclone ()  {         // TODO Auto-generated method stub         product p = null;        try {             p =  (Product)  clone ();         } catch  (clonenotsupportedexception e) {             e.printstacktrace ();         }        return p;     }   &nbsP;} 

Product interface Implementation class Underlinepen

public class underlinepen implements product {    private  Char ulchar;    public underlinepen (Char ulchar) {         this.ulchar = ulchar;    }    /*   (Non-javadoc)      * <p>use</p>     *  <p></p>     *  @param  s     *   @see  pattern.creational.prototype.framework.product#use (java.lang.String)       */     @Override     public void use (string s)  {        // todo auto-generated method stub         int length = s.getbytes () .length;        &nbSp System.out.println ("\" " + s + " \ ");         System.out.print (" ");         for (int i = 0;  i < length; i++) {             System.out.print (Ulchar);        }         system.out.println ("");    }    /*  (Non-Javadoc)      * <p>createclone</p>     * <p ></p>     *  @return      *  @see   Pattern.creational.prototype.framework.product#createclone ()      */      @Override     public product createclone ()  {         //&nbSp Todo auto-generated method stub        product p  = null;        try {             p =  (Product)  clone ();         } catch  (clonenotsupportedexception e) {             e.printstacktrace ();         }         return p;    }}

Main test class

Public static void main (String[] args)  {    // TODO  Auto-generated method stub    manager manager = new manager ( );     underlinepen upen = new underlinepen (' ~ ');     messagebox mbox = new messagebox (' * ');     messagebox sbox  = new messagebox ('/');     manager.register ("Strong message",  Upen);     manager.register ("Warning box",  mbox);     Manager.register ("Slash box",  sbox);    product p1 =  Manager.create ("Strong message");     p1.use ("Hello, world.");     product p2 = manager.create ("Warning box");     p2.use ("Hello, world.");     product p3 = manager.create ("Slash box");     p3.use ("Hello, world."); 
Excellent case analysis

Talk about good design patterns-prototypes

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.