• 每天一个设计模式之享元模式(Flyweight Pattern)


    享元是共享对象的意思,用来解决重复对象的内存浪费的问题。缓冲池就是为了共享对象而出现的一种技术,常见的使用池技术的场景有String常量池、数据库连接池、缓冲池等。

    一、UML类图

    在这里插入图片描述

    角色说明:

    1. FlyWeight,是抽象的享元角色,它是产品抽象类,同时定义出对象的外部状态和内部状态的接口或实现类;
    2. ConcreteFlyweight,是具体的享元角色,是具体的产品类,实现抽象角色定义相关业务;
    3. UnsharedConcreteFlyweight,是不可共享的角色,一般不会出现在享元工厂;存在的意义是为了不强制享元对象一定共享;
    4. FlyweightFactory,享元工厂类,用于构建一个池容器(集合),同时提供从池中获取对象的方法;

    内部状态

    • 指对象共享出来的信息,存储在享元对象内部且不会随环境的改变而改变;

    外部状态

    • 指对象得以依赖的一个标记 ,是随环境改变而改变的,不可共享的状态;

    二、记忆方法

    『享』共享,『元』对象;
    典型的共享技术——对象池,典型的对象池——String常量池,数据库连接池;

    三、代码示例

    下面是享元接口和享元类(共享)

    // 享元接口
    public interface Website {
        void use(User user);
    }
    
    // 享元具体类
    public class ConcreteWebsite implements Website {
        private String type;
    
        public ConcreteWebsite(String type) {
            this.type = type;
        }
    
        @Override
        public void use(User user) {
            System.out.println("网站的发布形式: " + type + ", 在使用中");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    下面是享元对象工厂

    public class WebsiteFactory {
        private Map<String, Website> sitePool = new HashMap<>();
        // 根据网站的类型返回一个网站,如果没有就创建一个网站,并放到池中并返回
        public Website getWebsiteByType(String type) {
            if (!sitePool.containsKey(type))
                sitePool.put(type, new ConcreteWebsite(type));
            return sitePool.get(type);
        }
    
        // 获取网站类型总数
        public int getWebsiteCount() {
            return sitePool.size();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    下面是客户类

    public class Client {
        public static void main(String[] args) {
            WebsiteFactory websiteFactory = new WebsiteFactory();
            Website site1 = websiteFactory.getWebsiteByType("新闻");
            site1.use(new User("Tom"));
    
            Website site2 = websiteFactory.getWebsiteByType("财经");
            site2.use(new User("Lucy"));
    
            Website site3 = websiteFactory.getWebsiteByType("财经");
            site3.use(new User("John"));
    
            Website site4 = websiteFactory.getWebsiteByType("财经");
            site4.use(new User("Baden"));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    运行结果如下

    website release: news, in using
    website release: finance, in using
    website release: finance, in using
    website release: finance, in using
    All category in websites sum up to => 2 kinds
    
    Process finished with exit code 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    【参考】

    1. https://www.runoob.com/design-pattern/flyweight-pattern.html
    2. 韩顺平《设计模式》
  • 相关阅读:
    力扣打卡----打家劫舍
    基于JAVA新生报到管理系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    基于Android家校互动系统 java家校通
    有朋友用过优码网吗?
    Go语言开发小技巧&易错点100例(十三)
    MHDNet
    <Xcode> Xcode IOS无开发者账号打包和分发
    LeetCode 每日一题 2023/8/28-2023/9/3
    云计算与ai人工智能对高防cdn的发展
    Go-知识map
  • 原文地址:https://blog.csdn.net/M_sdn/article/details/126294937