设计模式-外观模式

文章目录

一、详解

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

二、代码

  • 源码: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目录
相关推荐
q***710114 小时前
Spring Boot(快速上手)
java·spring boot·后端
better_liang16 小时前
每日Java面试场景题知识点之-分布式事务处理
java·微服务·面试·springcloud·分布式事务
执笔论英雄17 小时前
Slime异步原理(单例设计模式)4
开发语言·python·设计模式
L***d67018 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
凌波粒18 小时前
Springboot基础教程(3)--自动装配原理/静态资源处理/欢迎页
java·spring boot·后端
likuolei18 小时前
XSL-FO 软件
java·开发语言·前端·数据库
凌波粒18 小时前
SpringBoot基础教程(2)--yaml/配置文件注入/数据校验/多环境配置
java·spring boot·后端·spring
S***267518 小时前
Spring Boot环境配置
java·spring boot·后端
6***830518 小时前
什么是Spring Boot 应用开发?
java·spring boot·后端
毕设源码柳学姐18 小时前
计算机毕设 java 智慧社区服务系统 SSM 框架社区生活平台 Java 开发的便民服务与互动系统
java·开发语言·生活