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

1.为什么要使用原型模式:
1.通过new产生一个对象需要非常繁琐的数据准备或访问权限,则可以使 用原型模式。
2.就是java中的克隆技术,以某个对象为原型,复制出新的对象。显然,新的对象具备原型对象的特点
3.优势有:效率高(直接克隆,避免了重新执行构造过程步骤) 。
4.克隆类似于new,但是不同于new。new创建新的对象属性采用的是默认值。克隆出的 对象的属性值完全和原型对象相同。并且克隆出的新对象改变不会影响原型对象。然后, 再修改克隆对象的值。

2.原型模式分为:
1.浅克隆
2.深克隆

3.实现浅克隆:
实现 Cloneable接口和重写clone方法

package com.example.shejimoshi.prototype;

import java.util.Date;

/**
 * 克隆模式
 */
public class Sheep implements Cloneable {

    private String name;
    private Date birthday;

    public Date getBirthday() {
        return birthday;
    }

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

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

    public Sheep( ) {

    }

    public String getName() {
        return name;
    }

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

    /**
     * 实现浅克隆
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object obj =super.clone();

        return obj;
    }
}

测试浅克隆:缺点是如果原对象有对象引用的话 那么克隆出来的对象和原对象共享一个对象 这样肯定是不行的 由此引出深克隆。

package com.example.shejimoshi.prototype;

import java.util.Date;

public class Test {


    public static void main(String[] args) throws Exception {
        Date date =new Date(1231213232L);
        Sheep sheep= new Sheep("123",date);
        System.out.println(sheep);
        System.out.println(  sheep.getBirthday());
            //修改data
        date.setTime(2432423423L);

            Sheep sheep1 =(Sheep) sheep.clone();
        System.out.println(sheep1);
        System.out.println(sheep1.getName());
        System.out.println(sheep1.getBirthday());
    }

执行结果:

com.example.shejimoshi.prototype.Sheep@23223dd8
Thu Jan 15 14:00:13 CST 1970
com.example.shejimoshi.prototype.Sheep@43a25848
123
Thu Jan 29 11:40:23 CST 1970








}

4.实现深克隆:
通过序列化和反序列化来实现深克隆
1.类实现Serializable接口
2.

package com.example.shejimoshi.prototype;

import com.example.shejimoshi.singleton.SingleTonDemo5;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * 实现深克隆
 */
public class Test1 {


    public static void main(String[] args) throws Exception {
        Date date =new Date(1231213232L);
        Sheep sheep= new Sheep("123",date);
        System.out.println(  sheep.getBirthday());
            //修改data

        System.out.println(date.getTime());

//把对象写入硬盘
        FileOutputStream fos =new FileOutputStream("d:/b.txt");
        ObjectOutputStream oos =new ObjectOutputStream(fos);
        oos.writeObject(sheep); //把sheep对象写入硬盘
        oos.close();
        fos.close();
        //从硬盘中读取
        ObjectInputStream ois =new ObjectInputStream(new FileInputStream("d:/b.txt"));
        Sheep sheep1 =(Sheep) ois.readObject();
        date.setTime(2432423423L);
        System.out.println(sheep1);
        System.out.println(sheep1.getName());
        System.out.println(sheep1.getBirthday());
    }






}

执行结果:

Thu Jan 15 14:00:13 CST 1970
1231213232
com.example.shejimoshi.prototype.Sheep@1324409e
123
Thu Jan 15 14:00:13 CST 1970
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值