java 设计模式实战,原始模型模式之写作业,克隆以后就是新的

简介: 通过给出一个原型对象指明所要创建的对象的类型,然后通过复制这个原型对象来获取的更多的同类型的对象。这让我不由自主的想起克隆技术,还记得克隆羊吗?

什么是原始模型模式

通过给出一个原型对象指明所要创建的对象的类型,然后通过复制这个原型对象来获取的更多的同类型的对象。

这让我不由自主的想起克隆技术,还记得克隆羊吗?我们接下来讲的内容和克隆羊不能说关系密切,只能说毫无关系。
在这里插入图片描述

设计模式和编程语言无关,但是二当家的依然用Java语言去实战举例。而且Java有标准的实现原始模型模式的方法。


原始模型模式中的角色

img

  1. Prototype:抽象类或者一个接口,给出具体模型需要的接口。
  2. ConcretePrototype:继承抽象原型模型角色,被复制的对象。
  3. Client:提出复制请求。

抽象原型角色(Prototype)

我们用家庭作业为抽象原型角色(Prototype)。我们这里的作业是可以抄的。大家不要学哈。

package com.secondgod.prototype;

/**
 * 作业
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public interface IHomework {
    /**
     * 抄一份
     * @return
     */
    IHomework copy();

    /**
     * 修改所有者
     * @param owner
     */
    void setOwner(String owner);
}

具体原型角色(ConcretePrototype)

我们用语文作业作为具体原型角色(ConcretePrototype)。

package com.secondgod.prototype;

import java.text.MessageFormat;

/**
 * 语文作业
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class ChineseHomework implements IHomework {
    /**
     * 作业的所有者
     */
    private String owner;
    /**
     * 作业标题/作业要求
     */
    private String title;
    /**
     * 作业内容
     */
    private String content;

    public ChineseHomework(String owner, String title, String content) {
        this.owner = owner;
        this.title = title;
        this.content = content;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String toString() {
        return MessageFormat.format("owner:{0},title:{1},content:{2}", owner, title, content);
    }

    @Override
    public IHomework copy() {
        ChineseHomework homework = new ChineseHomework(this.getOwner(), this.getTitle(), this.getContent());
        return homework;
    }
}

客户端角色(Client)

我们测试一下。

package com.secondgod.prototype;

/**
 * 测试原始模型
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Test {
    public static void main(String[] args) {
        // 老师让写作业,大当家按时完成
        IHomework homework = new ChineseHomework("大当家的", "作文-最崇拜的人", "不瞒你们说,我最崇拜的是二当家的");
        // 二当家的没按时完成,决定去抄大当家的作业~
        IHomework newHomework = homework.copy();
        newHomework.setOwner("二当家的");

        System.out.println(homework);
        System.out.println(newHomework);
    }
}

image-20210809135840702

和我们的预期一致,Nice。二当家的竟然崇拜自己,但这是因为懒得写作业,不是真的。


使用Java内置机制实现原始模型模式

在Object类中有这样一个方法,Java中所有的类都继承自Object类,也就是说所有的类内部都可以复制自己。但是却不能直接调用别的类的克隆方法。也就是说有统一的方式,但是默认不可用。
在这里插入图片描述

我们改一下语文作业类,使用clone方法去尝试,克隆自己。

package com.secondgod.prototype;

import java.text.MessageFormat;

/**
 * 语文作业
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class ChineseHomework implements IHomework {
    /**
     * 作业的所有者
     */
    private String owner;
    /**
     * 作业标题/作业要求
     */
    private String title;
    /**
     * 作业内容
     */
    private String content;

    public ChineseHomework(String owner, String title, String content) {
        this.owner = owner;
        this.title = title;
        this.content = content;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String toString() {
        return MessageFormat.format("owner:{0},title:{1},content:{2}", owner, title, content);
    }

    @Override
    public IHomework copy() {
        try {
            return (ChineseHomework) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
}

在这里插入图片描述

这时候会报错,因为我们还少做一件事。我们需要把语文作业类实现Cloneable接口,然而这个接口里没有任何抽象方法,仅仅是一个标记接口。这就像注解一样,就是为了明确声明可以克隆。
在这里插入图片描述

实现接口后,则可以正确运行。

package com.secondgod.prototype;

import java.text.MessageFormat;

/**
 * 语文作业
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class ChineseHomework implements IHomework, Cloneable {
    /**
     * 作业的所有者
     */
    private String owner;
    /**
     * 作业标题/作业要求
     */
    private String title;
    /**
     * 作业内容
     */
    private String content;

    public ChineseHomework(String owner, String title, String content) {
        this.owner = owner;
        this.title = title;
        this.content = content;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String toString() {
        return MessageFormat.format("owner:{0},title:{1},content:{2}", owner, title, content);
    }

    @Override
    public IHomework copy() {
        try {
            return (ChineseHomework) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
}

在这里插入图片描述

和我们自己实现copy效果一样。clone是一个native方法,是交给本地实现的,通常是直接内存拷贝。


浅拷贝和深拷贝

浅拷贝:创建一个新对象,然后将当前对象的非静态字段复制到该新对象,如果字段是值类型的,那么对该字段执行复制;如果该字段是引用类型的话,则复制引用但不复制引用的对象。因此,原始对象及其副本引用同一个对象。

深拷贝:创建一个新对象,然后将当前对象的非静态字段复制到该新对象,无论该字段是值类型的还是引用类型,都复制独立的一份。当你修改其中一个对象的任何内容时,都不会影响另一个对象的内容。

二当家的理解方式是,浅拷贝就是仅拷贝当前对象的内容,深拷贝就是递归拷贝当前对象和当前对象的引用类型属性的内容,直到全部都是基本数据类型的属性为止。另外,仅仅使用clone方法是浅拷贝。

package com.secondgod.prototype;

/**
 * 测试原始模型
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Test implements Cloneable {
    private Field field;

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }

    public Test clone() throws CloneNotSupportedException {
        return (Test) super.clone();
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Test t = new Test();
        t.setField(new Field());

        Test cloneT = t.clone();

        System.out.println(t == cloneT);
        System.out.println(t.getField() == cloneT.getField());
    }
}

class Field {

}

在这里插入图片描述

源对象和克隆出的新对象field属性值是同一个对象。所以是浅拷贝。


怎么实现深拷贝

  1. 让每个引用类型属性内部都重写clone() 方法,然后需要对所有引用类型属性都调用clone方法。
package com.secondgod.prototype;

/**
 * 测试原始模型
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Test implements Cloneable {
    private Field field;

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }

    public Test clone() throws CloneNotSupportedException {
        return (Test) super.clone();
    }

    public Test deepClone() throws CloneNotSupportedException {
        Test t = new Test();
        t.setField(this.getField().deepClone());
        return t;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Test t = new Test();
        t.setField(new Field());

        Test cloneT = t.clone();

        System.out.println(t == cloneT);
        System.out.println(t.getField() == cloneT.getField());

        Test deepCloneT = t.deepClone();

        System.out.println(t == deepCloneT);
        System.out.println(t.getField() == deepCloneT.getField());
    }
}

class Field implements Cloneable {
    public Field clone() throws CloneNotSupportedException {
        return (Field) super.clone();
    }

    public Field deepClone() throws CloneNotSupportedException {
        // 没有引用类型属性
        return this.clone();
    }
}

在这里插入图片描述

这种方式需要递归每个引用类型的属性,要把他们的类都实现深拷贝,只到仅有基本数据类型为止。

  1. 利用序列化,这是个偷懒的办法。但是二当家的觉得更安全,如果你确定是要深拷贝,使用该方式可以防止某处漏实现clone方法,而变成不完全的深拷贝。
package com.secondgod.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * 测试原始模型
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Test implements Cloneable, Serializable {
    private static final long serialVersionUID = 5439585691441925427L;
    private Field field;

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }

    /**
     * 浅拷贝
     * @return
     * @throws CloneNotSupportedException
     */
    public Test clone() throws CloneNotSupportedException {
        return (Test) super.clone();
    }

    /**
     * 深拷贝
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public Test deepClone() throws IOException, ClassNotFoundException {
        // 序列化
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream    oos = new ObjectOutputStream(bos);
        oos.writeObject(this);

        // 反序列化
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream    ois = new ObjectInputStream(bis);

        return (Test) ois.readObject();
    }

    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
        Test t = new Test();
        t.setField(new Field());

        Test cloneT = t.clone();

        System.out.println(t == cloneT);
        System.out.println(t.getField() == cloneT.getField());

        Test deepCloneT = t.deepClone();

        System.out.println(t == deepCloneT);
        System.out.println(t.getField() == deepCloneT.getField());
    }
}

class Field implements Serializable {
    private static final long serialVersionUID = 4530741098042781181L;
}

在这里插入图片描述

到底使用浅拷贝还是深拷贝,要根据实际情况。但是有一点是确定的,深拷贝需要创建更多对象,占用更多内存。


尾声

最后二当家的再次声明,抄作业是不对的哦。


非常感谢你阅读本文~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://developer.aliyun.com/profile/sqd6avc7qgj7y 博客原创~
相关文章
|
4天前
|
设计模式 算法 搜索推荐
Java 设计模式之策略模式:灵活切换算法的艺术
策略模式通过封装不同算法并实现灵活切换,将算法与使用解耦。以支付为例,微信、支付宝等支付方式作为独立策略,购物车根据选择调用对应支付逻辑,提升代码可维护性与扩展性,避免冗长条件判断,符合开闭原则。
84 35
|
4天前
|
设计模式 消息中间件 传感器
Java 设计模式之观察者模式:构建松耦合的事件响应系统
观察者模式是Java中常用的行为型设计模式,用于构建松耦合的事件响应系统。当一个对象状态改变时,所有依赖它的观察者将自动收到通知并更新。该模式通过抽象耦合实现发布-订阅机制,广泛应用于GUI事件处理、消息通知、数据监控等场景,具有良好的可扩展性和维护性。
51 8
|
5天前
|
设计模式 网络协议 数据可视化
Java 设计模式之状态模式:让对象的行为随状态优雅变化
状态模式通过封装对象的状态,使行为随状态变化而改变。以订单为例,将待支付、已支付等状态独立成类,消除冗长条件判断,提升代码可维护性与扩展性,适用于状态多、转换复杂的场景。
45 0
|
5天前
|
设计模式 Java Spring
Java 设计模式之责任链模式:优雅处理请求的艺术
责任链模式通过构建处理者链,使请求沿链传递直至被处理,实现发送者与接收者的解耦。适用于审批流程、日志处理等多级处理场景,提升系统灵活性与可扩展性。
66 2
|
19天前
|
设计模式 缓存 安全
【JUC】(6)带你了解共享模型之 享元和不可变 模型并初步带你了解并发工具 线程池Pool,文章内还有饥饿问题、设计模式之工作线程的解决于实现
JUC专栏第六篇,本文带你了解两个共享模型:享元和不可变 模型,并初步带你了解并发工具 线程池Pool,文章中还有解决饥饿问题、设计模式之工作线程的实现
80 2
|
1月前
|
Java
Java基础学习day08-作业
本作业涵盖Java中Lambda表达式的应用,包括Runnable与Comparator接口的简化实现、自定义函数式接口NumberProcessor进行加减乘及最大值操作,以及通过IntProcessor处理整数数组,实现遍历、平方和奇偶判断等功能,强化函数式编程实践。
55 5
|
1月前
|
Java
Java基础学习day07-作业
本作业包含六个Java编程案例:1)动物类继承与多态;2)加油卡支付系统;3)员工管理类设计;4)学生信息统计接口;5)USB设备控制;6)家电智能控制。综合运用抽象类、接口、继承、多态等面向对象技术,强化Java基础编程能力。
150 3
|
1月前
|
Java
Java基础学习day06-作业
本内容为Java基础学习作业,涵盖两个案例:一是通过Card类及其子类GoldenCard、SilverCard实现加油卡系统,体现封装与继承;二是通过Shape类及子类Circle、Rectangle演示多态与方法重写,强化面向对象编程理解。
52 1
|
1月前
|
设计模式 人工智能 算法
基于多设计模式的状态扭转设计:策略模式与责任链模式的实战应用
接下来,我会结合实战案例,聊聊如何用「策略模式 + 责任链模式」构建灵活可扩展的状态引擎,让抽奖系统的状态管理从「混乱战场」变成「有序流水线」。
|
1月前
|
Java
Java基础学习day05-作业
本文为Java基础学习第五天作业,通过五个案例练习类与对象的定义、构造方法、set/get方法及成员方法的应用。涵盖女友、学生、教师、手机和电影等类的设计与测试,强化面向对象编程基础。
59 2

热门文章

最新文章