2024年最全面试官:设计模式中的原型模式是什么?_面试什么是原型,京东最新大数据开发面试真题解析

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

public abstract class ProtoType {
    public abstract ProtoType clone();
}

2、然后创建一个具体原型类

public class ConcretePrototype extends ProtoType {
    private String name;    //成员变量

    public String getName() {
        return name;
    }

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

    @Override
    public ProtoType clone() {
        ProtoType protoType = new ConcretePrototype();  //创建新对象
        ((ConcretePrototype) protoType).setName(this.name);
        return protoType;
    }
}

3、然后创建一个客户类

public class Client {
    public static void main(String[] args) {
        ConcretePrototype prototype = new ConcretePrototype();
        prototype.setName("孙悟空");
        ConcretePrototype clone = (ConcretePrototype)prototype.clone();
        System.out.println(clone.getName());
    }
}

在这里插入图片描述

面试官:你这个是自己写的克隆方法,可以使用java中自带的克隆方法吗?

我:在java中Object类提供了一个clone()方法,可以将一个java对象复制一份,因此可以使用clone()方法来实现对象的克隆,不过这里要注意,这个方法只能实现浅克隆。而且要使用这个方法就需要实现Cloneable接口。

1、具体克隆类实现Cloneable接口

public class ConcretePrototype implements Cloneable{
    @Override
    public Object clone() {
        Object object = null;
        try {
            object = super.clone(); //浅克隆
        }catch (CloneNotSupportedException exception){
            System.err.println("Not support cloneable");
        }
        return object;
    }
}


2、客户类用来测试是否能够克隆

public class Client {
    public static void main(String[] args) {
        ConcretePrototype prototype = new ConcretePrototype();
        ConcretePrototype copy = (ConcretePrototype)prototype.clone();
        System.out.println(prototype == copy);
    }
}

在这里插入图片描述

面试官:你上面说的是浅克隆的方法,那么我想要深克隆怎么办呢?

我:深克隆需要实现Serialization序列化方式来实现深克隆。序列化就是将对象写到流的过程,写到流中的对象是原有对象的一个复制,而原对象仍然存在于内存中。通过序列化的方式不仅可以复制基础类型,还可以复制引用类型。

面试官:可以简单写一个深克隆的例子吗?

1、首先定义一个学生实体。

public class Student implements Serializable {
    private String name;
    private int 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;
    }
}

2、然后定义一个老师实体,其中有自己写的深克隆方法

public class Teacher implements Serializable {
    private Student student;
    private String name;
    private String add;

    public Student getStudent() {
        return student;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAdd() {
        return add;
    }

    public void setAdd(String add) {
        this.add = add;
    }

    //在对象中自定义克隆的方法
    public Teacher deepClone() throws IOException,ClassNotFoundException, OptionalDataException {
        //将对象写入流中
        ByteArrayOutputStream out  = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(this);
        //将对象从流中取出
        ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (Teacher) ois.readObject();
    }
}


3、编写测试类

public class ClientDeep {
    public static void main(String[] args) {
        Teacher t_proto,t_new = null;
        t_proto = new Teacher();    //创建原型对象
        Student student = new Student();    //创建学生对象
        t_proto.setStudent(student);    //将学生添加到老师中
        try {
            t_new = t_proto.deepClone();    //调用深克隆方法创建克隆对象
        }catch (Exception e){
            System.err.println("克隆失败!");
        }
        //比较两个老师对象
        System.out.println("老师对象是否相同:"+ (t_proto == t_new));
        //比较老师中的两个学生是否相同
        System.out.println("学生对象是否相同:"+ (t_proto.getStudent() == t_new.getStudent()));
    }


![img](https://img-blog.csdnimg.cn/img_convert/d01bb688d72883cfe89f823c9eae1b07.png)
![img](https://img-blog.csdnimg.cn/img_convert/c066923be1537cca48df3aa0633c012f.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值