常见八种设计模式总结 (一直更新补充中)

我已经上传到gitee上了,里面有注释,每一种我都写了代码,写了注释,基本上很容易理解。

链接:

https://gitee.com/hhp123/design-mode.git

我写的8种设计模式分别是:

比如原型模式的深拷贝:

package 原型模式.深拷贝.字节流对象流;

import java.io.*;

public class Sheep implements Cloneable {
    String name;
    Sheep friends;  // 深拷贝:也为引用类型分配存储空间

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

    @Override
    protected Object clone() {
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(this);
            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            ObjectInputStream oi = new ObjectInputStream(bi);
            Object clone = oi.readObject();
            return clone;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        Sheep sheep1 = new Sheep("第一只羊");
        Sheep sheep2 = new Sheep("第二只羊");
        sheep1.friends = sheep2;

        System.out.println(sheep1.friends == sheep2);// true

    }
}

其他模式在gitee看吧~

对你有帮助不忘记点赞,收藏,后面会继续更新!~

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值