设计模式之外观模式

tech2022-07-06  225

目录

外观模式基本介绍示例小结

外观模式基本介绍

外观模式(Facade),也叫过程模式,外观模式为子系统中的一组接口提供 一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加 容易使用;外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端 只需跟这个接口发生调用,而无需关心这个子系统的内部细节

示例

影院例子

package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:21 */ public class DVDPlay { private static DVDPlay instance = new DVDPlay(); private DVDPlay() { } public static DVDPlay getInstance(){ return instance; } public void on(){ System.out.println("on"); } public void off(){ System.out.println("off"); } public void play(){ System.out.println("play"); } public void pause(){ System.out.println("pause"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:23 */ public class BaoMiHua { private BaoMiHua(){} private static BaoMiHua instance = new BaoMiHua(); public static BaoMiHua getInstance(){ return instance; } public void on(){ System.out.println("爆米花机on"); } public void off(){ System.out.println("爆米花机off"); } public void pop(){ System.out.println("爆米花机pop"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:29 */ public class Light { private Light(){} private static Light instance = new Light(); public static Light getInstance(){ return instance; } public void on(){ System.out.println("灯光on"); } public void off(){ System.out.println("灯光off"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:27 */ public class Screen { private Screen(){} private static Screen instance = new Screen(); public static Screen getInstance(){ return instance; } public void up(){ System.out.println("屏幕up"); } public void down(){ System.out.println("屏幕down"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:25 */ public class TouYing { private TouYing(){} private static TouYing instance = new TouYing(); public static TouYing getInstance(){ return instance; } public void on(){ System.out.println("投影仪on"); } public void off(){ System.out.println("投影仪off"); } public void focus(){ System.out.println("投影仪focus"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:28 */ public class YinXiang { private YinXiang(){} private static YinXiang instance = new YinXiang(); public static YinXiang getInstance(){ return instance; } public void on(){ System.out.println("音响on"); } public void off(){ System.out.println("音响off"); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:33 */ public class Home { //定义子系统对象 private BaoMiHua baoMiHua; private DVDPlay dvdPlay; private Light light; private Screen screen; private TouYing touYing; private YinXiang yinXiang; public Home() { this.baoMiHua = BaoMiHua.getInstance(); this.dvdPlay = DVDPlay.getInstance(); this.light = Light.getInstance(); this.screen = Screen.getInstance(); this.touYing = TouYing.getInstance(); this.yinXiang = YinXiang.getInstance(); } public void ready(){ baoMiHua.on(); baoMiHua.pop(); screen.down(); touYing.on(); yinXiang.on(); dvdPlay.on(); } public void play(){ dvdPlay.play(); } public void pause(){ dvdPlay.pause(); } public void end(){ baoMiHua.off(); screen.up(); touYing.off(); yinXiang.off(); dvdPlay.off(); } } package com.lesson11.waiguan; /** * @author 朝花不迟暮 * @version 1.0 * @date 2020/9/2 19:30 */ public class Client { public static void main(String[] args) { Home home = new Home(); home.ready(); home.play(); } }

灵魂在于设计分层 ——沃兹基朔德

小结

外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性;外观模式对客户端与子系统的耦合关系,让子系统内部的模块更易维护和扩展;通过合理的使用外观模式,可以帮我们更好的划分访问的层次;当系统需要进行分层设计时,可以考虑使用Facade模式;在维护一个遗留的大型系统时,可能这个系统已经变得非常难以维护和扩展,此时可以考虑为新系统开发一个Facade类,来提供遗留系统的比较清晰简单的接口,让新系统与Facade类交互,提高复用性;不能过多的或者不合理的使用外观模式,使用外观模式好,还是直接调用模块好。要以让系统有层次,利于维护为目的。
最新回复(0)