您当前的位置: 首页 > 

Dongguo丶

暂无认证

  • 2浏览

    0关注

    472博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

设计模式之原型模式

Dongguo丶 发布时间:2019-01-10 21:51:34 ,浏览量:2

原型模式是一种对象创建型模型,它采用复制原型对象的方法来创建对象的实例,具有与原型一样的数据。

模式定义

所谓原型模式就是用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。

​ 在原型模式中,所发动创建的对象通过请求原型对象来拷贝原型对象自己来实现创建过程,当然所发动创建的对象需要知道原型对象的类型。这里也就是说所发动创建的对象只需要知道原型对象的类型就可以获得更多的原型实例对象,至于这些原型对象时如何创建的根本不需要关心。

​ 讲到原型模式了,我们就不得不区分两个概念:深拷贝、浅拷贝。

​ 浅拷贝:使用一个已知实例对新创建实例的成员变量逐个赋值,这个方式被称为浅拷贝。

​ 深拷贝:当一个类的拷贝构造方法,不仅要复制对象的所有非引用成员变量值,还要为引用类型的成员变量创建新的实例,并且初始化为形式参数实例值。

举一个克隆羊例子

现在有一只羊tom,姓名为: tom, 年龄为:1,颜色为:白色,

编写程序创建和tom 羊属性完全相同的5只羊。

传统方式解决克隆羊问题

思路分析(图解)

创建一个羊类Sheep 直接使用new对象的方式创建五只羊

package com.dongguo.prototype;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-11:31
 * @description:
 */
public class Sheep {
    private String name;
    private int age;
    private String color;

    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    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;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}
package com.dongguo.prototype;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-11:32
 * @description:
 */
public class Client {
    public static void main(String[] args) {

        //传统的方法
        Sheep sheep = new Sheep("tom", 1, "白色");

        Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
        Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
        Sheep sheep4 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
        Sheep sheep5 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
        System.out.println(sheep);
        System.out.println(sheep2);
        System.out.println(sheep3);
        System.out.println(sheep4);
        System.out.println(sheep5);
    }
}
运行结果:
Sheep{name='tom', age=1, color='白色'}
Sheep{name='tom', age=1, color='白色'}
Sheep{name='tom', age=1, color='白色'}
Sheep{name='tom', age=1, color='白色'}
Sheep{name='tom', age=1, color='白色'}

传统的方式的优缺点

1) 优点是比较好理解,简单易操作。

2) 在创建新的对象时,总是需要重新获取原始对象的属性,如果创建的对象比较复杂时,效率较低

3) 总是需要重新初始化对象,而不是动态地获得对象运行时的状态, 不够灵活

原型模式

​ 下图是原型模式的UML结构图:

​ 原型模式主要包含如下三个角色:

​ Prototype:抽象原型类。声明克隆自身的接口。

ConcretePrototype:具体原型类。实现克隆的具体操作。

Client:客户类。让一个原型克隆自身,从而获得一个新的对象。

改进的思路分析

思路:Java 中Object 类是所有类的根类,Object 类提供了一个clone()方法,该方法可以将一个Java 对象复制一份,因此在java中可以直接使用clone()方法来复制一个对象。但是需要实现clone 的Java 类必须要实现一个接口Cloneable,该接口表示该类能够复制且具有复制的能力=>原型模式

​ Java中任何实现了Cloneable接口的类都可以通过调用clone()方法来复制一份自身然后传给调用者。一般而言,clone()方法满足: (1) 对任何的对象x,都有x.clone() !=x,即克隆对象与原对象不是同一个对象。

​ (2) 对任何的对象x,都有x.clone().getClass()==x.getClass(),即克隆对象与原对象的类型一样。

​ (3) 如果对象x的equals()方法定义恰当,那么x.clone().equals(x)应该成立。

使用原型模式改进传统方式,让程序具有更高的效率和扩展性。

package com.dongguo.prototype.improve;
/**
 * @author Dongguo 
 * @date 2021/8/22 0022 11:36
 * @description:
 */
public class Sheep implements Cloneable {
    private String name;
    private int age;
    private String color;

    public Sheep(String name, int age, String color) {
        super();
        this.name = name;
        this.age = age;
        this.color = color;
    }

    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;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }

    //克隆该实例,使用默认的clone 方法来完成
    @Override
    protected Object clone() {
        Sheep sheep = null;
        try {
            sheep = (Sheep) super.clone();
        } catch (Exception e) {

            System.out.println(e.getMessage());
        }
        return sheep;
    }
}
package com.dongguo.prototype.improve;

/**
 * @author Dongguo
 * @date 2021/8/22 0022 11:36
 * @description:
 */
public class Client {
    public static void main(String[] args) {
        System.out.println("原型模式完成对象的创建");

        Sheep sheep = new Sheep("tom", 1, "白色");
        Sheep sheep2 = (Sheep) sheep.clone(); //克隆
        Sheep sheep3 = (Sheep) sheep.clone(); //克隆
        Sheep sheep4 = (Sheep) sheep.clone(); //克隆
        Sheep sheep5 = (Sheep) sheep.clone(); //克隆
        System.out.println("sheep2 =" + sheep2);
        System.out.println("sheep3 =" + sheep3);
        System.out.println("sheep4 =" + sheep4);
        System.out.println("sheep5 =" + sheep5);
    }
}
运行结果:
原型模式完成对象的创建
sheep2 =Sheep{name='tom', age=1, color='白色'}
sheep3 =Sheep{name='tom', age=1, color='白色'}
sheep4 =Sheep{name='tom', age=1, color='白色'}
sheep5 =Sheep{name='tom', age=1, color='白色'}

深入讨论-浅拷贝和深拷贝

浅拷贝的介绍

对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将该属性值复制一份给新的对象。

对于数据类型是引用数据类型的成员变量,比如说成员变量是某个数组、某个类的对象等,那么浅拷贝会进行

引用传递,也就是只是将该成员变量的引用值(内存地址)复制一份给新的对象。因为实际上两个对象的该成员变量都指向同一个实例。在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成员变量值

 前面我们克隆羊就是浅拷贝,浅拷贝是使用Object中默认的clone()方法来实现

@Override
    protected Object clone() {
        Sheep sheep = null;
        try {
            sheep = (Sheep) super.clone();
        } catch (Exception e) {

            System.out.println(e.getMessage());
        }
        return sheep;
    }

验证

这里sheep.friend都是指向同一个对象的引用,sheep的friend指向的都是同一个实例

所以Object的clone方法时浅拷贝

package com.dongguo.prototype.improve2;
/**
 * @author Dongguo 
 * @date 2021/8/22 0022 11:36
 * @description:
 */
public class Sheep implements Cloneable {
    private String name;
    private int age;
    private String color;
    private String address = "蒙古羊";
    public Sheep friend; //是对象, 克隆是会如何处理, 默认是浅拷贝

    public Sheep(String name, int age, String color) {
        super();
        this.name = name;
        this.age = age;
        this.color = color;
    }

    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;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Sheep [name=" + name + ", age=" + age + ", color=" + color + ", address=" + address + "]";
    }

    //克隆该实例,使用默认的clone 方法来完成
    @Override
    protected Object clone() {
        Sheep sheep = null;
        try {
            sheep = (Sheep) super.clone();
        } catch (Exception e) {

            System.out.println(e.getMessage());
        }
        return sheep;
    }
}
package com.dongguo.prototype.improve2;

/**
 * @author Dongguo
 * @date 2021/8/22 0022 11:36
 * @description:
 */
public class Client {
    public static void main(String[] args) {
        System.out.println("原型模式完成对象的创建");
        Sheep sheep = new Sheep("tom", 1, "白色");
        sheep.friend = new Sheep("jack", 2, "黑色");
        Sheep sheep2 = (Sheep)sheep.clone(); //克隆
        Sheep sheep3 = (Sheep)sheep.clone(); //克隆
        Sheep sheep4 = (Sheep)sheep.clone(); //克隆
        Sheep sheep5 = (Sheep)sheep.clone(); //克隆
        System.out.println("sheep2 =" + sheep2 + "sheep2.friend=" + sheep2.friend.hashCode());
        System.out.println("sheep3 =" + sheep3 + "sheep3.friend=" + sheep3.friend.hashCode());
        System.out.println("sheep4 =" + sheep4 + "sheep4.friend=" + sheep4.friend.hashCode());
        System.out.println("sheep5 =" + sheep5 + "sheep5.friend=" + sheep5.friend.hashCode());
    }

}
运行结果:
原型模式完成对象的创建
sheep2 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep2.friend=460141958
sheep3 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep3.friend=460141958
sheep4 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep4.friend=460141958
sheep5 =Sheep [name=tom, age=1, color=白色, address=蒙古羊]sheep5.friend=460141958

 

深拷贝基本介绍

复制对象的所有基本数据类型的成员变量值

为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员变量所引用的对象,直到该对象可达的所有对象。也就是说,对象进行深拷贝要对整个对象(包括对象的引用类型)进行拷贝

深拷贝实现方式1:重写clone 方法来实现深拷贝

深拷贝实现方式2:通过对象序列化实现深拷贝(推荐)

深拷贝应用实例
package com.dongguo.prototype.deepclone;

import java.io.Serializable;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-11:53
 * @description:
 */
public class DeepCloneableTarget implements Serializable, Cloneable {

    private static final long serialVersionUID = 1L;
    private String cloneName;
    private String cloneClass;

    

    //构造器
    public DeepCloneableTarget(String cloneName, String cloneClass) {
        this.cloneName = cloneName;
        this.cloneClass = cloneClass;
    }

    //因为该类的属性,都是String , 因此我们这里使用默认的clone 完成即可
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    @Override
    public String toString() {
        return "DeepCloneableTarget{" +
                "cloneName='" + cloneName + '\'' +
                ", cloneClass='" + cloneClass + '\'' +
                '}';
    }
}
package com.dongguo.prototype.deepclone;

import java.io.*;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-11:56
 * @description:
 */
public class DeepProtoType implements Serializable, Cloneable {
    public String name; //String 属性
    public DeepCloneableTarget deepCloneableTarget;// 引用类型

    public DeepProtoType() {
        super();
    }

    @Override
    public String toString() {
        return "DeepProtoType{" +
                "name='" + name + '\'' +
                ", deepCloneableTarget=" + deepCloneableTarget +
                '}';
    }

    //深拷贝- 方式1 使用clone 方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object deep = null;
        //这里完成对基本数据类型(属性)和String 的克隆
        deep = super.clone();
        //对引用类型的属性,进行单独处理
        DeepProtoType deepProtoType = (DeepProtoType) deep;
        deepProtoType.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone();
        return deepProtoType;
    }

    //深拷贝- 方式2 通过对象的序列化实现(推荐)
    public Object deepClone() {
        //创建流对象
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            //序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this); //当前这个对象以对象流的方式输出
            //反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            DeepProtoType copyObj = (DeepProtoType) ois.readObject();
            return copyObj;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            //关闭流
            try {
                bos.close();
                oos.close();
                bis.close();
                ois.close();
            } catch (Exception e2) {
                System.out.println(e2.getMessage());
            }
        }
    }
}

package com.dongguo.prototype.deepclone;

/**
 * @author Dongguo
 * @date 2021/8/22 0022-11:58
 * @description:
 */
public class Client {
    public static void main(String[] args) throws Exception {

        DeepProtoType p = new DeepProtoType();
        p.name = "张三";
        p.deepCloneableTarget = new DeepCloneableTarget("李四", "people");
        //方式1 重写clone()完成深拷贝
        DeepProtoType p2 = (DeepProtoType) p.clone();

        //方式2 序列化完成深拷贝
        DeepProtoType p3 = (DeepProtoType) p.deepClone();
        
        System.out.println( p + ",p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode());
        System.out.println("---------------------------");
        System.out.println(p2 + ",p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode());
        System.out.println("---------------------------");
        System.out.println(p3 + "p3.deepCloneableTarget=" + p3.deepCloneableTarget.hashCode());
    }
}
运行结果:
DeepProtoType{name='张三', deepCloneableTarget=DeepCloneableTarget{cloneName='李四', cloneClass='people'}},p.deepCloneableTarget=460141958
DeepProtoType{name='张三', deepCloneableTarget=DeepCloneableTarget{cloneName='李四', cloneClass='people'}},p2.deepCloneableTarget=1163157884
---------------------------
DeepProtoType{name='张三', deepCloneableTarget=DeepCloneableTarget{cloneName='李四', cloneClass='people'}}p3.deepCloneableTarget=2094548358
原型模式在spring中的使用

Spring 中原型bean 的创建,就是原型模式的应用

Spring中配置bean的时候,scope属性可以配置一个prototype值,该值指定该bean的创建是使用原型模式

在创建ioc容器后,通过getBean()获取bean对象时,往里追可以发现在核心方法处spring对bean的scope属性进行了判断,配置了prototype时,进入了原型模式的使用,

  1. 创建ioc的时候创建了一个这样的容器实例

public Object getBean(String name) throws BeansException {
    this.assertBeanFactoryActive();
    //我们先进入getBeanFactory()方法,看看得到的是哪个BeanFactory,再寻找他的getBean()方法
    return this.getBeanFactory().getBean(name);
}
 
//追踪发现这是一个抽象方法,应该是由AbstractApplicationContext的子类实现
public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;

2. 往下找getBeanFactory()方法的实现

3.发现在AbstractRefreshableApplicationContext中实现了这个方法,返回的是一个DefaultListableBeanFactory,也就是调用了DefaultListableBeanFactory的getBean()方法

private DefaultListableBeanFactory beanFactory;
//实现了getBeanFactory方法,确保了线程安全的前提下返回了一个DefaultListableBeanFactory
public final ConfigurableListableBeanFactory getBeanFactory() {
    synchronized(this.beanFactoryMonitor) {
        if (this.beanFactory == null) {
            throw new IllegalStateException("BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext");
        } else {
            return this.beanFactory;
        }
    }
}

4.去到DefaultListableBeanFactory中没有找到getBean()方法,于是往他的父类去找

 

5.从这个bean的父类的父类AbstractBeanFactory可以看到这个getBean(),同时调用了核心方法doGetBean();

public Object getBean(String name) throws BeansException {
    return this.doGetBean(name, (Class)null, (Object[])null, false);
}

6.进入到doGetBean()方法可以发现,spring对参数进行了判断,对应调用createBean创建了原型模式的对象

protected  T doGetBean(String name, @Nullable Class requiredType, @Nullable Object[] args, boolean typeCheckOnly) throws BeansException {
    String beanName = this.transformedBeanName(name);
    Object sharedInstance = this.getSingleton(beanName);
   			.................
 
            if (mbd.isSingleton()) {
                sharedInstance = this.getSingleton(beanName, () -> {
                    try {
                        return this.createBean(beanName, mbd, args);
                    } catch (BeansException var5) {
                        this.destroySingleton(beanName);
                        throw var5;
                    }
                });
                bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
            //判断了是否设置了原型模式
            } else if (mbd.isPrototype()) {
                var11 = null;
 
                Object prototypeInstance;
                try {
                    this.beforePrototypeCreation(beanName);
            	    //进入了原型模式的对象创建
                    prototypeInstance = this.createBean(beanName, mbd, args);
                } finally {
                    this.afterPrototypeCreation(beanName);
                }
 
                bean = this.getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
            } else {
                ...................
优点

​ 1、如果创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程,同时也能够提高效率。

​ 2、可以使用深克隆保持对象的状态。

​ 3、原型模式提供了简化的创建结构。

缺点

​ 1、在实现深克隆的时候可能需要比较复杂的代码。

​ 2、需要为每一个类配备一个克隆方法,而且这个克隆方法需要对类的功能进行通盘考虑,这对全新的类来说不是很难,但对已有的类进行改造时,不一定是件容易的事,必须修改其源代码,违背了“开闭原则”。

模式使用场景

​ 1、如果创建新对象成本较大,我们可以利用已有的对象进行复制来获得。

​ 2、如果系统要保存对象的状态,而对象的状态变化很小,或者对象本身占内存不大的时候,也可以使用原型模式配合备忘录模式来应用。相反,如果对象的状态变化很大,或者对象占用的内存很大,那么采用状态模式会比原型模式更好。

3、需要避免使用分层次的工厂类来创建分层次的对象,并且类的实例对象只有一个或很少的几个组合状态,通过复制原型对象得到新实例可能比使用构造函数创建一个新实例更加方便。

模式总结

​ 1、原型模式向客户隐藏了创建对象的复杂性。客户只需要知道要创建对象的类型,然后通过请求就可以获得和该对象一模一样的新对象,无须知道具体的创建过程。

​ 2、克隆分为浅克隆和深克隆两种。

​ 3、我们虽然可以利用原型模式来获得一个新对象,但有时对象的复制可能会相当的复杂,比如深克隆。

关注
打赏
1638062488
查看更多评论
立即登录/注册

微信扫码登录

0.0379s