设计模式-原型模式

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

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

原型模式

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

为什么要使⽤原型模式

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

原型模式的基本实现

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

  • 创建⼀个抽象类或接⼝,声明⼀个克隆⽅法 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);
    }
}

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




/**
 * @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是同一个

基本实现 : (深克隆)

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流来实现 

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

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

这期就到这里 , 下期见!

  • 21
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值