设计模式(十)外观

一、定义

为子系统中的一组接口提供一个一致的入口,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。外观模式是一种结构型模式。

二、描述

包含以下两个角色: 1、Facade(外观角色):在客户端可以调用它的方法,在外观角色中可以知道相关的(一个或多个)子系统的功能和责任;在正常情况下,它将所有从客户端发来的请求委派到相应的子系统中去,传递给相应的子系统对象处理。
2、SubSystem(子系统角色):在软件系统中可以有一个或者多个子系统角色,每一个子系统可以不是一个单独的类,而是一个类的集合,它实现子系统的功能;每一个子系统都可以被客户端直接调用,或者被外观角色调用,它处理外观类传过来的请求;子系统并不知道外观的存在,对于子系统而言,外观角色仅仅是另一个客户端而已。

三、例子

X公司想要开发一个可应用于多个软件的文件加密模块,该模块可以对文件中的数据进行加密并将加密后的数据存储在一个新文件中,具体的流程包括3个部分,分别是读取源文件、加密、保存加密之后的文件。其中,读取文件和保存文件使用流来实现,加密操作通过求模运算实现。这3个操作相对独立,为了实现代码地独立重用,让设计更加符合单一职责原则,这3个操作的业务代码封装在3个不同的类中。 FileReader、CipherMachie、FileWriter:文件读取类、数据加密类、文件保存类,充当子系统类

C# 复制代码
public class FileReader
{
    public string Read(string fileNameSrc)
    {
        Console.WriteLine("读取文件,获取明文:");
        FileStream fs = null;
        StringBuilder sb = new StringBuilder();
        try
        {
            fs = new FileStream(fileNameSrc, FileMode.Open);
            int data;
            while ((data = fs.ReadByte()) != -1)
            {
                sb.Append((char)data);
            }
            fs.Close();
            Console.WriteLine(sb.ToString());
        }
        catch (FileNotFoundException e)
        {
            Console.WriteLine("文件不存在");
        }
        catch (IOException e)
        {
            Console.WriteLine("文件操作错误");
        }
        return sb.ToString();
    }
}

public class CipherMachine
{
    public string Encrypt(string plainText)
    {
        Console.WriteLine("数据加密,将明文转换为密文:");
        string es = "";
        char[] chars = plainText.ToCharArray();
        foreach (char ch in chars)
        {
            string c = (ch % 7).ToString();
            es += c;
        }
        Console.WriteLine(es);
        return es;
    }
}

public class FileWriter
{
    public void Write(string encryptedStr, string fileNameDes)
    {
        Console.WriteLine("保存密文,写入文件");
        FileStream fs = null;
        StringBuilder sb = new StringBuilder();
        try
        {
            fs = new FileStream(fileNameDes, FileMode.Create);
            byte[] str = Encoding.Default.GetBytes(encryptedStr);
            fs.Write(str, 0, str.Length);
            fs.Flush();
            fs.Close();
        }
        catch (FileNotFoundException e)
        {
            Console.WriteLine("文件不存在");
        }
        catch (IOException e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine("文件操作错误");
        }
    }
}

EncrytFacade:外观类

C# 复制代码
public class EncryptFacade
{
    private FileReader reader;
    private CipherMachine cipher;
    private FileWriter writer;

    public EncryptFacade()
    {
        reader = new FileReader();
        cipher = new CipherMachine();
        writer = new FileWriter();
    }

    public void FileEncrypt(string fileNameSrc, string fileNameDes)
    {
        string plainStr = reader.Read(fileNameSrc);
        string encryptedStr = cipher.Encrypt(plainStr);
        writer.Write(encryptedStr, fileNameDes);
    }
}

Program:客户端测试类

C# 复制代码
EncryptFacade facade = new EncryptFacade();
facade.FileEncrypt("src.txt", "des.txt");
Console.ReadLine();

四、总结

1、优点

(1)它对客户端屏蔽了子系统组件,减少了客户端需要处理的对象数目,并且使得子系统使用起来更加容易。通过引入外观模式,客户端代码将变得很简单,与之关联的对象也很少。
(2)它实现了子系统与客户端之间的松耦合关系,这使得子系统的变化不会影响到调用它的客户端,只需要调整外观类即可。
(3)一个子系统的修改对于其他子系统没有任何影响,而且子系统的内部变化也不会影响到外观对象。

2、缺点

(1)外观模式不能很好地限制客户端直接使用子系统类,如果对客户端访问子系统类做太多的限制则减少了可变性和灵活性。
(2)如果设计不当,增加新的子系统可能需要修改外观类的源代码,违背了开闭原则。

相关推荐
李广坤29 分钟前
状态模式(State Pattern)
设计模式
李广坤2 小时前
观察者模式(Observer Pattern)
设计模式
李广坤3 小时前
中介者模式(Mediator Pattern)
设计模式
李广坤3 小时前
迭代器模式(Iterator Pattern)
设计模式
李广坤3 小时前
解释器模式(Interpreter Pattern)
设计模式
阿无,6 小时前
java23种设计模式之前言
设计模式
Asort7 小时前
JavaScript设计模式(八):组合模式(Composite)——构建灵活可扩展的树形对象结构
前端·javascript·设计模式
数据智能老司机7 小时前
数据工程设计模式——数据基础
大数据·设计模式·架构
笨手笨脚の10 小时前
设计模式-代理模式
设计模式·代理模式·aop·动态代理·结构型设计模式
Overboom17 小时前
[C++] --- 常用设计模式
开发语言·c++·设计模式