设计模式——原型模式

2023-09-20  本文已影响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方法来实现,原型模式的实现分为浅拷贝深拷贝

上一篇下一篇

猜你喜欢

热点阅读