Prototype-----设计模式
来源:互联网 发布:mac 打开应用程序目录 编辑:程序博客网 时间:2024/05/29 02:05
1.动机 举个简单的例子一份文件是一个对象,我们要多分文件那么就要产生多个对象。
有的时候,我们需要一个实例时,并且,当创建一个实例的过程比较复杂或者说是昂贵时,比如,创建实例的构造函数非常的复杂,在执行这个构造函数时会消耗较长的时间,同时呢,这个构造函数中的一些信息又没有什么变化(也就是说创建第一个实例时初始化信息是这样的,创建第二个实例时初始化信息还是还是这样的),那么直接使用 new 来创建这样一个实例就显得太昂贵了,最好的解决方法,并不是使用 new 来实例化一个对象,而是使用克隆,也就是复制,克隆呢,就是通过复制现在已经有了的实例来创建新的实例。
so 原型模式就是来clone对象的。
2.代码:
package com.framework;public interface Product extends Cloneable {public abstract void use(String s);public abstract Product createClone();}
package com.framework;public class MessageBox implements Product {private char decochar;public MessageBox(char decochar) {this.decochar = decochar;}@Overridepublic void use(String s) {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("");}@Overridepublic Product createClone() {Product p = null;try {p = (Product)clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}return p;}}
package com.framework;public class UnderlinePen implements Product {private char ulchar;public UnderlinePen(char ulchar) {this.ulchar = ulchar;}@Overridepublic void use(String s) {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("");}@Overridepublic Product createClone() {Product p = null;try {p = (Product)super.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}return p;}}
package com.framework;import java.util.Hashtable;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 protoname) {Product p = (Product)showcase.get(protoname);return p.createClone();}}
package com.framework;public class Main {public static void main(String[] args) {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");}}
- 设计模式:Prototype模式
- 设计模式-Prototype模式
- 设计模式-Prototype模式
- 设计模式 - Prototype 模式
- 设计模式-Prototype模式
- 设计模式--Prototype模式
- 实现Prototype设计模式
- 实现Prototype设计模式
- 设计模式之Prototype
- 设计模式之Prototype
- Prototype设计模式
- 设计模式之Prototype
- 设计模式之Prototype
- 实现Prototype设计模式
- 设计模式--Prototype
- Prototype设计模式
- 设计模式----Prototype
- 设计模式之Prototype
- C#程序单一运行
- 在navigationBar上面添加多个任意控件
- GSM PDU串编解码
- WebBrowser控件判断页面加载完毕
- 李开复写给中国大学生的七封信【1/7】
- Prototype-----设计模式
- 李开复写给中国大学生的七封信【2/7】
- SAP预留(转)
- 李开复写给中国大学生的七封信【3/7】
- Linux查看文件句柄占用多的进程
- 李开复写给中国大学生的七封信【4/7】
- 李开复写给中国大学生的七封信【5/7】
- 简单看AfxOleInit()的内部工作~~
- Castle.ActiveRecord 使用 存储过程 返回实体类