美文网首页
创建型模式:05-原型模式

创建型模式:05-原型模式

作者: 综合楼 | 来源:发表于2021-06-11 20:31 被阅读0次

浅克隆

image.png

深克隆

image.png

示例代码:

import java.io.*;

public class Dog implements Cloneable, Serializable {

    private static final long serialVersionUID = 1589397062304887365L;

    String color;
    Integer old;
    Dog son;
    Dog(String color, Integer old, Dog son){
        this.color =  color;
        this.old =  old;
        this.son =  son;
    }

    //浅克隆(ShallowClone)
    @Override
    public Dog clone() {
        try {
            return (Dog) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }

    //深克隆(DeepClone)
    public Dog deepClone() {

        //将对象写入流中
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        try(ObjectOutputStream oos= new ObjectOutputStream(bao)) {
            oos.writeObject(this);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //将对象从流中取出
        try(ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
            ObjectInputStream ois= new ObjectInputStream(bis)) {
            return (Dog)ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
public class TestShallowClone {
    public static void main(String[] args) {
        Dog son = new Dog("red", new Integer(1),null);
        Dog father = new Dog("black", new Integer(5), son);

        Dog father2 = father.clone();
        System.out.println(father==father2);
        System.out.println(father.color==father2.color);
        System.out.println(father.old==father2.old);
        System.out.println(father.old.equals(father2.old));
        System.out.println(father.son==father2.son);
    }
}
image.png
public class TestDeepClone {
    public static void main(String[] args) {
        Dog son = new Dog("red", new Integer(1),null);
        Dog father = new Dog("black", new Integer(5), son);

        Dog father2 = father.deepClone();
        System.out.println(father==father2);
        System.out.println(father.color==father2.color);
        System.out.println(father.old==father2.old);
        System.out.println(father.old.equals(father2.old));
        System.out.println(father.son==father2.son);

    }
}
image.png

相关文章

  • PHP常用设计模式

    # 创建型 单例模式 工厂模式 工厂抽象模式 原型模式 建造者模式 # 结构型 # 行为型 # 3.注册模式 # ...

  • 创建型设计模式——原型模式

    定义 原型模式是一个创建型的设计模式。用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。原型模式多用于...

  • 原型模式

    个人博客http://www.milovetingting.cn 原型模式 模式介绍 原型模式是一个创建型的模式。...

  • 三、原型模式

    原型模式用于创建重复对象的同时保持性能,该模式属于创建型设计模式,是创建对象的最佳实现方式。 为什么要使用原型模式...

  • 初始设计模式之原型模式

    原型模式是什么? 原型模式怎么用?浅拷贝深拷贝 原型模式再理解 一、原型模式是什么? ​ 原型模式是一种创建型设计...

  • 设计模式[4]-原型模式-Prototype Pattern

    1. 原型模式简介 原型模式(Prototype Pattern)是创建型设计模式,根据一个原型对象,通过Clon...

  • S3. 原型模式

    原型模式(Prototype) baidu[www.baidu.com] 原型模式是一种创建型设计模式,其功能为复...

  • Objective-C 原型模式 -- 简单介绍和使用

    用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。Prototype原型模式是一种创建型设计模式,P...

  • 设计模式最佳实践之原型模式

    What 原型模式:是一个创建型的模式。用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。 使用场景:...

  • 设计模式小结

    设计模式 7 大原则 创建型 单例模式 原型模式 创建者模式(生成器模式) 简单工厂模式 工厂方法模式 抽象工厂模...

网友评论

      本文标题:创建型模式:05-原型模式

      本文链接:https://www.haomeiwen.com/subject/yhcceltx.html