设计模式|原型模式

作用和场景

对象创建成本过大,同一个类的不同对象之间差别不大,利用已有的对象进行复制,减少创建成本。

原型模式属于创建型的设计模式。

创建成本过大?

对象数据涉及大量计算,排序、哈希计算、io等等。

深拷贝与浅拷贝

原型模式的实现关键

浅拷贝:

java语言中的clone()方法就是浅拷贝,浅拷贝会创建一个新对象,新对象和原对象本身没有任何关系,新对象和原对象不等,但是新对象的属性和老对象相同,
如果属性是基本类型(int,double,long,boolean等),拷贝的就是基本类型的值;
如果属性是引用类型,拷贝的就是内存地址(即复制引用但不复制引用的对象) ,因此如果其中一个对象改变了这个地址,就会影响到另一个对象。

他只会拷贝基本数据类型,复制引用对象的内存地址,不会复制对象本身。对于需要方B来说,浅拷贝得到的对象和来源方A是共享的,这就会出现一个问题,相互间的修改会导致来源方数据不统一,比如一次耗时的更新,期望依托临时的散列表B来存储更新的数据,全部更新完成后再替代工作中的A,浅拷贝就会导致A 中数据新旧参半的情况发生。

实现

,Obeject中提供了clone()方法,但不能直接调用clone()方法,需要实现Cloneable接口,否则会抛出CloneNotSupportedException异常。我们可以在类中重写clone()方法,实现浅拷贝

 class  Car implements Cloneable{
        public Object copy() {
            Object car=null;
            try {
                car = super.clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return car;
        }
    }

或者通过构造方法的方式:

 class  Car implements Cloneable{
        private int flag;
        private Wheel c;
        void Car(Car car) {
          this.flag = car.flag;
          this.c = car.c;
        }
    }

对于浅拷贝对象的引用对象成员来说,双方的修改是有影响的,因为共享的是同一个地址对象。

深拷贝

深拷贝会复制对象本身,申请新的内存地址。

通过序列化实现
public class DeepCopy {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        Category category = new Category(1);
        Watch p1 = new Watch("high", category);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        //将p1序列化
        oos.writeObject(p1);
        oos.flush();
        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
        Watch p2 = (Watch) ois.readObject();
        System.out.println("修改前:");
        System.out.println(p1.toString());
        System.out.println(p2.toString());
        System.out.println("修改后:");
        p1.setLevel("low");
        category.setCategory(2);
        System.out.println(p1.toString());
        System.out.println(p2.toString());
    }
}

class Watch implements Serializable{
    private String level;
    private Category category;

    public Watch(Watch watch) {
        this.level = watch.level;
        this.category = watch.category;
    }

    public Watch(String level, Category category) {
        this.level = level;
        this.category = category;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    @Override
    public String toString() {
        return "Watch{" +
                "level='" + level + '\'' +
                ", category=" + category +
                '}';
    }
}

class Category implements Serializable{
    private int category;

    public Category(int category) {
        this.category = category;
    }

    public int getCategory() {
        return category;
    }

    public void setCategory(int category) {
        this.category = category;
    }

    @Override
    public String toString() {
        return "Category{" +
                "category=" + category +
                '}';
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值