说说优秀设计模式--原型说说优秀设计模式--原型(Prototype)
2016-1-8 by Damon
说说原型(Prototype)
一句话总结
不再是利用类产生对象实例,而是复制对象实例产生新的对象实例。
比喻:买蛋糕时,指着一个蛋糕说要一样的,然后得到新做的一样的蛋糕。
有什么好处
- 用于不容易用类产生对象实例的场景;
- 框架和所产生的对象实例解耦;
简单的小例子
Product 接口继承Cloneable,声明可以执行克隆(clone)
public interface Product extends Cloneable { public abstract void use(String s); public abstract Product createClone(); }
Manager类利用Product接口进行复制
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 接口实现类 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; } }
Product 接口实现类 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; 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() { // TODO Auto-generated method stub Product p = null; try { p = (Product) clone(); } catch (CloneNotSupportedException e){ e.printStackTrace(); } return p; } }
Main 测试类
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."); }
优秀案例分析
时间: 2024-11-04 15:37:57