欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

创建型设计模式 -- 原型模式

程序员文章站 2022-06-16 23:09:54
一、小案例分析 1、功能需求: 现有一个员工,姓名为Rick,年龄22,ID为193211,如何创建10个完全相同的对象。 2、小菜鸡的答案: (1)直接new 10个对象就完了。(2)代码实现: (3)代码分析: 容易理解,好操作。但是每次都是初始化对象,而不是动态获得对象,不够灵活,效率低。 二 ......

一、小案例分析

1、功能需求:

  现有一个员工,姓名为rick,年龄22,id为193211,如何创建10个完全相同的对象。

2、小菜鸡的答案:

(1)直接new 10个对象就完了。
(2)代码实现:

package prototype.pattern;

public class demo {
    public static void main(string[] args) {
        employee employee = new employee("tom", 22, 193211);
        employee employee1 = new employee("tom", 22, 193211);
        employee employee2 = new employee("tom", 22, 193211);
        employee employee3 = new employee("tom", 22, 193211);
        employee employee4 = new employee("tom", 22, 193211);
        employee employee5 = new employee("tom", 22, 193211);
        employee employee6 = new employee("tom", 22, 193211);
        employee employee7 = new employee("tom", 22, 193211);
        employee employee8 = new employee("tom", 22, 193211);
        employee employee9 = new employee("tom", 22, 193211);
        system.out.println(employee);
        system.out.println(employee1);
        system.out.println(employee2);
        system.out.println(employee3);
        system.out.println(employee4);
        system.out.println(employee5);
        system.out.println(employee6);
        system.out.println(employee7);
        system.out.println(employee8);
        system.out.println(employee9);
    }
}

class employee {
    private string name;
    private int age;
    private long id;

    public employee(string name, int age, long id) {
        super();
        this.name = name;
        this.age = age;
        this.id = id;
    }

    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 long getid() {
        return id;
    }

    public void setid(long id) {
        id = id;
    }

    @override
    public string tostring() {
        return "employee [name=" + name + ", age=" + age + ", id=" + id + "]";
    }
}

(3)代码分析:
  容易理解,好操作。但是每次都是初始化对象,而不是动态获得对象,不够灵活,效率低。

二、原型模式

1、什么是原型模式

  指的是用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象。是一种创建模式,允许一个对象去创建另一个可定制的对象且不用知道如何创建的细节。

2、如何实现(影分身之术)

  将一个原型对象传给 发动创建的对象,此对象通过请求原型对象拷贝自身来进行创建,即调用clone方法。

3、clone方法、浅拷贝、深拷贝

  可以参照如下地址:
    https://www.cnblogs.com/l-y-h/p/10906499.html#_label1
注:
  上面链接中只讲述了通过clone方法实现深拷贝(所有涉及到的类均要实现cloneable接口)。
  也可以使用序列化的方式实现深拷贝(推荐,所有涉及到的类均要实现serializable接口)。

package prototype.pattern;

import java.io.bytearrayinputstream;
import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.io.objectinputstream;
import java.io.objectoutputstream;
import java.io.serializable;

public class demo {
    public static void main(string[] args) {
        employee employee = new employee("tom", 22, 193211);
        employee employee2 = (employee) employee.deepclone();
        system.out.println(employee);
        system.out.println(employee2);
    }
}

// 此处若不实现serializable 接口,会抛出java.lang.nullpointerexception异常。
class person implements serializable {
    private string name;
    private int age;

    public person(string name, int age) {
        super();
        this.name = name;
        this.age = 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;
    }

    @override
    public string tostring() {
        return "person [name=" + name + ", age=" + age + "]";
    }
}

class employee implements serializable {
    private string name;
    private int age;
    private long id;
    private person people = new person("jarry", 20);

    public employee(string name, int age, long id) {
        super();
        this.name = name;
        this.age = age;
        this.id = id;
    }

    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 long getid() {
        return id;
    }

    public void setid(long id) {
        id = id;
    }

    @override
    public string tostring() {
        return "employee [name=" + name + ", age=" + age + ", id=" + id + "]";
    }

    /**
     * 使用序列化的方式实现深拷贝
     * 
     * @return
     */
    public object deepclone() {
        bytearrayoutputstream baos = null;
        objectoutputstream oos = null;
        bytearrayinputstream bias = null;
        objectinputstream ois = null;
        try {
            // 序列化
            baos = new bytearrayoutputstream();
            oos = new objectoutputstream(baos);
            oos.writeobject(this); // 将当前对象以对象流的方式输出

            // 反序列化
            bias = new bytearrayinputstream(baos.tobytearray());
            ois = new objectinputstream(bias);
            employee employee = (employee) ois.readobject();
            return employee;
        } catch (exception e) {
            system.out.println(e.getmessage());
            return null;
        } finally {
            try {
                baos.close();
                oos.close();
                bias.close();
                ois.close();
            } catch (ioexception e) {
                system.out.println(e.getmessage());
            }
        }
    }
}

4、使用原型模式实现上例

(1)使原型类实现 cloneable 接口(只有实现该接口,才能重写clone方法)。
(2)调用原型对象自身的clone方法实现拷贝。
(3)代码实现:

package prototype.pattern;

public class demo {
    public static void main(string[] args) {
        employee employee = new employee("tom", 22, 193211);
        employee employee1 = (employee) employee.clone();
        employee employee2 = (employee) employee.clone();
        employee employee3 = (employee) employee.clone();
        employee employee4 = (employee) employee.clone();
        employee employee5 = (employee) employee.clone();
        employee employee6 = (employee) employee.clone();
        employee employee7 = (employee) employee.clone();
        employee employee8 = (employee) employee.clone();
        employee employee9 = (employee) employee.clone();
        system.out.println(employee);
        system.out.println(employee1);
        system.out.println(employee2);
        system.out.println(employee3);
        system.out.println(employee4);
        system.out.println(employee5);
        system.out.println(employee6);
        system.out.println(employee7);
        system.out.println(employee8);
        system.out.println(employee9);
    }
}

class employee implements cloneable {
    private string name;
    private int age;
    private long id;

    public employee(string name, int age, long id) {
        super();
        this.name = name;
        this.age = age;
        this.id = id;
    }

    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 long getid() {
        return id;
    }

    public void setid(long id) {
        id = id;
    }

    @override
    public string tostring() {
        return "employee [name=" + name + ", age=" + age + ", id=" + id + "]";
    }

    @override
    protected object clone() {
        employee employee = null;
        try {
            employee = (employee) super.clone();
        } catch (clonenotsupportedexception e) {
            system.out.println(e.getmessage());
        }
        return employee;
    }
}

(3)代码分析:
  让程序有更好的扩展性与效率,当修改某个原型对象时,其余clone后的对象均会被修改。
(4)uml图:

创建型设计模式 -- 原型模式

 

5、使用原型模式的优缺点

(1)优点:当创建新的对象比较复杂时,使用原型模式可以简化创建流程、且提高创建的效率。不需要重新new对象,而是动态的获取运行中的对象。
(2)缺点:当实现深拷贝的clone方法时,需要给所有涉及到的类加个clone方法,可能会违反开闭原则。

 

三、spring中原型模式分析

1、bean的创建

  在创建bean的时候,可以选择是单例模式(同一对象) 还是原型模式(不同对象)。

2、部分源码:(稍后整理)