• 设计模式—结构型模式之享元模式


    设计模式—结构型模式之享元模式

    享元模式(Flyweight Pattern),运用共享技术有效地支持大量细粒度对象的复用。系统只使用少量的对象,而这些对象都很相似,状态变化很小,可以实现对象的多次复用。对象结构型。
    在享元模式中可以共享的相同内容称为内部状态(IntrinsicState),而那些需要外部环境来设置的不能共享的内容称为外部状态(Extrinsic State),由于区分了内部状态和外部状态,因此可以通过设置不同的外部状态使得相同的对象可以具有一些不同的特征,而相同的内部状态是可以共享的。
    在享元模式中通常会出现工厂模式,需要创建一个享元工厂来负责维护一个享元池(Flyweight Pool)用于存储具有相同内部状态的享元对象。

    享元模式可以理解为池化技术。

    享元模式包含如下角色:

    • Flyweight: 抽象享元类 Connection
    • ConcreteFlyweight: 具体享元类 ConnectionImpl(user,pwd,url)
    • UnsharedConcreteFlyweight: 非共享具体享元类ConnectionImpl(state)
    • FlyweightFactory: 享元工厂类;可以写一个简单工厂,因为产品就一个Connection。

    例子

    比如一个网吧中有很多电脑,每个电脑都可以被人使用,但是当有人在使用这个电脑,就不能被其他人使用了。这便是享元模式的实例。

    /**
     * 抽象电脑类
     */
    public abstract class AbstarctComputer {
        //是否能被使用
        boolean canUse = true;
    
        //使用电脑
        abstract void useComputer();
    
        //使用完成
        abstract void useComputerCompletion();
    
    
        public boolean isCanUse(){
            return canUse;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    实际的电脑类如下:

    public class MyComputer extends AbstarctComputer{
        //电脑编号
        private String id;
    
        public MyComputer(String id) {
            this.id = id;
        }
    
        /**
         * 使用电脑
         */
        @Override
        void useComputer() {
            System.out.println(this.id + "号电脑使用中");
            //设置其他人不能使用
            this.canUse = false;
        }
    
        @Override
        void useComputerCompletion() {
            System.out.println(this.id + "号电脑使用完成");
            //设置其他人可以使用
            this.canUse = true;
        }
        public String getId() {
            return id;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    网吧类如下:

    public class MyInternetCafe {
    
        //电脑集合--》池子
        private static Map<String,MyComputer> computerPool = new HashMap<>();
    
        static {
            MyComputer myComputer1 = new MyComputer("111");
            MyComputer myComputer2 = new MyComputer("222");
            computerPool.put(myComputer1.getId(),myComputer1);
            computerPool.put(myComputer2.getId(),myComputer2);
        }
    
    
        /**
         * 通过id返回能够使用的电脑,如果找不到,返回一个可以使用的
         * @param id
         * @return
         */
        public static AbstarctComputer getComputer(String id){
            MyComputer computer = computerPool.get(id);
    
            
            if(computer == null || !computer.isCanUse()) {
                //如果没有这个id 或者 不能被使用,找一个可以使用的
                for (MyComputer myComputer : computerPool.values()) {
                    if(myComputer.isCanUse()){
                        System.out.println("您要使用的"+id+"号电脑正在被使用或者无此电脑,为您找到一台电脑id为:"+myComputer.getId());
                        return myComputer;
                    }
                }
                
                //都在使用返回空
                return null;
            }
            
            return computer;
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    测试类如下:

    public class Test {
        public static void main(String[] args) {
            AbstarctComputer computer = MyInternetCafe.getComputer("111");
    
            computer.useComputer();
    
            AbstarctComputer computer1 = MyInternetCafe.getComputer("111");
            computer1.useComputer();
    
            computer.useComputerCompletion();
    
            AbstarctComputer computer2 = MyInternetCafe.getComputer("111");
            computer2.useComputer();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    运行截图如下:

    运行截图

  • 相关阅读:
    【计算机视觉 05】YOLO论文讲解:V1-V7
    asp毕业设计——基于asp+access的文学网站设计与实现(毕业论文+程序源码)——文学网站
    【Java】设计模式之单例模式与工厂模式
    一套方便实用的分页设计方案 Bootstrap5 + Thymeleaf
    9.03 Day45---搭建Vue开发环境,NPM工具使用,Vue工程创建,启动,打包,部署
    计算机毕设(附源码)JAVA-SSM基于的楼盘销售系统的设计与实现
    caspase-2 酶抑制剂有望治疗非酒精性脂肪肝
    CancerLLM: 癌症领域的大型语言模型
    【PID优化】基于樽海鞘算法PID控制器优化设计含Matlab源码
    基于JAVA的学生课程后台管理系统【数据库设计、源码、开题报告】
  • 原文地址:https://blog.csdn.net/qq_41243472/article/details/134517799