设计模式-原型模式

设计模式-原型模式
原型模式分为:不通过构造函数的方式创建对象,也就是copy对象。
浅克隆和深克隆两种
原型模式的有点:
性能优良,Java自带的原型模式,是基于内存二进制流的拷贝,比直接new一个对象性能上提升了许多;
可以使用深克隆方式保存对象的状态,使用原型模式将对象复制一份并将其状态保存起来,简化了创建过程。
原型模式的缺点:
必须配备克隆方法;
当对已有类进行改造的时候,需要修改代码,违反了开闭原则。
一、通常写法:
克隆的接口

public interface IPrototype<T> {
    T clone();
}

实现克隆的接口,然后硬编码,重写clone方法

public class ConcretePrototype implements IPrototype {
    private String name;
    private int 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 "ConcretePrototype{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public ConcretePrototype clone() {
        ConcretePrototype prototype  =  new  ConcretePrototype();
        prototype.setAge(this.getAge());
        prototype.setName(this.getName());
        return prototype;
    }
}

测试类

public class Client {
    public static void main(String[] args) {

        ConcretePrototype prototype = new ConcretePrototype();
        prototype.setName("张三");
        prototype.setAge(18);

        System.out.println("原型对象: " + prototype);

        ConcretePrototype clone = prototype.clone();
        System.out.println("克隆对象: " + clone);
    }
}

运行结果

原型对象: ConcretePrototype{name='张三', age=18}
克隆对象: ConcretePrototype{name='张三', age=18}

二、实现Cloneable接口的浅克隆
只要实现Cloneable接口的都是浅克隆

public class ConcretePrototype implements Cloneable {
    private String name;
    private int age;
    private List<String> hobbs;

    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 List<String> getHobbs() {
        return hobbs;
    }

    public void setHobbs(List<String> hobbs) {
        this.hobbs = hobbs;
    }

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

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

三、深克隆

public class ConcretePrototype implements Cloneable,Serializable{
    private String name;
    private int age;
    private List<String> hobbs;

    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 List<String> getHobbs() {
        return hobbs;
    }

    public void setHobbs(List<String> hobbs) {
        this.hobbs = hobbs;
    }

    @Override
    public String toString() {
        return "ConcretePrototype{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", hobbs=" + hobbs +
                '}';
    }
    // 浅克隆
    @Override
    public ConcretePrototype clone() {
        try {
            return (ConcretePrototype) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
	// 深克隆
    public ConcretePrototype deepClone() {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            return (ConcretePrototype) ois.readObject();
        } catch (Exception e) {
            e.getMessage();
        }
        return null;
    }
    // 深克隆2
    public ConcretePrototype deepJsonClone() {
        JSONObject obj = (JSONObject) JSONObject.toJSON(this);
        ConcretePrototype prototype = JSONObject.toJavaObject(obj, ConcretePrototype.class);
        return prototype;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值