设计模式-外观模式

文章目录

一、详解

  • 概念:为多个子系统的交互 提供接口,供客户端使用
  • 主要用途:当一个系统的功能非常复杂,由很多个子系统组成时,可以使用外观模式对这些子系统进行封装,提供一个简单的接口给客户端使用
  • 代码:对外接口、封装多个子系统的实现类

二、代码

  • 源码:https://gitee.com/deschen/designPattern-study

  • 文件上传为例:涉及到用户认证、文件上传、消息通知

  • 对外接口

    java 复制代码
    public interface FileFacade {
    
        /**
         * 文件上传,涉及到用户认证、文件上传、消息通知
         * @param filePath
         * @param fileName
         */
        void uploadFile(String filePath, String fileName);
    
    }
  • 封装多个系统交互的实现类

    java 复制代码
    public class FileFacadeImpl implements FileFacade {
    
        private AuthSystem authSystem;
    
        private FileSystem fileSystem;
    
        private MessageSystem messageSystem;
    
        public FileFacadeImpl(AuthSystem authSystem, FileSystem fileSystem, MessageSystem messageSystem) {
            this.authSystem = authSystem;
            this.fileSystem = fileSystem;
            this.messageSystem = messageSystem;
        }
    
        @Override
        public void uploadFile(String filePath, String fileName) {
            boolean hasAuth = authSystem.isAuthenticated();
            if (!hasAuth) {
                System.out.println("当前用户没有权限");
            }
            fileSystem.uploadFile(filePath, fileName);
    
            messageSystem.sendMessage("上传" + fileName + "到" + filePath + "目录");
        }
    }
    
    
    // 认证系统
    public interface AuthSystem {
    
        /**
         * 判断当前用户是否有权限
          */
        boolean isAuthenticated();
    
    }
    public class AuthSystemImpl implements AuthSystem {
    
        @Override
        public boolean isAuthenticated() {
            System.out.println("认证系统: 当前用户有操作权限");
            return true;
        }
    }
    
    
    // 文件系统
    public interface FileSystem {
    
        /**
         * 上传文件
         * @param filePath
         * @param fileName
         */
        void uploadFile(String filePath, String fileName);
    }
    public class FileSystemImpl implements FileSystem {
    
        @Override
        public void uploadFile(String filePath, String fileName) {
            System.out.println("文件系统: 文件目录: " + filePath + ", 上传的文件: " + fileName);
        }
    }
    
    // 消息系统
    public interface MessageSystem {
    
        /**
         * 发送消息
         * @param message
         */
        void sendMessage(String message);
    }
    public class MessageSystemImpl implements MessageSystem {
    
        @Override
        public void sendMessage(String message) {
            System.out.println("消息系统: 发送的消息: " + message);
        }
    }
  • 测试用例

    java 复制代码
    public class Demo {
    
        public static void main(String[] args) {
            AuthSystem authSystem = new AuthSystemImpl();
            FileSystem fileSystem = new FileSystemImpl();
            MessageSystem messageSystem = new MessageSystemImpl();
            FileFacade fileFacade = new FileFacadeImpl(authSystem, fileSystem, messageSystem);
    
            fileFacade.uploadFile("/resources", "test.doc");
        }
    }
    
    // 输出结果
    认证系统: 当前用户有操作权限
    文件系统: 文件目录: /resources, 上传的文件: test.doc
    消息系统: 发送的消息: 上传test.doc到/resources目录
相关推荐
dishugj36 分钟前
系统架构风险点、敏感点和权衡点的概念和区分
java·开发语言
高晶41 分钟前
ARM 嵌入式 Linux 系统启动流程实战指南
设计模式·程序员
苜蓿muxu44 分钟前
常用代码命名规范
java
高晶1 小时前
BMS 通信开发实战:从 PMBus 协议到数据采集的完整指南
设计模式·程序员
geovindu1 小时前
rust:Memento Pattern(续)
开发语言·后端·设计模式·rust·行为模式·备忘录模
电子IT笔记1 小时前
老MES登录改造:弱密码和暴破一起治
设计模式
马优晨1 小时前
JAVA 中 VO 实体类是什么意思,为什么要做这个
java·开发语言·状态模式·java vo·java vo是什么
Baron X1 小时前
Eclipse Temurin 21 (JRE) Alpine 镜像获取、离线导入与推送操作
java·docker·eclipse·temurin
孙浩君2 小时前
我用 Spring Boot + xterm.js 写了个终端风格互动叙事游戏引擎:游戏即文件,上传一个 JSON 就能玩
java