美文网首页
设计模式——原型模式

设计模式——原型模式

作者: 笑对浮华 | 来源:发表于2023-09-20 15:10 被阅读0次

一、介绍

当系统中需要频繁的创建同一个对象,对象创建时又伴随着大量初始化操作、比较繁琐时,这个时候考虑用原型模式去生成多个对象。Java中所有的类最终都继承自父类Object,Obejct中定义了clone这个抽象方法,子类调用clone方法获取的对象属于浅拷贝;定义类的时候实现Cloneable接口,重写clone方法重写具体的clone细节可实现深拷贝
原型模式是对对象的拷贝,分为浅拷贝深拷贝两种方式。

二、代码实例

1、浅拷贝

定义Sheep类,实现Cloneable接口,重写clone方法:

public class Sheep implements Cloneable{

    private String name;

    private int age;

    private Sheep friend;

    public Sheep(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Sheep getFriend() {
        return friend;
    }

    public void setFriend(Sheep friend) {
        this.friend = friend;
    }

    @Override
    protected Object clone() {
        Sheep sheep = null;
        try {
            sheep = (Sheep)super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return sheep;
    }
}

调用:

public class Main {

    public static void main(String[] args){
        Sheep sheep1 = new Sheep("喜羊羊",1);
        sheep1.setFriend(new Sheep("懒羊羊",1));
        Sheep sheep2 = (Sheep) sheep1.clone();


        System.out.println("sheep1 hashcode:"+sheep1.hashCode());
        System.out.println("sheep2 hashcode:"+sheep2.hashCode());

        System.out.println("sheep1 friend hashcode:"+sheep1.getFriend().hashCode());
        System.out.println("sheep2 friend hashcode:"+sheep2.getFriend().hashCode());

        sheep2.getFriend().setName("美羊羊");
        System.out.println("sheep1 friend name:"+sheep1.getFriend().getName());
        System.out.println("sheep2 friend name:"+sheep2.getFriend().getName());
    }
}
浅拷贝

2、深拷贝

定义Person类:

public class Person implements Cloneable{

    private String name;

    private int age;

    private Boy boy;

    public Person(String name, int age, Boy boy) {
        this.name = name;
        this.age = age;
        this.boy = boy;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Boy getBoy() {
        return boy;
    }

    public void setBoy(Boy boy) {
        this.boy = boy;
    }

    @Override
    protected Object clone()  {
        Object person;
        try{
            person = super.clone();
            Person clonePerson = (Person) person;
            clonePerson.setBoy((Boy)clonePerson.getBoy().clone());
            return clonePerson;
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return null;
    }
}

调用:

public class Main {

    public static void main(String[] args){
        Person person1 = new Person("张三",22,new Boy("张三boy",22));
        Person person2 = (Person) person1.clone();

        System.out.println("person1 hashcode:"+person1.hashCode());
        System.out.println("person2 hashcode:"+person2.hashCode());

        System.out.println("person1 boy hashcode:"+person1.getBoy().hashCode());
        System.out.println("person2 boy hashcode:"+person2.getBoy().hashCode());

        person2.getBoy().setName("李四boy");

        System.out.println("person1 boy name:"+person1.getBoy().getName());
        System.out.println("person2 boy name:"+person2.getBoy().getName());
    }
}
深拷贝

三、总结

原型模式作为Java23中设计模式中创建型模式的一种,主要是对于对象创建麻烦,牵扯到其他变量的初始化,同时又需要大量、频繁的创建场景使用,通过调用父类Objec定义的clone方法或者实现Cloneable接口,重写clone方法来实现,原型模式的实现分为浅拷贝深拷贝

  • 浅拷贝:直接调用super.clone()即可。clone对象的时候,类中包含的基本类型变量和String类型的变量只是进行值传递,clone的对象对于这些变量新开辟一片内存地址去存储值;引用类型的变量在clone后还是指向之前的内存地址,原对象和clone的对象共享同一个内存地址,即改变其中某个对象的引用类型变量值,其他对象的对应值也会变化。(浅拷贝代码贴图中,改变了sheep2对象中的引用对象friend的name值,sheep1的friend name值也改变了。)
  • 深拷贝:引用类型变量也需要实现Cloneable接口,重写clone方法,对引用类型调用clone方法,重新赋值给对象返回。深拷贝中不管什么类型的变量都会重新开辟内存,即clone的对象和原对象的变量指向不同的地址,clone的对象变量值变化不会引起原对象变量值的变化。(深拷贝代码贴图中clone对象person2改变引用变量boy的name后,原对象person1的boy name还是没变。)

相关文章

网友评论

      本文标题:设计模式——原型模式

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