如何使用设计模式的外观模式
发表于:2025-11-20 作者:千家信息网编辑
千家信息网最后更新 2025年11月20日,这篇文章主要介绍"如何使用设计模式的外观模式",在日常操作中,相信很多人在如何使用设计模式的外观模式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"如何使用设计模式的外观
千家信息网最后更新 2025年11月20日如何使用设计模式的外观模式
这篇文章主要介绍"如何使用设计模式的外观模式",在日常操作中,相信很多人在如何使用设计模式的外观模式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"如何使用设计模式的外观模式"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1、概述
外观模式是一种结构型设计模式, 能为程序库、 框架或其他复杂类提供一个简单的接口。
避免多种不相关的功能污染单一外观, 使其变成又一个复杂结构。客户端和其他外观都可使用附加外观。
2、适用场景
1)如果你需要一个指向复杂子系统的直接接口, 且该接口的功能有限, 则可以使用外观模式。外观将会提供指向子系统中最常用功能的快捷方式, 能够满足客户端的大部分需求。
2)如果需要将子系统组织为多层结构, 可以使用外观。你可以为每个层次创建一个外观, 然后要求各层的类必须通过这些外观进行交互。
3、实例
有以下场景:
当前有学生子系统,该系统有三个接口,查询学生姓名,查询学生年龄,查询学生家庭地址。
有一个教学系统,要分别去调用这三个接口。
有一个成绩系统,要分别调用者三个接口。
有一个考试系统,也要分别调用这三个系统。
3.1 不使用外观模式时候
/** * 学生 */ public class Student { private String name = "狼王"; private int age = 25; private String address = "上海"; public Student(String name, int age, String address) { this.name = name; this.age = age; this.address = address; } public Student(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }/** * 学生 */ public class Student { private String name = "狼王"; private int age = 25; private String address = "上海"; public Student(String name, int age, String address) { this.name = name; this.age = age; this.address = address; } public Student(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }/** * 年龄接口 */ @Service public class StudentAgeService implements IStudentAge{ @Override public int getAge() { Student student = new Student(); return student.getAge(); } }@Service public class StudentNameService implements IStudentName{ @Override public String getName() { Student student = new Student(); return student.getName(); } }三个外部服务
/** * 教育服务 */ @Service public class EduService { @Autowired private StudentNameService studentNameService; @Autowired private StudentAgeService studentAgeService; @Autowired private StudentAddressService studentAddressService; public void getStudentName(){ System.out.println("学生姓名是:" + studentNameService.getName()); } public void getStudentAge(){ System.out.println("学生年龄是:" + studentAgeService.getAge()); } public void getStudentAddress(){ System.out.println("学生地址是:" + studentAddressService.getAddress()); } }/** * 考试服务 */ @Service public class ExamService { @Autowired private StudentNameService studentNameService; @Autowired private StudentAgeService studentAgeService; @Autowired private StudentAddressService studentAddressService; public void getStudentName(){ System.out.println("学生姓名是:" + studentNameService.getName()); } public void getStudentAge(){ System.out.println("学生年龄是:" + studentAgeService.getAge()); } public void getStudentAddress(){ System.out.println("学生地址是:" + studentAddressService.getAddress()); } }/** * 成绩服务 */ @Service public class ScoreService { @Autowired private StudentNameService studentNameService; @Autowired private StudentAgeService studentAgeService; @Autowired private StudentAddressService studentAddressService; public void getStudentName(){ System.out.println("学生姓名是:" + studentNameService.getName()); } public void getStudentAge(){ System.out.println("学生年龄是:" + studentAgeService.getAge()); } public void getStudentAddress(){ System.out.println("学生地址是:" + studentAddressService.getAddress()); } }3.2 使用外观模式
在学生服务这里增加一个外观service
/** * 外观模式服务 */ @Service public class StudentFacedService { @Autowired private StudentNameService studentNameService; @Autowired private StudentAgeService studentAgeService; @Autowired private StudentAddressService studentAddressService; public String getStudentName(){ return studentNameService.getName(); } public int getStudentAge(){ return studentAgeService.getAge(); } public String getStudentAddress(){ return studentAddressService.getAddress(); } }三个调用服务只需要引入外观服务
/** * 教育服务 */ @Service public class EduService { @Autowired private StudentFacedService studentFacedService; public void getStudentName() { System.out.println("学生姓名是:" + studentFacedService.getStudentName()); } public void getStudentAge() { System.out.println("学生年龄是:" + studentFacedService.getStudentAge()); } public void getStudentAddress() { System.out.println("学生地址是:" + studentFacedService.getStudentAddress()); } }/** * 考试服务 */ @Service public class ExamService { @Autowired private StudentFacedService studentFacedService; public void getStudentName() { System.out.println("学生姓名是:" + studentFacedService.getStudentName()); } public void getStudentAge() { System.out.println("学生年龄是:" + studentFacedService.getStudentAge()); } public void getStudentAddress() { System.out.println("学生地址是:" + studentFacedService.getStudentAddress()); } }/** * 成绩服务 */ @Service public class ScoreService { @Autowired private StudentFacedService studentFacedService; public void getStudentName() { System.out.println("学生姓名是:" + studentFacedService.getStudentName()); } public void getStudentAge() { System.out.println("学生年龄是:" + studentFacedService.getStudentAge()); } public void getStudentAddress() { System.out.println("学生地址是:" + studentFacedService.getStudentAddress()); } }4、分析
以上两种方式代码结构如下所示:


从上面两张图可以看到,对于外部服务来说,极大的缩减了代码复杂度,只需要调用学生服务的一个接口。
5、总结
优点:
让客户端代码独立独立于复杂的子系统,且减少对于子系统的依赖。
缺点:
过于庞大的外观,会使得该外观称成为上帝对象,造成所有类的耦合,可通过它操作所有的类功能。
到此,关于"如何使用设计模式的外观模式"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
学生
外观
模式
服务
年龄
接口
地址
姓名
子系统
设计模式
设计
复杂
三个
系统
功能
结构
学习
代码
客户
成绩
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件开发中心迁移
shell语句建立服务器连接
中职计算机网络技术ps笔记
工行西安软件开发中心展望
黑龙江仟帧网络技术有限公司
本科院校离校系统软件开发
网络安全事件的考核
软件开发工程师30之后
胃癌TCGA数据库
踢出服务器
广州net软件开发哪家专业
河北新一代网络技术质量服务
网络安全形势研判与防控策略培训
计算机网络技术学来有什么用
当前数据库的一些主流技术
燃气网络安全关键基础设施
网络安全艺术字立体简单
宁夏回族自治区网络安全应急预案
媒体中心网络安全规范
手机电脑互联网有关的科技
美橙云 服务器
互联网的根或服务器在什么地方
新年云服务器
家庭网络安全加固
美国直面竞争网络安全官
瀑布模型是一种软件开发方法
数据库如何通过互联网
低代码软件开发是什么
网络技术基础学什么的
怎么做手机软件开发公司