设计模式(4、原型模式)

定义:拷贝原型创建新的对象;
对象克隆-程序更高的效率和扩展性;
对象调用Object的clone方法,必须实现Cloneable接口,默认是浅拷贝;

概念:
浅拷贝-值类型拷贝和引用拷贝,引用的具体对象不拷贝,Object的clone方法;
深拷贝-实现引用对象的拷贝、可以使用1.重写clone方法,引用属性逐个拷贝或  2.对象的序列化和反序列化实现(简单、常用);

UML类图:

代码实现:

/**
 * 原型模式(克隆模式)
 *
 */
class Address implements Serializable {
    public String city;
    public String country;

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", country='" + country + '\'' +
                '}';
    }
}

class User implements Cloneable, Serializable {
    //引用类型
    public String name;
    //引用对象
    public Address address;

    public User(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    /**
     * 浅拷贝:利用Object的clone方法克隆对象
     *
     * @return 拷贝的对象
     */
    protected User shallowClone() {
        User userClone = null;
        try {
            //调用Object的clone方法
            userClone = (User) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return userClone;
    }

    /**
     * 深拷贝:利用对象的序列化与反序列化
     *
     * @return
     */
    protected User deepClone() {
        User userClone = null;
        // 将对象写到流里
        ByteArrayOutputStream bo = null;
        ObjectOutputStream oo = null;
        // 从流里读出来
        ByteArrayInputStream bi = null;
        ObjectInputStream oi = null;
        try {
            // 将对象写到流里
            bo = new ByteArrayOutputStream();
            oo = new ObjectOutputStream(bo);
            oo.writeObject(this);
            // 从流里读出来
            bi = new ByteArrayInputStream(bo.toByteArray());
            oi = new ObjectInputStream(bi);
            userClone = (User) oi.readObject();
        } catch (IOException | ClassNotFoundException e1) {
            e1.printStackTrace();
        } finally {
            //关闭资源
            if (oi != null) {
                try {
                    oi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bi != null) {
                try {
                    bi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (oo != null) {
                try {
                    oo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bo != null) {
                try {
                    bo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return userClone;
    }

    /**
     * 重写Object的clone方法
     *
     * @return
     */
    @Override
    protected User clone() {
        //深拷贝
//        return deepClone();
        //浅拷贝
        return shallowClone();
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", address=" + address +
                '}';
    }
}

/**
 * 对象拷贝测试
 */
public class MyClone {
    public static void main(String[] args) {
        User user = new User("张三", new Address());
        //对象克隆
        User user2 = user.clone();
        //验证深拷贝/浅拷贝
        user2.address.city = "深圳";
        System.out.println(user);
        System.out.println(user2);
        System.out.println(user == user2);
        System.out.println(user.name == user2.name);
        System.out.println(user.address == user2.address);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小绿豆

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值