软件设计模式——门面模式(Facade)
2016-04-22 00:06
197 查看
定义: 面模式(facade)又称为外观模式,它是为了解决类与类之间的依赖关系的,像 spring 一样,可以将类和类之间的关系配 置到配置文件中,而外观模式就是将他们的关系放在一个 Facade 类中,降低了类类之间的 耦合度,该模式中没有涉及到接口,看下类图:(我们以一个计算机的启动过程为例)
门面模式的组成:
1)门面角色( facade ):这是门面模式的核心。它被客户角色调用,因此它熟悉子系统的功能。它内部根据客户角色已有的需求预定了几种功能组合。
2)子系统角色:实现了子系统的功能。对它而言, façade 角色就和客户角色一样是未知的,它没有任何 façade 角色的信息和链接。
3)客户角色:调用 façade 角色来完成要得到的功能。
输出为 :
start the computer!
cpu startup!
memory startup!
disk startup!
start computer finished!
begin to close the compter!
cpu shutdown!
memory shutdown!
disk shutdown!
computer closed!
门面模式的组成:
1)门面角色( facade ):这是门面模式的核心。它被客户角色调用,因此它熟悉子系统的功能。它内部根据客户角色已有的需求预定了几种功能组合。
2)子系统角色:实现了子系统的功能。对它而言, façade 角色就和客户角色一样是未知的,它没有任何 façade 角色的信息和链接。
3)客户角色:调用 façade 角色来完成要得到的功能。
//子系统角色——CPU public class CPU { public void startup(){ System.out.println("cpu startup!"); } public void shutdown(){ System.out.println("cpu shutdown!"); } }
//子系统角色——Disk public class Disk { public void startup(){ System.out.println("disk startup!"); } public void shutdown(){ System.out.println("disk shutdown!"); } }
//子系统角色——Memory public class Memory { public void startup(){ System.out.println("memory startup!"); } public void shutdown(){ System.out.println("memory shutdown!"); } }
//门面角色————Computer public class Computer { private CPU cpu; private Memory memory; private Disk disk; public Computer(){ cpu = new CPU(); memory = new Memory(); disk = new Disk(); } public void startup(){ System.out.println("start the computer!"); cpu.startup(); memory.startup(); disk.startup(); System.out.println("start computer finished!"); } public void shutdown(){ System.out.println("begin to close the compter!"); cpu.shutdown(); memory.shutdown(); disk.shutdown(); System.out.println("computer closed!"); } }
//客户角色 public class User { public static void main(String[] args){ Computer computer = new Computer(); computer.startup(); computer.shutdown(); } }
输出为 :
start the computer!
cpu startup!
memory startup!
disk startup!
start computer finished!
begin to close the compter!
cpu shutdown!
memory shutdown!
disk shutdown!
computer closed!
相关文章推荐
- SpringMVC拦截器(资源和权限管理)
- 機械学習の Python との出会い
- JavaScript基础
- 20150122--商品入库+缩略图+访问权限
- unity3d 坐标系
- jstat命令
- jquery attr()方法
- 从今天开始,记录每天学习的点点滴滴
- lightoj 1071
- LiteIDE X33.1 发布,Go 语言开发工具
- ASP.NET 中ImageMap控件的用法
- ASP.NET中Label控件用法详解
- ASP.NET中 ListBox列表框控件的使用方法
- ASP.NET中Application、Cookie、Session、Cache和ViewState
- ASP.NET中Literal控件的使用方法 原创
- ASP.NET Core配置教程之读取配置信息
- Django小白教程之Django用户注册与登录
- Python开发之快速搭建自动回复微信公众号功能
- 基于python实现的抓取腾讯视频所有电影的爬虫
- 在windows下快速搭建web.py开发框架方法