孙悟空的身外身法术使用了Java设计模式:原型模式

定义

原型模式属于对象的创建型模式,通过给出一个原型对象来指明所要创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象,这就是原型模式的用意

java语言的构建模型直接支持原型模式,所有的JavaBean都继承自java.lang.Object,而且Object类提供了一个clone()方法,可以将一个JavaBean对象复制一份

但是这个JavaBean必须实现一个标识接口Cloneable,表明这个JavaBean支持复制。
如果一个对象没有实现这个接口却使用了clone()方法,Java编译器会抛出异常:CloneNotSupportedException

意图

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象

主要解决问题

在运行期建立和删除原型

何时使用

如果一个系统的产品类是动态加载的,而且产品类具有一定的等级结构,这时可以采用原型模式

优缺点

优点:

  • 允许动态的增加或者减少产品类
  • 提供简化的创建结构
  • 具有给一个应用软件动态加载新功能的能力

缺点:

  • 每一个类都必须匹配一个克隆方法
  • 必须实现 Cloneable 接口

结构

原型模式有两种表现形式:简单形式和登记形式

简单形式的原型模式

在这里插入图片描述
涉及的角色:

  • 客户(Client)角色:客户类提出创建对象的请求
  • 抽象原型(Prototype)角色:通常由一个Java接口或者Java抽象类实现,此角色给出所有的具体原型类所需要的接口
  • 具体原型(ConcretePrototype)角色:被复制的对象,此角色需要实现抽象原型角色所要求的接口

源码如下:

public class Client {
    
    

    private Prototype prototype;

    public void operation(Prototype example) {
    
    
        Prototype p = (Prototype) example.clone();
    }
}

抽象原型角色声明了一个clone方法:

public interface Prototype extends Cloneable {
    
    

    Object clone();
}
public class ConcretePrototype implements Prototype {
    
    
    /** 克隆方法 */
    @Override
    public Object clone() {
    
    
        try {
    
    
            return super.clone();
        } catch (CloneNotSupportedException e) {
    
    
            return null;
        }
    }
}

登记形式的原型模式

在这里插入图片描述
涉及的角色:

  • 客户端(Client)角色:客户端向管理员提出创建对象的请求
  • 抽象原型(Prototype)角色:通常由一个Java接口或者Java抽象类实现,此角色给出所有的具体原型类所需要的接口
  • 具体原型(ConcretePrototype)角色:被复制的对象,此角色需要实现抽象原型角色所要求的接口
  • 原型管理器(PrototypeManager)角色:创建具体原型类的对象,并记录每一个被创建的对象

源码如下:

public interface Prototype extends Cloneable {
    
    

    Object clone();
}
public class ConcretePrototype implements Prototype {
    
    
    /** 克隆方法 */
    @Override
    public synchronized Object clone() {
    
    
        Prototype temp = null;
        try {
    
    
            temp = (Prototype) super.clone();
            return temp;
        } catch (CloneNotSupportedException e) {
    
    
            System.out.println("克隆失败!");
        } finally {
    
    
            return temp;
        }
    }
}
public class PrototypeManager {
    
    

    private Vector vector = new Vector();

    /** 增加一个新的对象 */
    public void add(Prototype prototype) {
    
    
        vector.add(prototype);
    }

    /** 取出聚集中的一个对象 */
    public Prototype get(int index) {
    
    
        return (Prototype) vector.get(index);
    }

    /** 给出聚集的大小 */
    public int getSize() {
    
    
        return vector.size();
    }
}
public class Client {
    
    
    private PrototypeManager manager;
    private Prototype prototype;

    public void registerPrototype() {
    
    
        prototype = new ConcretePrototype();
        Prototype cloneType = (Prototype) prototype.clone();
        manager.add(cloneType);
    }
}

两种形式比较

如果需要创建的原型对象数目比较少而且比较固定的话,可以采用简单形式,原型对象的引用由客户端保存

如果要创建的原型对象数目不固定的话,可以采用登记形式,在这种情况下,客户端不保存对原型对象的引用,这个任务由管理员对象来完成
在复制一个原型对象之前,客户端可以查看管理员对象中是否已经有一个满足要求的原型对象,如果有可以直接取出引用,如果没有客户端就需要自行复制此原型对象

浅克隆和深克隆

浅克隆:
被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象,换而言之,浅克隆仅仅复制所考虑的对象,而不复制它所引用的对象

深克隆:
被复制对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量;那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换而言之,深克隆把要复制的对象所引用的对象都复制一遍,而这种对被引用到的对象的复制叫做间接复制

孙悟空的身外身法术

孙悟空一根毫毛可以变幻出许多的大圣,由此场景熟悉原型模式

浅克隆实现

孙悟空本人:

public class TheGreatestSage {
    
    
    private Monkey monkey = new Monkey();

    public void change() {
    
    
        //创建大圣本尊对象
        Monkey copyMonkey;
        for (int i=0; i<2000; i++) {
    
    
            //克隆大圣本尊
            copyMonkey = (Monkey) monkey.clone();
            System.out.println("大圣本尊的生日:" + monkey.getBirthDate());
            System.out.println("变化出来的大圣的生日:" + copyMonkey.getBirthDate());
            System.out.println("大圣本尊== 变化出来的大圣:" + (monkey == copyMonkey));
            System.out.println("大圣本尊的金箍棒== 变化出来的大圣的金箍棒:"
                    + (monkey.getStaff() == copyMonkey.getStaff()));
        }
    }

    public static void main(String[] args) {
    
    
        TheGreatestSage sage = new TheGreatestSage();
        sage.change();
    }
}

具体原型角色:

public class Monkey implements Cloneable {
    
    
    /** 身高 */
    private float height;

    /** 体重 */
    private float weight;

    /** 金箍棒 */
    private GoldRingedStaff staff;

    /** 日期 */
    private Date birthDate;

    public Monkey() {
    
    
        this.birthDate = new Date();
    }

    /** 克隆方法 */
    @Override
    public Object clone() {
    
    
        Monkey temp = null;
        try {
    
    
            temp = (Monkey) super.clone();
        } catch (CloneNotSupportedException e) {
    
    
            System.out.println("克隆失败!");
            e.printStackTrace();
        } finally {
    
    
            return temp;
        }
    }

    public float getHeight() {
    
    
        return height;
    }

    public void setHeight(float height) {
    
    
        this.height = height;
    }

    public float getWeight() {
    
    
        return weight;
    }

    public void setWeight(float weight) {
    
    
        this.weight = weight;
    }

    public Date getBirthDate() {
    
    
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
    
    
        this.birthDate = birthDate;
    }

    public GoldRingedStaff getStaff() {
    
    
        return staff;
    }

    public void setStaff(GoldRingedStaff staff) {
    
    
        this.staff = staff;
    }
}

大圣拥有的金箍棒:

public class GoldRingedStaff {
    
    

    /** 高度 */
    private float height = 100.0f;

    /** 半径 */
    private float radius = 5.0f;

    /** 构造函数 */
    public GoldRingedStaff() {
    
    
    }

    /** 增长行为,每次调用高度和半径增加一倍 */
    public void grow() {
    
    
        this.height = this.height * 2;
        this.radius = this.radius * 2;
    }

    /** 缩小行为,每次调用高度和半径减少一半 */
    public void shrink() {
    
    
        this.height = this.height / 2;
        this.radius = this.radius / 2;
    }

    /** 移动 */
    public void move() {
    
    

    }

    /** 高度取值方法 */
    public float getHeight() {
    
    
        return height;
    }

    /** 高度赋值方法 */
    public void setHeight(float height) {
    
    
        this.height = height;
    }

    /** 半径取值方法 */
    public float getRadius() {
    
    
        return radius;
    }

    /** 半径赋值方法 */
    public void setRadius(float radius) {
    
    
        this.radius = radius;
    }
}

在这里插入图片描述
从输出结果可以看到,复制出来的大圣和原来的大圣拥有的金箍棒是一根,而不是每一个复制出来的大圣都有一根
下面使用深克隆改进上面的代码

深克隆实现

大圣本人:

public class TheGreatestSage {
    
    
    private Monkey monkey = new Monkey();

    public void change() throws IOException, ClassNotFoundException {
    
    
        //创建大圣本尊对象
        Monkey copyMonkey;
        for (int i=0; i<2000; i++) {
    
    
            copyMonkey = (Monkey) monkey.deepClone();
            System.out.println("大圣本尊的生日:" + monkey.getBirthDate());
            System.out.println("变化出来的大圣的生日:" + copyMonkey.getBirthDate());
            System.out.println("大圣本尊== 变化出来的大圣:" + (monkey == copyMonkey));
            System.out.println("大圣本尊的金箍棒== 变化出来的大圣的金箍棒:"
                    + (monkey.getStaff() == copyMonkey.getStaff()));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
    
    
        TheGreatestSage sage = new TheGreatestSage();
        sage.change();
    }
}

在Monkey类中增加深克隆方法:

public class Monkey implements Cloneable, Serializable {
    
    
    /** 身高 */
    private float height;

    /** 体重 */
    private float weight;

    /** 金箍棒 */
    private GoldRingedStaff staff;

    /** 日期 */
    private Date birthDate;

    public Monkey() {
    
    
        this.birthDate = new Date();
        this.staff = new GoldRingedStaff();
    }

    /** 深拷贝方法 */
    public Object deepClone() throws IOException, ClassNotFoundException {
    
    
        //首先将对象写出到流
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(this);
        //然后将对象从流里读出来
        ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
        ObjectInputStream oi = new ObjectInputStream(bi);
        return oi.readObject();
    }

    /** 浅拷贝方法 */
    @Override
    public Object clone() {
    
    
        Monkey temp = null;
        try {
    
    
            temp = (Monkey) super.clone();
        } catch (CloneNotSupportedException e) {
    
    
            System.out.println("克隆失败!");
            e.printStackTrace();
        } finally {
    
    
            return temp;
        }
    }

    public float getHeight() {
    
    
        return height;
    }

    public void setHeight(float height) {
    
    
        this.height = height;
    }

    public float getWeight() {
    
    
        return weight;
    }

    public void setWeight(float weight) {
    
    
        this.weight = weight;
    }

    public Date getBirthDate() {
    
    
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
    
    
        this.birthDate = birthDate;
    }

    public GoldRingedStaff getStaff() {
    
    
        return staff;
    }

    public void setStaff(GoldRingedStaff staff) {
    
    
        this.staff = staff;
    }
}

金箍棒类:

public class GoldRingedStaff implements Cloneable, Serializable {
    
    

    /** 高度 */
    private float height = 100.0f;

    /** 半径 */
    private float radius = 5.0f;

    /** 构造函数 */
    public GoldRingedStaff() {
    
    
    }

    /** 增长行为,每次调用高度和半径增加一倍 */
    public void grow() {
    
    
        this.height = this.height * 2;
        this.radius = this.radius * 2;
    }

    /** 缩小行为,每次调用高度和半径减少一半 */
    public void shrink() {
    
    
        this.height = this.height / 2;
        this.radius = this.radius / 2;
    }

    /** 移动 */
    public void move() {
    
    

    }

    /** 高度取值方法 */
    public float getHeight() {
    
    
        return height;
    }

    /** 高度赋值方法 */
    public void setHeight(float height) {
    
    
        this.height = height;
    }

    /** 半径取值方法 */
    public float getRadius() {
    
    
        return radius;
    }

    /** 半径赋值方法 */
    public void setRadius(float radius) {
    
    
        this.radius = radius;
    }
}

在这里插入图片描述
从输出中可以清楚的看到,大圣本人拥有的金箍棒和复制出来的金箍棒不是同一根了

おすすめ

転載: blog.csdn.net/qq_34365173/article/details/108330587