【设计模式】创建型模式之原型模式

【设计模式】创建型模式之原型模式

1.概述

原型模式(Prototype Pattern):是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式之一。这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式

例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。


2. 构成

原型模式包含如下三个角色:

  • 抽象原型类:规定了具体原型对象必须实现的的 clone() 方法。
  • 具体原型类:实现抽象原型类的 clone() 方法,它是可被复制的对象。
  • 访问类:使用具体原型类中的 clone() 方法来复制新的对象。

3. 实现

原型模式的克隆分为浅克隆和深克隆:

  • 浅克隆:创建一个新对象,对于基本类型,新对象的属性和原来对象的完全相同;对于引用类型,新对象的属性仍指向原对象属性的内存地址。
  • 深克隆:创建一个新对象,新对象的引用类型也会被克隆,不再指向原有对象地址。

java中的Object类中提供了 clone() 方法来实现浅克隆。 Cloneable 是jdk提供的抽象圆形类,而实现了 Cloneable 接口的子实现类就是具体的原型类。

Realizetype(具体的原型类):

public class Realizetype implements Cloneable {
    
    
    public Realizetype() {
    
    
        System.out.println("具体的原型对象创建完成!");
    }

    @Override
    public Realizetype clone() throws CloneNotSupportedException {
    
    
        System.out.println("具体原型复制成功!");
        return (Realizetype) super.clone();
    }
}

Client(测试类):

public class Client {
    
    
    public static void main(String[] args) throws CloneNotSupportedException {
    
    
        //创建一个原型类对象
        Realizetype realizetype = new Realizetype();
        //调用Realizetype的clone方法进行对象克隆
        Realizetype clone = realizetype.clone();

        System.out.println("原形对象和克隆出来的是否是同一个对象?:"+(realizetype==clone));
    }
}

运行结果如下:

image-20230328221527801


3.1 浅克隆

需求:

同一学校的“三好学生”奖状除了获奖人姓名不同,其他都相同,可以使用原型模式复制多个“三好学生”奖状出来,然后在修改奖状上的名字即可。

1)定义一个学生类

public class Student {
    
    

    private String name;

    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    public String getName() {
    
    
        return name;
    }

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

2)定义一个三好学生类

public class Citation implements Cloneable {
    
    

    //三好学生
    private Student stu;

    public Student getStu() {
    
    
        return stu;
    }

    public void setStu(Student stu) {
    
    
        this.stu = stu;
    }

    @Override
    public Citation clone() throws CloneNotSupportedException {
    
    
        return (Citation) super.clone();
    }

    public void show() {
    
    
        System.out.println(stu.getName() + "同学:在2020学年第一学期中表现优秀,被评为三好学生。特发此状!");
    }
}

3)测试浅克隆

public class Test {
    
    
    public static void main(String[] args) throws CloneNotSupportedException {
    
    
        //创建原型对象
        Citation citation = new Citation();

        Student student = new Student();
        student.setName("张三");
        citation.setStu(student);

        //浅克隆
        Citation citation1 = citation.clone();
        Student student1 = citation.getStu();
        student1.setName("李四");

        System.out.println("student和student1是不是同一个对象:"+(student==student1));
        citation.show();
        citation1.show();
    }
}

根据浅克隆的定义,如果该测试方法的克隆属于浅克隆,那么 student==student1 必然为 true,因为它们指向同一地址。点击运行,结果如下:

image-20230328222016820

我们发现,student==student1果然为true,所以说这两个引用对象指向同一个地址,才会“里斯同学:在…”语句才会输出两次。


3.2 深克隆

深拷贝是基于序列化来实现的,所以需要克隆的对象都得实现 Serializable 接口,我们在学生类和三好学生类中都实现该接口。

编写测试方法如下:

public class Test {
    
    
    public static void main(String[] args) throws Exception {
    
    
        //创建原型对象
        Citation citation = new Citation();

        Student student = new Student();
        student.setName("张三");
        citation.setStu(student);

        //深克隆
        //序列化对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\a.txt"));
        oos.writeObject(citation);
        oos.close();

        //反序列化对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\a.txt"));
        Citation citation1 = (Citation) ois.readObject();
        Student student1 = citation1.getStu();
        student1.setName("李四");
        ois.close();

        System.out.println("student和student1是不是同一个对象:"+(student==student1));
        citation.show();
        citation1.show();
    }
}

根据深拷贝定义,如果该测试方法属于深拷贝,那么 student==student1 必然为 false。点击运行,结果如下:

image-20230328222854298

猜你喜欢

转载自blog.csdn.net/Decade_Faiz/article/details/129826883