美文网首页设计模式专家
spring 设计模式篇—原型模式

spring 设计模式篇—原型模式

作者: Lucksheep | 来源:发表于2018-08-10 18:22 被阅读58次

一、啥是原型模式?

   把对象中配置的依赖关系,在每次使用对象之前,都会创建一个新的对象,并且将依赖关系完整的赋值给这个新创建的对象。
   如:spring beacon中的scope="prototype";

二、深克隆、浅克隆

   浅克隆:不只是克隆了相同的属性值,还克隆了引用地址;

   深克隆:单纯的克隆相同的属性值,不克隆引用地址;

三、demo

被克隆类:

/**
 * @ather: lucksheep
 * @date: 2018/8/10 17:21
 * @description:
 */
public class BaseClone implements Serializable {

    private String tall;

    public String getTall() {
        return tall;
    }

    public void setTall(String tall) {
        this.tall = tall;
    }
}

克隆类:

import lombok.Data;

import java.io.*;

/**
 * @ather: lucksheep
 * @date: 2018/8/10 16:48
 * @description:
 */
@Data
public class Prototype implements Cloneable ,Serializable{

    public String name;

    public BaseClone baseClone;

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

    public Object deepClone(){
        try {
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois =new ObjectInputStream(bis);

            Prototype pro=(Prototype) ois.readObject();
            return pro;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

测试:

/**
 * @ather: lucksheep
 * @date: 2018/8/10 16:49
 * @description:
 */
public class Test {

    public static void main(String[] args) {

        Prototype p = new Prototype();
        p.setName("zhangsan");

        BaseClone baseClone=new BaseClone();
        baseClone.setTall("我是你爸爸!");
        p.setBaseClone(baseClone);

        System.out.println(p.getBaseClone().getTall()+"  "+p.getBaseClone());
        try {
            Prototype p2=(Prototype) p.clone();
            System.out.println(p2.getBaseClone().getTall()+"  "+p2.getBaseClone());

            Prototype p3=(Prototype) p.deepClone();
            System.out.println(p3.getBaseClone().getTall()+"  "+p3.getBaseClone());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

测试结果:

我是你爸爸!  com.demo.prototype.BaseClone@140e19d
我是你爸爸!  com.demo.prototype.BaseClone@140e19d
我是你爸爸!  com.demo.prototype.BaseClone@1d6c5e0

Process finished with exit code 0

相关文章

  • Java架构师课程

    Java架构班开学典礼 Spring中常用的设计模式概述及工厂模式详解 单例模式及原型模式单例模式及原型模式单例模...

  • spring 设计模式篇—原型模式

    一、啥是原型模式?    把对象中配置的依赖关系,在每次使用对象之前,都会创建一个新的对象,并且将依赖关系完整的赋...

  • JavaJavascript基础进阶(十七)JS中常用的设计模式

    单利设计模式、构造原型设计模式、发布订阅设计模式、promise设计模式 单利模式 构造原型设计模式 最贴近OOP...

  • spring框架中的设计模式三

    以下主要讲spring的原型,对象池,观察者模式: 原型模式 这篇文章的第一个设计模式是原型。可以通过官方文档查找...

  • Spring 准备内容

    准备内容 原型设计模式 PropotypeModle 原型模式也属于创造型设计模式,用原型实例指定创建对象的种类,...

  • 设计模式问答(一)

    设计模式问答(一) 什么是设计模式?您能说出工厂模式、抽象工厂模式、创建者模式、原型模式、原型模式的潜复制及深复制...

  • Spring 设计模式及事务

    Spring 设计模式应用 工厂设计模式 : Spring 通过 BeanFactory、ApplicationC...

  • 原型和原型链篇

    原型和原型链 1.理解原型设计模式以及JavaScript中的原型规则 原型设计模式JavaScript是一种基于...

  • 设计模式(十六)原型模式

    前言 公众号有同学留言设计模式,才发现好久没有写设计模式了。关于创建型设计模式只差原型模式没写了,这一篇就来填补这...

  • 初始设计模式之原型模式

    原型模式是什么? 原型模式怎么用?浅拷贝深拷贝 原型模式再理解 一、原型模式是什么? ​ 原型模式是一种创建型设计...

网友评论

    本文标题:spring 设计模式篇—原型模式

    本文链接:https://www.haomeiwen.com/subject/acklbftx.html