每日学一个设计模式6——原型模式

本文介绍了原型模式如何通过现有实例创建新对象,适用于对象种类繁多、难于生成实例的情况。主要角色包括Prototype接口、ConcretePrototype实现类和Client。通过Manager类的实例方法,展示了如何使用createClone方法实现浅拷贝。
摘要由CSDN通过智能技术生成

原型模式(通过复制生成实例)

用处

根据现有的实例生成新的实例

在以下情况下,我们不能根据类来实现实例,而根据现有的实例来生成新的实例

  1. 对象种类繁多,无法将他们整合到一个类中
  2. 难以根据类生成实例时
  3. 想解耦框架与生成的实例时

角色

  • Prototype
    该角色负责定义用于复制现有实例来生成新实例的方法。
  • ConcretePrototype
    该角色负责实现复制现有实例并生成新实例的方法
  • Client
    该角色负责使用复制实例的方法并生成新实例的方法

类图

在这里插入图片描述
可以看出原型模式还是非常简单的,只需要客户端通过原型接口调用具体原型的实现类中的createClone方法就即可。

举例

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角色
interface Product extends Cloneable{
    void use(String use);
    Product createClone();
}
class Manager{
    private HashMap showcase = new HashMap();
    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();
    }
}
//concretePrototype角色
class MessageBox implements Product{
    private char decochar;
    public MessageBox(char decochar){
        this.decochar = decochar;
    }

    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("");
    }
    public Product createClone(){
        Product p = null;
        try{
            p = (Product)clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return p;
    }
}

//如需扩展只需要多加一个类
//ConcretePrototype角色
class UnderlinePen implements Product{
    private char ulchar;
    public UnderlinePen(char ulchar){
        this.ulchar = ulchar;
    }

    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("");
    }
    public Product createClone(){
        Product p = null;
        try{
            p = (Product)clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return p;
    }
}

在这里插入图片描述
Manager类的create函数中返回克隆对象,使用后发现该对象与原对象完全相同。

总结

  • 符合开闭原则
  • 用于克隆一个与原对象完全相同的对象
  • clone方法是浅拷贝,如果需要克隆复杂对象,则需要使用深拷贝,用递归调用clone方法,或者序列化与反序列化实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑白程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值