java设计模式(四):Prototype (原型模式)

一、使用场景

  用克隆一个对象的方法去生成另一个对象。就比如一个类有很多属性,想创建多个对象,这些对象的大多数属性都相同,只有几个属性不同,可以先克隆,再设置不一样的值。

二、浅克隆

  复制基本数据类型,引用类型没有进行复制,Object的clone()方法默认的克隆形式。被克隆的对象必须实现Cloneable接口,否则会报CloneNotSupportedException异常。Cloneable为标记接口,告诉jdk实现该接口的类可以被克隆,无具体内容。

package prototype;

public class ConcretePrototypeShallow implements  Cloneable{

    private int id;

    private String next;

    @Override
    public ConcretePrototypeShallow clone() {
        ConcretePrototypeShallow concretePrototype = null;
        try {
            concretePrototype = (ConcretePrototypeShallow)super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return concretePrototype;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNext() {
        return next;
    }

    public void setNext(String next) {
        this.next = next;
    }
}
package prototype;

public class Client {

    public static void main(String[] args) {
        ConcretePrototypeShallow concretePrototypeA = new ConcretePrototypeShallow();
        concretePrototypeA.setId(1);
        concretePrototypeA.setNext("abc");
        ConcretePrototypeShallow concretePrototypeB = (ConcretePrototypeShallow)concretePrototypeA.clone();
        System.out.println(concretePrototypeA.getNext() == concretePrototypeB.getNext());
    }
}

打印为true,说明引用String类型next对象没有被复制,内存地址相同。

三、深克隆

  复制基本数据类型和引用类型。

1.引用对象直接new值方法:

package prototype;

public class ConcretePrototypeDeep implements  Cloneable{

    private int id;

    private String next;

    @Override
    public ConcretePrototypeDeep clone() {
        ConcretePrototypeDeep concretePrototype = null;
        try {
            concretePrototype = (ConcretePrototypeDeep)super.clone();
            concretePrototype.setNext(new String("abc"));
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return concretePrototype;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNext() {
        return next;
    }

    public void setNext(String next) {
        this.next = next;
    }
}
package prototype;

public class ClientDeep {

    public static void main(String[] args) {
        ConcretePrototypeDeep concretePrototypeA = new ConcretePrototypeDeep();
        concretePrototypeA.setId(1);
        concretePrototypeA.setNext("abc");
        ConcretePrototypeDeep concretePrototypeB = (ConcretePrototypeDeep)concretePrototypeA.clone();
        System.out.println(concretePrototypeA.getNext() == concretePrototypeB.getNext());
    }
}

此时的打印为false。

2.用fastjson包:

package prototype;

import com.alibaba.fastjson.JSONObject;

public class ConcretePrototypeDeepFastjson implements  Cloneable{

    private int id;

    private String next;

    @Override
    public ConcretePrototypeDeepFastjson clone() {
        ConcretePrototypeDeepFastjson concretePrototype = null;
        try {
            concretePrototype = (ConcretePrototypeDeepFastjson)super.clone();
            String nextClone = JSONObject.parseObject(JSONObject.toJSONBytes(concretePrototype.getNext()), String.class);
            concretePrototype.setNext(nextClone);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return concretePrototype;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNext() {
        return next;
    }

    public void setNext(String next) {
        this.next = next;
    }
}
package prototype;

public class ClientDeepfastjson {

    public static void main(String[] args) {
        ConcretePrototypeDeepFastjson concretePrototypeA = new ConcretePrototypeDeepFastjson();
        concretePrototypeA.setId(1);
        concretePrototypeA.setNext("abc");
        ConcretePrototypeDeepFastjson concretePrototypeB = (ConcretePrototypeDeepFastjson)concretePrototypeA.clone();
        System.out.println(concretePrototypeA.getNext() == concretePrototypeB.getNext());
    }
}

3.使用ObjectStream

package prototype;

import java.io.*;

public class ConcretePrototypeDeepStream implements  Cloneable, Serializable{

    private int id;

    private String next;

    @Override
    public ConcretePrototypeDeepStream clone() {
        ConcretePrototypeDeepStream concretePrototypeDeepStream = null;
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return (ConcretePrototypeDeepStream)objectInputStream.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return concretePrototypeDeepStream;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNext() {
        return next;
    }

    public void setNext(String next) {
        this.next = next;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值