1原型模式-java

1、原型模式是在内存中二进制流的拷贝,要比直接new性能好很多。如果在一个循环体内创建大量对象,原型模式能体现出他的优点。
2、由于是二进制的拷贝,所有构造函数不会被执行。这也是优点,也是缺点,在实际应用中要考虑。

package com.mfz.prototype;

import java.io.Serializable;
import java.util.Date;

public class Sheep implements Cloneable, Serializable {

    private String sname;
    private Date birthday;

    public Sheep(String sname, Date birthday) {
        this.sname = sname;
        this.birthday = birthday;
    }

    public Sheep() {
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    /*
     * 只是对当前对象的一个clone
     * 浅clone
     * 浅拷贝
Object类提供的方法clone只是拷贝本对象,其对象内部的数组、引用对象等都不拷贝, 还是指向原生对象的内部元素地址, 这种拷贝是浅拷贝。
     * @see Cloneable
     */
    /*@Override
    protected Object clone() throws CloneNotSupportedException {
       Object obj = super.clone();
       return obj;
    }*/

    /*
     *  对对象中的clone(包括对象中的对象一起clone)
     *  对象内部的数组、引用对象等要手动拷贝
     * deep clone
     * @see Cloneable
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object obj = super.clone();
        Sheep  s = (Sheep) obj;
        s.birthday = (Date) s.getBirthday().clone();
        return obj;
    }
}



//测试代码
package com.mfz.prototype;

import java.util.Date;

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

        Date date = new Date();
        Sheep s1 = new Sheep("Tom",date);

        System.out.println("prototype: name:"+s1.getSname()+" ,date:"+s1.getBirthday());

        Sheep s2 = (Sheep) s1.clone();
        s2.setSname("Lucy");

        System.out.println("Clone: name:"+s2.getSname()+" ,date:"+s2.getBirthday());
        System.out.println("------------时间修改:---------------------");
        //date.setTime(123456789L);
        s1.getBirthday().setTime(123456789L);
        System.out.println("原型中的时间:"+s1.getBirthday());
        System.out.println("克隆中的时间:"+s2.getBirthday());
    }
}

//执行结果
prototype: name:Tom ,date:Sat Nov 07 11:30:53 CST 2020
Clone: name:Lucy ,date:Sat Nov 07 11:30:53 CST 2020
------------时间修改:---------------------
原型中的时间:Fri Jan 02 18:17:36 CST 1970
克隆中的时间:Sat Nov 07 11:30:53 CST 2020


//使用序列化与反序列化实现对象的deep clone
package com.mfz.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

public class Client2 {
    //使用序列化与反序列化实现对象的deep clone
    public static void main(String []args) throws Exception{

        Sheep s1 = new Sheep("Lucy",new Date());

        //序列化
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(s1);
        byte[] data = bos.toByteArray();

        //反序列化
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        ObjectInputStream bin = new ObjectInputStream(bis);

        Sheep s2 = (Sheep) bin.readObject();

        System.out.println("原型对象-> name:"+s1.getSname()+" 生日:"+s1.getBirthday());
        System.out.println("克隆对象-> name:"+s2.getSname()+" 生日:"+s2.getBirthday());
        System.out.println("修改原型对象中生日:");
        s1.getBirthday().setTime(12345678L);
        System.out.println("原型对象-> 生日:"+s1.getBirthday());
        System.out.println("克隆对象-> 生日:"+s2.getBirthday());

    }
}

//执行结果 
原型对象-> name:Lucy 生日:Sat Nov 07 11:33:19 CST 2020
克隆对象-> name:Lucy 生日:Sat Nov 07 11:33:19 CST 2020
修改原型对象中生日:
原型对象-> 生日:Thu Jan 01 11:25:45 CST 1970
克隆对象-> 生日:Sat Nov 07 11:33:19 CST 2020


//测试clone与new创建耗时
package com.mfz.prototype;


public class Client3 {

    //测试创建耗时

    //使用new方法创建1000个对象
    public static void TestNew(){
        long start = System.currentTimeMillis();
        for(int i = 0; i < 1000; i++){
           Lemon l = new Lemon();
        }
        long end = System.currentTimeMillis();
        System.out.println("new -> use total time: "+(end - start)+" millis");
    }

    //使用new方法创建1000个对象
    public static void TestClone() throws Exception{
        Lemon l = new Lemon();
        long start = System.currentTimeMillis();
        for(int i = 0; i < 1000; i++){
            Lemon n  = (Lemon) l.clone();
        }
        long end = System.currentTimeMillis();
        System.out.println("clone -> use total time: "+(end - start)+" millis");
    }

    public static void main(String[] args) throws Exception{
        TestNew();
        TestClone();
    }

}

//执行结果:
new -> use total time: 11173 millis
clone -> use total time: 0 millis


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值