原版设计模式之外观模式(Facade)

Intent (定义)

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.(将子系统的一系列接口统一为一个接口。外观模式为了使子系统更易用,而定义了更高层次的接口

Motivation (更多释义)

Structuring a system into subsystems helps reduce complexity. A common design goal is to minimize the communication and dependencies between subsystems. One way to achieve this goal is to introduce a facade object that provides a single, simplified interface to the more general facilities of a subsystem.(将系统拆分为子系统有助于降低复杂度。常用的设计目标是减少子系统间的通信和依赖。实现这种设计的一个方式是引入一个对外的对象,这个对象为子系统常用的功能提供一个单一功能、简单的方法
在这里插入图片描述

如下图所示,应用程序访问其编译器子系统的编程环境。这个子系统包含Scanner, Parser, ProgramNode, BytecodeStream, 和ProgramNodeBuilder等类。
有些应用程序可能需要直接访问这些类。但是大多数客户端并不关心解析和代码生成等细节,他们只是想编译一些代码。
对于他们来说,编译器子系统中功能强大,更底层的接口只会使它们的任务复杂化。
在这里插入图片描述
为了不让客户端过度依赖这些底层接口,就需要在子系统中提供一个对外的编译对象——Compiler。
Compiler对外提供一个单一的,简单的compile方法,并整合了Scanner,Parser这些类。
The compiler facade makes life easier for most programmers without hiding the lower-level functionality from the few that need it.(Complier类使程序运行变得更简单,同时也没有对需要使用底层功能的客户隐藏这些功能。

Applicability (适用点)

  • you want to provide a simple interface to a complex subsystem. (当你想给复杂的子系统提供一个简单的接口时。)Subsystems often get more complex as they evolve. Most patterns, when applied, result in more and smaller classes. This makes the subsystem more reusable and easier to customize, but it also becomes harder to use for clients that don’t need to customize it. A facade can provide a simple default view of the subsystem that is good enough for most clients. Only clients needing more customizability will need to look beyond the facade.(随着子系统的迭代升级,会变得更加复杂。当使用其他大多数的模式时,会产生更多,更细化的类,这导致子系统变得更加灵活和自定义,但是对于不需要自定义的客户,就变得更难接入了。外观接口为子系统提供了一个默认视图,对于大部分客户端来说,是足够使用的。仅仅只有需要自定义的客户端才不需要使用外观接口。
  • there are many dependencies between clients and the implementation classes of an abstraction. Introduce a facade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and portability.(客户端和抽象的实现之间有很强的依赖关系。引入一个外观接口使子系统与客户端和其他子系统解耦,从而提高子系统的独立性和可移植性。)
  • you want to layer your subsystems. Use a facade to define an entry point to each subsystem level. If subsystems are dependent, then you can simplify the dependencies between them by making them communicate with each other solely through their facades.(想对子系统进行分层。使用外观接口作为每个子系统级别的入口。当子系统间相互依赖时,那你就可以通过它们的外观接口相互通信,从而简化它们之间的依赖关系。

Structure (结构)

在这里插入图片描述

Participants (类说明)

  • Facade (Compiler) (外观
    • knows which subsystem classes are responsible for a request. (了解子系统的哪些类需要处理请求
    • delegates client requests to appropriate subsystem objects. (将客户端请求委托给适当的子系统对象。
  • subsystem classes (Scanner, Parser, ProgramNode, etc.) (子系统
    • implement subsystem functionality. (实现子系统功能
    • handle work assigned by the Facade object. (处理外观对象分配的任务
    • have no knowledge of the facade; that is, they keep no references to it.(对外观对象一无所知,这意味着不需要指向外观对象【依赖,实现,继承等关系都不存在】

Collaborations (约定)

  • Clients communicate with the subsystem by sending requests to Facade, which forwards them to the appropriate subsystem object(s). Although the subsystem objects perform the actual work, the facade may have to do work of its own to translate its interface to subsystem interfaces.(客户端通过向Facade发送请求与子系统通信,Facade将请求转发给适当的子系统对象。尽管子系统对象执行实际工作,但Facade可能需要做某些处理,才能和子系统接口对接上。
  • Clients that use the facade don’t have to access its subsystem objects directly.(客户端使用外观对象,而不需要直接接触子系统的对象

Related Patterns (相关模式)

  • Abstract Factory (99) can be used with Facade to provide an interface for creating subsystem objects in a subsystem-independent way. Abstract Factory can also be used as an alternative to Facade to hide platform-specific classes.(抽象工厂可以与Facade一起使用,以独立于子系统的方式对外提供一个创建子系统对象的接口。抽象工厂还可以作为Facade的替代模式,用来隐藏特殊平台的类。
  • Mediator (305) is similar to Facade in that it abstracts functionality of existing classes. However, Mediator’s purpose is to abstract arbitrary communication between colleague objects, often centralizing functionality that doesn’t belong in any one of them. A mediator’s colleagues are aware of and communicate with the mediator instead of communicating with each other directly(中介者在抽象现有类功能方面类似于Facade。然而,中介者的目的是把同事对象间的任意通信抽象隔离开,核心功能并不属于同事对象间任何一个对象。中介者的同事们知道自己是和中介者在交流,而不是同事间直接相互沟通。). In contrast, a facade merely abstracts the interface to subsystem objects to make them easier to use; it doesn’t define new functionality, and subsystem classes don’t know about it.(相比之下,外观模式只是抽象子系统对象形成一个新接口,使它们更容易被使用;外观模式没有定义新的功能,而且子系统类也不知道它。
  • Usually only one Facade object is required. Thus Facade objects are often Singletons. (通常情况下只需要一个Facade对象,Facade对象经常是一个单例对象
posted @ 2022-05-24 10:47  伟衙内  阅读(45)  评论(0编辑  收藏  举报