设计模式-外观模式
来源:互联网 发布:分类信息网 软件 编辑:程序博客网 时间:2024/06/12 01:36
定义:子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个借口使得这一子系统更加容易使用。
类图:
说明:Facade表示外观角色,客户端可以调用这个角色的方法,此角色知道相关的(一个或者多个)子系统的功能和责任,在这种情况下,将所有从客户端发来的请求委派到相应的子系统去,传递给相应的子系统对象处理;Subsystem表示子系统角色,一个系统可以同时有一个或者多个子系统,每一个子系统都不是一个单独的类,而是一个类的集合,它实现子系统的功能,每一个子系统都可以被客户端直接调用,或被外观角色调用,它处理有外观泪传过来的任务,子系统并不知道外观的存在,对于子系统而言,外观仅仅是一个客户端而已。
优点:
①引入外观你模式,是客户对子系统使用变得简单了,减少了与子系统的关联对象,实现了子系统与客户端之间的松耦合关系
②客户端屏蔽了子系统组件,减少了客户端所需处理的对象数目,并使得子系统使用起来更加容易。
缺点:
①不能很好地限制客户使用子系统类,如果对客户访问子系统类做太多的限制则减少了可变性和灵活性
②在不引入抽象外观类的情况下,增加行的子系统可能需要修改外观类或客户端的源代码,违背了“开闭原则”;
适用环境:
①当要为一个复杂子系统提供一个简单接口时可以使用外观模式
②客户程序与多个子系统之间存在很大的依赖性。引入外观类将子系统与客户以及其他子系统解耦,可以提高子系统的独立性和可移植性。
实例场景:某系统需要提供一个文件加密模块,加密流程包括三个操作,分别是读取源文件、加密、保存加密之后的文件。读取文件和保存文件使用流来实现,者三个操作相对独立,其业务代码封装在三个不同的类中。现在需要提供一个统一的加密外观类 ,用户可以直接使用该加密外观类来完成文件的读取、加密和保存三个操作,而不需要与每一个类进行交互。
实例代码:
//以下分别为文件读取,加密,保存类:
/** * 文件读取类:子系统类 * @author fly * */class FileReader {public String read(String fileNameSrc) {// TODO Auto-generated method stubSystem.out.println("读取文件,获取明文");StringBuffer sb = new StringBuffer();try {FileInputStream inFS = new FileInputStream(fileNameSrc);int data;while ((data=inFS.read())!=-1) {sb = sb.append((char)data);}inFS.close();} catch (FileNotFoundException e) {// TODO: handle exceptionSystem.out.println("文件不存在!");}catch (IOException e) {// TODO: handle exceptionSystem.out.println("文件操作失败!");}return sb.toString();}}/** * 数据加密类:子系统类 * @author fly * */class CipherMachine {public String encrypt(String plainText) {// TODO Auto-generated method stubSystem.out.println("数据加密,将明文转换为密文。");String es = "";for (int i = 0; i < plainText.length(); i++) {String c = String.valueOf(plainText.charAt(i)%7);es+=c;}return es;}}/** * 文件保存类:子系统类 * @author fly * */class FileWriter {public void write(String encryptStr,String fileNameDes) {// TODO Auto-generated method stubSystem.out.println("保存密文,写入文件");try {FileOutputStream outFS = new FileOutputStream(fileNameDes);outFS.write(encryptStr.getBytes());outFS.close();} catch (FileNotFoundException e) {// TODO: handle exceptionSystem.out.println("文件不存在!");}catch (IOException e) {// TODO: handle exceptionSystem.out.println("文件操作错误!");}}}//以下为外观类,使用该类直接之间完成读取、加密、保存、三个操作。
class EncryptFacade {private FileReader reader;private CipherMachine cipher;private FileWriter writer;public EncryptFacade() {// TODO Auto-generated constructor stubreader = new FileReader();cipher = new CipherMachine();writer = new FileWriter();}public void fileEncrypt(String fileNameSrc,String fileNameDes){String plainStr = reader.read(fileNameSrc);String encryptStr = cipher.encrypt(plainStr);writer.write(encryptStr, fileNameDes);}}//客户端测试类
public class Client {public static void main(String[] args) {EncryptFacade ef = new EncryptFacade();ef.fileEncrypt(System.getProperty("user.dir")+"\\src\\facade\\src.txt", System.getProperty("user.dir")+"/src/facade/des.txt");}}//上述两个文件位置截图:
- 设计模式--外观模式
- 设计模式-外观模式
- 设计模式:外观模式
- 设计模式----外观模式
- 设计模式------外观模式
- 设计模式 外观模式
- 设计模式-【外观模式】
- 设计模式-----外观模式
- 设计模式 - 外观模式
- 设计模式 - 外观模式
- 设计模式- 外观模式
- 设计模式-外观模式
- 设计模式 - 外观模式
- 设计模式-外观模式
- 设计模式 - 外观模式
- 设计模式-外观模式
- 设计模式--外观模式
- 设计模式-外观模式
- PHP中错误模块简介
- 文本建模常用的预处理方法
- 物联网操作系统HelloX开发者入门指南
- 排序算法总结---冒泡排序
- 滚动页面时DIV到顶部时fixed(原生js实现)
- 设计模式-外观模式
- this is a test
- 教程——基于HLS实现FIR滤波器
- 网络编程 select epoll
- ubuntu下安装XAMPP
- Quartz2D Paths 重要属性
- Timer的用法
- How to pass QEMU command-line options through libvirt
- Caffe+Ubuntu14.04+CUDA6.5新手安装配置指南