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?
-
For scenes that are not easy to produce object instances with classes;
-
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