原型模式

一.原型模式

原型模式,即克隆模式,主要采用浅克隆和深克隆,两种模式进行,省去创建对象的初始化时间和代码

1.浅克隆

新建一苹果类,有名称以及颜色属性

public class Apple {

    public Apple(String color, String name) {
        this.color = color;
        this.name = name;
    }

    private String color ;

    private String name;

}

再新建一苹果订单类,有苹果引用以及数量属性--实现jdk中Cloneable接口,重写clone方法

// 实现Cloneable接口
public class AppleOrder implements Cloneable {

    public AppleOrder(Apple apple, Integer num) {
        this.apple = apple;
        this.num = num;
    }

    private Apple apple;

    private Integer num;

    // 重写clone方法
    @Override
    protected AppleOrder clone() throws CloneNotSupportedException {
        AppleOrder appleOrder = (AppleOrder)super.clone();
        return appleOrder;
    }

    public Apple getApple() {
        return apple;
    }

    public void setApple(Apple apple) {
        this.apple = apple;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}

再建一测试类,进行测试

public class Test {
    public static void main(String[] args) throws Exception{
        Apple apple = new Apple("red","apple");
        AppleOrder appleOrder1 = new AppleOrder(apple,20);
        AppleOrder appleOrder2 = appleOrder1.clone();
        System.out.println(appleOrder1);
        System.out.println(appleOrder2);
        System.out.println(appleOrder1.getApple());
        System.out.println(appleOrder2.getApple());
    }
}

原型模式_第1张图片

浅克隆:1.对于基础类型进行克隆,

            2.对于引用类型,仅拷贝应用地址

所以浅克隆后的对象中的,成员变量为引用类型的,都指向同一个对象

2.深度克隆

新建一苹果类,有名称以及颜色属性--苹果类也实现Cloneable接口,重写clone方法

public class Apple implements Cloneable{

    public Apple(String color, String name) {
        this.color = color;
        this.name = name;
    }

    private String color ;

    private String name;

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

再新建一苹果订单类,有苹果引用以及数量属性--实现jdk中Cloneable接口,重写clone方法,clone方法中,将引用苹果进行克隆

// 实现Cloneable接口
public class AppleOrder implements Cloneable {

    public AppleOrder(Apple apple, Integer num) {
        this.apple = apple;
        this.num = num;
    }

    private Apple apple;

    private Integer num;

    // 重写clone方法
    @Override
    protected AppleOrder clone() throws CloneNotSupportedException {
        AppleOrder appleOrder = (AppleOrder)super.clone();
        if(apple!=null){
            appleOrder.setApple(apple.clone());
        }
        return appleOrder;
    }

    public Apple getApple() {
        return apple;
    }

    public void setApple(Apple apple) {
        this.apple = apple;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}

再建一测试类,进行测试

public class Test {
    public static void main(String[] args) throws Exception{
        Apple apple = new Apple("red","apple");
        AppleOrder appleOrder1 = new AppleOrder(apple,20);
        AppleOrder appleOrder2 = appleOrder1.clone();
        System.out.println(appleOrder1);
        System.out.println(appleOrder2);
        System.out.println(appleOrder1.getApple());
        System.out.println(appleOrder2.getApple());
    }
}

原型模式_第2张图片

深克隆,即将被克隆对象中成员变量为应用类型的属性,也进行克隆,形成不同对象

3.利用反射机制实现简单克隆方法

新建一苹果类,有名称以及颜色属性--创建无参构造

public class Apple {

    public Apple(String color, String name) {
        this.color = color;
        this.name = name;
    }

    public Apple() {
    }

    private String color ;

    private String name;

    public String getColor() {
        return color;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 新建一测试类,包含有copy方法

public class Test {
    public static void main(String[] args) throws Exception{
        Apple apple1 = new Apple("red","apple");
        Apple apple2 = copy(apple1);
        System.out.println(apple1);
        System.out.println(apple1.getColor()+":"+apple1.getName());
        System.out.println(apple2);
        System.out.println(apple2.getColor()+":"+apple2.getName());
    }

    public static  T copy(Object object)throws Exception {
        Class objectClass = object.getClass();
        // 通过无参构造函数实例化一个对象
        Object objectCopy = objectClass.getConstructor().newInstance();
        // 获得对象的所有成员变量
        Field[] fields = objectClass.getDeclaredFields();
        for (Field field : fields) {
            // 获取成员变量的名字
            String name = field.getName();
            // 首字母大写
            String initialFiledName = name.substring(0, 1).toUpperCase();
            // 拼接成 getName
            String getMethodName = "get" + initialFiledName + name.substring(1);
            // 拼接成 setName
            String setMethodName = "set" + initialFiledName + name.substring(1);
            // 获取get()方法对象
            Method getMethod = objectClass.getMethod(getMethodName);
            // 获取set()方法对象
            Method setMethod = objectClass.getMethod(setMethodName, new Class[]{field.getType()});
            // 调用get方法获取旧的对象的值
            Object value = getMethod.invoke(object);
            // 调用set方法将这个值复制到新的对象中去
            setMethod.invoke(objectCopy, new Object[]{value});
        }
        return (T)objectCopy;
    }
}

测试结果

原型模式_第3张图片

通过copy方法,生成了新的类,也拷贝了属性

==================================================================================

原型模式的本质:即当我们需要创建多个复杂对象时,我们仅需要创建一个复杂对象(原型),再对其进行copy,这样我们就不需要进行复杂对象的初始化,从而得到多个新的复杂对象. 

你可能感兴趣的:(设计模式)