设计模式-原型模式

 大家好我是苏麟 , 今天聊聊原型模式.

此系列全是帮忙宣传 , 原创放在下面了 .

原型模式

原型模式⼀种创建型设计模式,该模式的核⼼思想是基于现有的对象创建新的对象,⽽不是从头开始创建。 在原型模式中,通常有⼀个原型对象,它被⽤作创建新对象的模板。新对象通过复制原型对象的属性和状态来创 建,⽽⽆需知道具体的创建细节。

为什么要使⽤原型模式

如果⼀个对象的创建过程⽐较复杂时(⽐如需要经过⼀系列的计算和资源消耗),那每次创建该对象都需要消耗资源,⽽通过原型模式就可以复制现有的⼀个对象来迅速创建/克隆⼀个新对象,不必关⼼具体的创建细节,可以降 低对象创建的成本

原型模式的基本实现

原型模式的实现过程即上⾯描述模块的实现过程:

  • 创建⼀个抽象类或接⼝,声明⼀个克隆⽅法 clone
  • 实现具体原型类,重写克隆⽅法
  • 客户端中实例化具体原型类的对象,并调⽤其克隆⽅法来创建新的对象。

原型模式的克隆分为浅克隆和深克隆
浅克隆:创建一个新对象,新对象的属性和原来对象完全相同,对于非基本类型属性,仍指向原有属性所指向的对象的内存地址。
深克隆:创建一个新对象,属性中引用的其他对象也会被克降,不再指向原有对象地址。

基本实现 : (浅克隆)

/**
 * @className: Realize
 * @author: SL 苏麟
 **/
public class RealizeType implements Cloneable{

    public RealizeType() {
        System.out.println("原型对象创建完毕!!!");
    }

    @Override
    protected RealizeType clone() throws CloneNotSupportedException {
        System.out.println("克隆对象克隆成功");
        return (RealizeType) super.clone();
    }


}
/**
 * @className: Client
 * @author: SL 苏麟
 **/
public class Client {
    public static void main(String[] args) throws CloneNotSupportedException {
        RealizeType realizeType = new RealizeType();
        RealizeType clone = realizeType.clone();

        System.out.println(realizeType == clone);
    }
}

证明我们创建出来的对象不是一个

设计模式-原型模式_第1张图片




/**
 * @className: Student
 * @author: SL 苏麟
 **/
public class Student {
    private String name;

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
/**
 * @className: Realize
 * @author: SL 苏麟
 **/
public class RealizeType implements Cloneable{

    private Student student;

    public void setStudent(Student student) {
        this.student = student;
    }

    public Student getStudent() {
        return student;
    }

    public RealizeType() {
    }

    @Override
    protected RealizeType clone() throws CloneNotSupportedException {
        return (RealizeType) super.clone();
    }

    @Override
    public String toString() {
        return "RealizeType{" +
                "student=" + student +
                '}';
    }
}
/**
 * @className: Client
 * @author: SL 苏麟
 **/
public class Client {
    public static void main(String[] args) throws CloneNotSupportedException {
        RealizeType realizetype = new RealizeType();
        Student student = new Student();
        student.setName("苏麟");
        realizetype.setStudent(student);

        RealizeType clone = realizetype.clone();

        System.out.println(realizetype.getStudent() == clone.getStudent());
        System.out.println(realizetype);
        System.out.println(clone);
    }
}

浅拷贝的引用对象Student是同一个

设计模式-原型模式_第2张图片

基本实现 : (深克隆)

import java.io.Serializable;

/**
 * @className: Student
 * @author: SL 苏麟
 **/
public class Student implements Serializable {
    private String name;

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
import java.io.Serializable;

/**
 * @className: Realize
 * @author: SL 苏麟
 **/
public class RealizeType implements Cloneable, Serializable {

    private Student student;

    public void setStudent(Student student) {
        this.student = student;
    }

    public Student getStudent() {
        return student;
    }

    public RealizeType() {
    }

    @Override
    protected RealizeType clone() throws CloneNotSupportedException {
        return (RealizeType) super.clone();
    }

    @Override
    public String toString() {
        return "RealizeType{" +
                "student=" + student +
                '}';
    }
}
import java.io.*;

/**
 * @className: Client
 * @author: SL 苏麟
 **/
public class Client {
    public static void main(String[] args) throws Exception {
        RealizeType realizetype = new RealizeType();
        Student student = new Student();
        student.setName("苏麟");
        realizetype.setStudent(student);


        //创建对象输出流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/sl/sl.txt"));
        //写对象
        oos.writeObject(realizetype);
        //释放资源
        oos.close();

        //创建对象输入流对象
        ObjectInputStream ooi = new ObjectInputStream(new FileInputStream("D:/sl/sl.txt"));
        //读取对象
        RealizeType clone = (RealizeType) ooi.readObject();
        //释放资源
        ooi.close();
        clone.getStudent().setName("杨科");

        System.out.println(realizetype);
        System.out.println(clone);
    }
}

深拷贝需要借助IO流来实现 

设计模式-原型模式_第3张图片

原创地址 : GitHub - youngyangyang04/kama-DesignPattern: 卡码网-23种设计模式精讲,每种设计模式都配套代码练习题,支持 Java,CPP,Python,Go

设计模式-原型模式_第4张图片

了解更多 : 原型模式 | 菜鸟教程 (runoob.com)

设计模式-原型模式_第5张图片

这期就到这里 , 下期见!

你可能感兴趣的:(设计模式,设计模式,java,开发语言,原型模式)