• 享元模式【结构模式】


    享元模式

    Use sharing to support large numbers of fine-grained objects efficiently.
    使用共享来支持大量细粒度对象
    
    public class FlyWeight {
        /**
         * 享元模式:
         * Use sharing to support large numbers of fine-grained objects efficiently.
         * 使用共享来支持大量细粒度对象
         */
        @Test
        public void all() {
            final String[] cities = { "杭州", "上海", "北京", "苏州" };
            final String[] depts = { "人事部", "研发部", "财务部", "产品部" };
    
            final ThreadLocalRandom current = ThreadLocalRandom.current();
    
            IntStream.rangeClosed(0, 29).forEach(order -> {
                final String city = cities[current.nextInt(cities.length)];
                final String dept = depts[current.nextInt(depts.length)];
                final Employe employe = Employe.builder().name("zxd" + order).location(LocaltionPool.get(city, dept)).build();
                employe.show();
            });
        }
    }
    
    /**
     * 1)提取出不可变部分,该部分可实现共享。
     */
    @Data
    @Builder
    class Location {
        private final String city;
        private final String dept;
    }
    
    /**
     * 2)包含可变部分和不可变部分的实例对象
     */
    @Data
    @Builder
    @Slf4j
    class Employe {
        private String name;
        private Location location;
    
        public void show() {
            log.info("{} {} {} {}", name, "入职了", location.getCity(), location.getDept());
        }
    }
    /**
     * 3)基于不可变部分构建共享池
     */
    @Slf4j
    class LocaltionPool {
        private static final String KEY_SEP = "-";
        /**
         * 内置的城市和部门
         */
        private static final String[] CITIES = { "杭州", "上海", "北京" };
        private static final String[] DEPTS = { "人事部", "研发部", "财务部", "产品部" };
        /**
         * 基于 ConcurrentHashMap 构建的内存缓存池
         */
        private static final ConcurrentMap<String, Location> locations;
        static {
            /**
             * 初始化内置的城市和部门
             */
            locations = new ConcurrentHashMap<>();
            Arrays.stream(CITIES).forEach(city -> {
                Arrays.stream(DEPTS).forEach(dept -> {
                    locations.put(new StringJoiner(KEY_SEP).add(city).add(dept).toString(), Location.builder().city(city).dept(dept).build());
                });
            });
        }
    
        public static final Location get(String city, String dept) {
            final String key = String.join(KEY_SEP, city, dept);
            final Location location = locations.get(key);
            if (null != location) {
                log.info("从缓存池中获取");
                return location;
            }
    
            final Location build = Location.builder().city(city).dept(dept).build();
            locations.put(key, build);
            log.info("对象加入缓存池", build);
            return build;
        }
    }
    
  • 相关阅读:
    关于Netty4.x中文教程系列更新进度的说明和道歉
    Netty4.x中文教程系列(四) ChannelHandler
    Netty4.x中文教程系列(三) Hello World !详解
    Netty4.x中文教程系列(二) Hello World !
    Netty4.x中文教程系列(一) 目录及概述
    【推荐】HTML5 UI框架 推荐
    【转载】【JQuery学习】jQuery插件开发
    前端与后端的数据交互(jquery ajax+python flask)
    【JS教程32】ajax
    【JS教程31】json
  • 原文地址:https://www.cnblogs.com/zhuxudong/p/10164228.html
Copyright © 2020-2023  润新知