图解设计模式

来源:互联网 发布:mysql教程视频下载 编辑:程序博客网 时间:2024/06/07 02:44

读书笔记 仅供参考

Prototype 模式

有时会出现“在不指定类名的前提下生成实例”的情况,就是根据实例生成实例,这就是原型模式。
以下情况需要使用原型模式

对象种类繁多,无法将它们整合到一个类中

需要处理的对象太多,如果将它们分别作为一个类,必须要编写许多个类。

难以根据类生产实例

生成实例的过程太过复杂,很难根据类来生成实例,所以需要通过复制来生成新的实例。

想解耦框架与生成的实例

想要生成实例的框架不依赖于具体的类,需要事先“注册”一个“原型”实例,然后复制。(例子中使用了这种方式)

UML 和角色

这里写图片描述

Prototype

负责定义用于复制现有实例来生成新实例的方法

ConcretePrototype

负责实现复制现有实例并生成新实例的方法

Client

负责使用复制实例的方法生成新的实例

例子

例子程序是编写使用多种方式显示文字,例如将字符串放入方框或加上下换线。
Product 接口属于 Prototype 角色,MessageBox 和 UnderlinePen 实现 Product,属于 ConcreteProduct 角色。Manager 类属于 Client 角色。

public interface Product extends Cloneable{    void use(String s);    //复制实例的方法    Product createClone();}
public class MessageBox implements Product{    private char decoChar;    public MessageBox(char decoChar) {        this.decoChar = decoChar;    }    @Override    public 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();    }    @Override    public Product createClone() {        Product p = null;        //使用 clone 方法必须继承 Cloneable 接口,否则会抛出异常。        try {            p = (Product) clone();        } catch (CloneNotSupportedException e) {            e.printStackTrace();        }        return p;    }}
public class UnderlinePen implements Product {    private char ulChar;    public UnderlinePen(char ulChar) {        this.ulChar = ulChar;    }    @Override    public 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();    }    @Override    public Product createClone() {        Product p = null;        //使用 clone 方法必须继承 Cloneable 接口,否则会抛出异常。        try {            p = (Product) clone();        } catch (CloneNotSupportedException e) {            e.printStackTrace();        }        return p;    }}
public class Manager {    private Map<String, Product> showccase = new HashMap<>();    public void register(String name, Product proto) {        showccase.put(name, proto);    }    public Product create(String protoName) {        Product p = (Product) showccase.get(protoName);        return p.createClone();    }}

测试代码

public class Main {    public static void main(String[] args) {        //准备        Manager manager = new Manager();        UnderlinePen underlinePen = new UnderlinePen('~');        MessageBox mbox = new MessageBox('*');        MessageBox sbox = new MessageBox('/');        manager.register("strong message", underlinePen);        manager.register("warning box", mbox);        manager.register("slash box", sbox);        //生成        Product p1 = manager.create("strong message");        Product p2 = manager.create("warning box");        Product p3 = manager.create("slash box");        p1.use("Hello, world.");        p2.use("Hello, world.");        p3.use("Hello, world.");    }}

结果
这里写图片描述

UML
这里写图片描述

clone

这里产生新的对象使用了 clone 方法,但是 clone 方法只是浅拷贝,虽然可以重写 clone 方法来实现深拷贝,但是使用 clone 方法是十分危险和麻烦的一件事。详情:http://blog.csdn.net/wujunyucg/article/details/78524195

原创粉丝点击