为了账号安全,请及时绑定邮箱和手机立即绑定

【学习打卡】第4天 Java设计模式精讲-Debug方式+内存分析 第十三讲

【学习打卡】第4天 Java设计模式精讲-Debug方式+内存分析 第十三讲

课程名称:Java设计模式精讲-Debug方式+内存分析,真正学懂设计模式

课程章节: 享元模式讲解+Coding+源码解析

主讲老师:Geely

课程内容:

今天学习的内容包括:

什么是享元模式  享元模式  优点 缺点  Coding  源码解析 以及在业务上的应用

课程收获:

享元 设计模式

1.定义

    提供了减少对象数量从而改善应用所需的对象结构的方式

1.1 特点

  运用共享技术有效地支持大量细粒度的对象 一句话来说的话 就是减少创建对象的数量从而减少内存的占用

1.2 类型  : 结构型

2.适用场景

   1、常常应用于系统底层的开发 以便解决系统的性能问题。

   2、系统有大量相似对象、需要缓冲池的场景

3.缺点

  1.关注内/外部状态 关注线程安全问题

   2.使系统 程序的逻辑复杂化

4.优点

    1.减少对象的创建 降低内存中对象的数量,降低系统的内存,提高效率

    2.减少内存之外的其他资源占用

5.享元-扩展

 1.内部状态

 2.外部状态

6.享元-相关的设计模式

  享元模式和代理模式

  享元模式和单例模式

7.ulm 设计图

https://img1.sycdn.imooc.com//62ee1fb40001919c12721206.jpg

8.代码如下

package com.zw.design.pattern.creational.structural.flyweight;

public interface Empployee {
    void  report();
}
package com.zw.design.pattern.creational.structural.flyweight;

public class Manager implements Empployee {

    @Override
    public void report() {
        System.out.println(reportContent);
    }

    private String department;
    private String reportContent;


    public void setReportContent(String reportContent) {
        this.reportContent = reportContent;
    }

    public Manager(String department){
        this.department=department;
    }

}

2.工厂模式

package com.zw.design.pattern.creational.structural.flyweight;

import java.util.HashMap;
import java.util.Map;

public class EmpployeeFactory {

    private static final Map<String, Empployee> EMPPLOYEE_MAP = new HashMap<String, Empployee>();

    public static Empployee getManager(String department) {
        Manager manager = (Manager) EMPPLOYEE_MAP.get(department);
        if (manager == null) {
            manager = new Manager(department);
            System.out.println("manager部门经理 = " + department);
            String str=department+" 部门汇报:此次报告主要内容是。。。。。";
            manager.setReportContent(str);
            System.out.println("创建报告 = " + str);
            EMPPLOYEE_MAP.put(department, manager);
        }
        return manager;
    }
}

3.测试类

package com.zw.design.pattern.creational.structural.flyweight;

public class Test {
    private static  final  String departments[]={"RD","QA","PM","BA","DB"};
    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            String depart=departments[(int) (Math.random()*departments.length)];
            Manager manager = (Manager) EmpployeeFactory.getManager(depart);
            manager.report();
        }

    }
}

测试结果如下

https://img1.sycdn.imooc.com//62ee1fc20001f92807880824.jpg

享元设计模式jdk当中应用解析

在jdk 当中Integer 当中是用享元模式的代码如图

https://img1.sycdn.imooc.com//62ee1fcf00015dd318880358.jpg

在tomcat 当中GenericObjectPoolConfig 当中大量的使用享元设计模式

https://img1.sycdn.imooc.com//62ee1fdd000153bc22340804.jpg

今天学习课程共用了1小时,重新学习一下设计模式 更加清楚知道享元设计模式的应用以及如何在自己项目当中去使用它  大家一起加油 💪🏻





点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消