设计模式(17)-----结构型模式-----外观设计模式
假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html
外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。
这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。
介绍
意图:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
主要解决:降低访问复杂系统的内部子系统时的复杂度,简化客户端与之的接口。
何时使用: 1、客户端不需要知道系统内部的复杂联系,整个系统只需提供一个"接待员"即可。 2、定义系统的入口。
如何解决:客户端不与系统耦合,外观类与系统耦合。
关键代码:在客户端和复杂系统之间再加一层,这一层将调用顺序、依赖关系等处理好。
应用实例: 1、去医院看病,可能要去挂号、门诊、划价、取药,让患者或患者家属觉得很复杂,如果有提供接待人员,只让接待人员来处理,就很方便。 2、JAVA 的三层开发模式。
优点: 1、减少系统相互依赖。 2、提高灵活性。 3、提高了安全性。
缺点:不符合开闭原则,如果要改东西很麻烦,继承重写都不合适。
使用场景: 1、为复杂的模块或子系统提供外界访问的模块。 2、子系统相对独立。 3、预防低水平人员带来的风险。
注意事项:在层次化结构中,可以使用外观模式定义系统中每一层的入口。
案例:
我们看到上面的一张图:
我们现在希望,按下一个按钮就可以看电影,而不是说要需要调用n多个对象去实现
下面我们来看代码:
PopcornPopper(爆米花类)
1 package com.DesignPatterns.ag.hometheater; 2 3 public class PopcornPopper { 4 String description; 5 6 public PopcornPopper(String description) { 7 this.description = description; 8 } 9 10 public void on() { 11 System.out.println(description + " on"); 12 } 13 14 public void off() { 15 System.out.println(description + " off"); 16 } 17 18 public void pop() { 19 System.out.println(description + " popping popcorn!"); 20 } 21 22 23 public String toString() { 24 return description; 25 } 26 }
TheaterLights(灯光类)
1 package com.DesignPatterns.ag.hometheater; 2 3 public class TheaterLights { 4 String description; 5 6 public TheaterLights(String description) { 7 this.description = description; 8 } 9 10 public void on() { 11 System.out.println(description + " on"); 12 } 13 14 public void off() { 15 System.out.println(description + " off"); 16 } 17 18 public void dim(int level) { 19 System.out.println(description + " dimming to " + level + "%"); 20 } 21 22 public String toString() { 23 return description; 24 } 25 }
Screen(投影机类)
1 package com.DesignPatterns.ag.hometheater; 2 3 public class Projector { 4 String description; 5 DvdPlayer dvdPlayer; 6 7 public Projector(String description, DvdPlayer dvdPlayer) { 8 this.description = description; 9 this.dvdPlayer = dvdPlayer; 10 } 11 12 public void on() { 13 System.out.println(description + " on"); 14 } 15 16 public void off() { 17 System.out.println(description + " off"); 18 } 19 20 public void wideScreenMode() { 21 System.out.println(description + " in widescreen mode (16x9 aspect ratio)"); 22 } 23 24 public void tvMode() { 25 System.out.println(description + " in tv mode (4x3 aspect ratio)"); 26 } 27 28 public String toString() { 29 return description; 30 } 31 }
Amplifier(功放类)
1 package com.DesignPatterns.ag.hometheater; 2 3 public class Amplifier { 4 String description; 5 Tuner tuner; 6 DvdPlayer dvd; 7 CdPlayer cd; 8 9 public Amplifier(String description) { 10 this.description = description; 11 } 12 13 public void on() { 14 System.out.println(description + " on"); 15 } 16 17 public void off() { 18 System.out.println(description + " off"); 19 } 20 21 public void setStereoSound() { 22 System.out.println(description + " stereo mode on"); 23 } 24 25 public void setSurroundSound() { 26 System.out.println(description + " surround sound on (5 speakers, 1 subwoofer)"); 27 } 28 29 public void setVolume(int level) { 30 System.out.println(description + " setting volume to " + level); 31 } 32 33 public void setTuner(Tuner tuner) { 34 System.out.println(description + " setting tuner to " + dvd); 35 this.tuner = tuner; 36 } 37 38 public void setDvd(DvdPlayer dvd) { 39 System.out.println(description + " setting DVD player to " + dvd); 40 this.dvd = dvd; 41 } 42 43 public void setCd(CdPlayer cd) { 44 System.out.println(description + " setting CD player to " + cd); 45 this.cd = cd; 46 } 47 48 public String toString() { 49 return description; 50 } 51 }
DvdPlayer(dvd类)
1 package com.DesignPatterns.ag.hometheater; 2 3 public class DvdPlayer { 4 String description; 5 int currentTrack; 6 Amplifier amplifier; 7 String movie; 8 9 public DvdPlayer(String description, Amplifier amplifier) { 10 this.description = description; 11 this.amplifier = amplifier; 12 } 13 14 public void on() { 15 System.out.println(description + " on"); 16 } 17 18 public void off() { 19 System.out.println(description + " off"); 20 } 21 22 public void eject() { 23 movie = null; 24 System.out.println(description + " eject"); 25 } 26 27 public void play(String movie) { 28 this.movie = movie; 29 currentTrack = 0; 30 System.out.println(description + " playing \"" + movie + "\""); 31 } 32 33 public void play(int track) { 34 if (movie == null) { 35 System.out.println(description + " can't play track " + track + " no dvd inserted"); 36 } else { 37 currentTrack = track; 38 System.out.println(description + " playing track " + currentTrack + " of \"" + movie + "\""); 39 } 40 } 41 42 public void stop() { 43 currentTrack = 0; 44 System.out.println(description + " stopped \"" + movie + "\""); 45 } 46 47 public void pause() { 48 System.out.println(description + " paused \"" + movie + "\""); 49 } 50 51 public void setTwoChannelAudio() { 52 System.out.println(description + " set two channel audio"); 53 } 54 55 public void setSurroundAudio() { 56 System.out.println(description + " set surround audio"); 57 } 58 59 public String toString() { 60 return description; 61 } 62 }
测试类
1 package com.DesignPatterns.ag.hometheater; 2 3 public class HomeTheaterTestDrive { 4 public static void main(String[] args) { 5 Amplifier amp = new Amplifier("Top-O-Line Amplifier"); 6 Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp); 7 DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player", amp); 8 Projector projector = new Projector("Top-O-Line Projector", dvd); 9 TheaterLights lights = new TheaterLights("Theater Ceiling Lights"); 10 Screen screen = new Screen("Theater Screen"); 11 PopcornPopper popper = new PopcornPopper("Popcorn Popper"); 12 13 HomeTheaterFacade homeTheater = 14 new HomeTheaterFacade(amp, tuner, dvd, 15 projector, screen, lights, popper); 16 17 homeTheater.watchMovie("Raiders of the Lost Ark"); 18 homeTheater.endMovie(); 19 } 20 }
Get ready to watch a movie... Popcorn Popper on Popcorn Popper popping popcorn! Theater Ceiling Lights dimming to 10% Theater Screen going down Top-O-Line Projector on Top-O-Line Projector in widescreen mode (16x9 aspect ratio) Top-O-Line Amplifier on Top-O-Line Amplifier setting DVD player to Top-O-Line DVD Player Top-O-Line Amplifier surround sound on (5 speakers, 1 subwoofer) Top-O-Line Amplifier setting volume to 5 Top-O-Line DVD Player on Top-O-Line DVD Player playing "Raiders of the Lost Ark" Shutting movie theater down... Popcorn Popper off Theater Ceiling Lights on Theater Screen going up Top-O-Line Projector off Top-O-Line Amplifier off Top-O-Line DVD Player stopped "Raiders of the Lost Ark" Top-O-Line DVD Player eject Top-O-Line DVD Player off
从上面我们可以看到一个watchMovie endMovie就可以调用许多类的方法,这个就是典型的外观设计模式,把复杂的运算包装起来,只是留一个开关来控制后面一大堆的逻辑来实现的。
假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html
设计模式(17)-----结构型模式-----外观设计模式相关推荐
- 设计模式 结构型模式 外观模式(Facade Pattern)
在软件开发过程中,客户端程序经常会与复杂系统的内部子系统进行耦合,从而导致客户端程序随着子系统的变化而变化. 这时为了将复杂系统的内部子系统与客户端之间的依赖解耦,从而就有了外观模式,也称作 &quo ...
- Java设计模式之结构型:外观模式
一.什么是外观模式: 外观模式通过对客户端提供一个统一的接口,用于访问子系统中的一群接口.使用外观模式有以下几点好处: (1)更加易用:使得子系统更加易用,客户端不再需要了解子系统内部的实现,也不需要 ...
- 设计模式:结构型模式-桥接、外观、组合、享元模式
结构型模式 结构型模式描述如何将类或对象按某种布局组成更大的结构.它分为类结构型模式和对象结构型模式,前者采用继承机制来组织接口和类,后者采用组合或聚合来组合对象. 由于组合关系或聚合关系比继承关系耦 ...
- 设计模式之结构型模式(5种)
目录 结构型模式(Structural Pattern):怎么构造一个对象(行为.属性) 一.适配器模式 二.桥接模式(Bridge) 三.装饰者模式 设计模式在JAVA I/O库中的应用 案例 使用 ...
- 备战面试日记(3.3) - (设计模式.23种设计模式之结构型模式)
本人本科毕业,21届毕业生,一年工作经验,简历专业技能如下,现根据简历,并根据所学知识复习准备面试. 记录日期:2022.1.9 大部分知识点只做大致介绍,具体内容根据推荐博文链接进行详细复习. 文章 ...
- 设计模式 之 结构型模式
设计模式 之 结构型模式 模式 & 描述 包括 结构型模式 这些设计模式关注类和对象的组合.继承的概念被用来组合接口和定义组合对象获得新功能的方式. 适配器模式(Adapter Pattern ...
- JAVA23种设计模式(2)-结构型模式7种
JAVA23种设计模式(2)-结构型模式7种 把类结合在一起形成更大的结构 适配器模式(adapter) 一句话:将一个类的接口转换成另一种接口.让原本接口不兼容的类可以兼容 这是平时比较常见的一种模 ...
- 设计模式3——结构型模式
结构型模式描述如何将类或对象按某种布局组成更大的结构,它分为类结构型和对象结构型模式,前者采用继承机制来组织接口和类,后者采用组合或聚合来组合对象. 由于组合关系或聚合关系比继承关系耦合度低,满足&q ...
- 设计模式_结构型模式学习
其中,单例模式用来创建全局唯一的对象.工厂模式用来创建不同但是相关类型的对象(继承同一父类或者接口的一组子类),由给定的参数来决定创建哪种类型的对象.建造者模式是用来创建复杂对象,可以通过设置不同的可 ...
最新文章
- linux 内核调试 booting the kernel.,Uncompressing Linux...done, booting the kernel解决办法
- Linux修改文件及文件夹权限
- Java HashMap的死循环
- ModuleNotFoundError: No module named 'distutils.core'
- 二分k均值 matlab,Matlab函数kmeans:K-均值聚类
- nmap查看开放端口以及使用的协议
- 使用log4net记录日志到数据库(含有自定义属性)
- 操作系统--文件管理
- php lwm2m,理解COAP/LWM2M/MQTT协议和TCP/UDP协议的关系
- 程序员必备的技术社区都有哪些?
- 如何正确获取支付宝网页支付的APPID、私钥、支付宝公钥
- SCCM 2016安装部署
- mysql 数据表格切分_MySQL数据库垂直和水平切分
- java面试-jvm
- 东华大学计算机学院迎新晚会,第五届东华大学研究生才艺之星暨校研会迎新晚会圆满结束...
- SpringCloud-Netfilx
- skyline系列10 - Skyline TerraExplorer 加载数据使用方法 (客户版)
- 计算机科学与技术英语面试,2018北大计算机科学与技术智能科学与技术考研复试通知复试经验英语及面试技巧...
- 第二章:华软代码生成器模板配置
- Java+MySQL+查询操作
热门文章
- 计算机c盘管理权限,删除C盘文件还要权限?一分钟夺回Windows系统权限
- 台式计算机 拯救者刃7000II,战力升级 联想拯救者刃7000 3代游戏台式机震撼上市...
- 取出两个有序数组里面的公共元素 java_C语言计算两个有序数组中的公共元素
- 橄榄山建立的Revit二次开发2群317179938,专门讨论Revit和BIM二次开发问题
- 利用python itchat给女朋友定时发信息
- 统计套利策略的五大主流策略分析与优缺点
- 【Rust日报】2022-05-29 Komorebi Windows 的平铺窗口管理器
- Java笔记-01(Java语言的特性、JDK、JRE、JVM三者之间的关系和Java重点术语)
- 行内元素和块级元素的具体区别
- mac自动备份文件到ftp服务器,被忽视的 FTP 与文件管理工具:ForkLift 3 for Mac