外观设计模式和网关设计模式有什么区别?

发布于 2024-10-07 05:33:56 字数 16 浏览 2 评论 0原文

或者门面==网关?

or Facade==Gateway?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

零崎曲识 2024-10-14 05:33:56

回顾 GoF 书中的 Facade 和 Martin Fowler 的 Gateway 的另一个答案中的链接,看起来他们的重点是相反的方向。

Facade 向(一个或多个)外部客户端提供复杂内部结构的简单统一视图;

网关为应用程序内部提供了外部资源的简单统一视图。

这种区别让我们关注设计中哪个更重要:

对于 Facade,外部系统是我们的客户;对于 Facade,外部系统是我们的客户;而对于 Facade,外部系统是我们的客户。如果可以使外部接口更简单,那么最好增加面向内的复杂性。

有了网关,内部系统就是我们的客户;尽我们所能提供帮助,即使外部情况更加复杂。

Reviewing Facade in the GoF book and the link in another answer to Martin Fowler's Gateway, it appears that their focus is in opposite directions.

Facade provides a simple uniform view of complex internals to (one or more) external clients;

Gateway provides a simple uniform view of external resources to the internals of an application.

This distinction lets us focus on which is more important in a design:

With the Facade, the external system is our customer; it is better to add complexity facing inwards if it makes the external interface simpler.

With the Gateway, the internal system is our customer; give it all the help we can, even if the externals are more complex.

迷你仙 2024-10-14 05:33:56

这两种模式非常相似,都充当某些东西的包装。区别在于上下文:外观代表一组子系统,而网关可以代表任何功能。从这个角度来看,对我来说Facade是Gateway的具体案例(不是相反)。

当我们认为使用子系统很复杂或者如果我们想要将多个子系统调用分组到一个[方法]执行中时,就会应用外观。然而,这并不一定意味着子系统不可访问,或者它们足够复杂。它仅仅意味着我们拥有子系统。

当我们想要包装一些东西并将它们以不同的方式公开时,就可以使用网关。网关可能不包含一个子系统,而只是一个相对复杂的功能。网关是一种通用模式,可以将其视为外观、代理和其他模式的基础。

如果还需要举例说明

Facade可以通过查询支票账户子系统、信用账户子系统、储蓄子系统和后台子系统来计算客户的信用度(我想我在GOF中见过类似的例子)图书)。

class MortgateFacade {
    bool IsCreditWorth(string customerName) {
        return !_checkingAccSystem.HasNegativeBalance(customerName) && !_creditAccSystem.HasNegativeCredit(customerName) && !_backOfficeSystem.IsInBlackList(customerName);
    }
}

网关可以查询数据库表并根据ID返回客户。 (是的,就这么简单!)

class CustomersGateway {
    Customer GetCustomer(int id) {
        return _db.ExecuteOne("SELECT TOP 1 FROM CUSTOMERS WHERE CUSTOMER_ID="+id).AsCustomer();
    }
}

【显然这是伪代码】

These two patterns are very similar in the way that both they serve as wrappers over something. The difference is in the context: facade stands over a group of subsystems, while gateway can be standing over any functionality. From this point of view, to me Facade is the concrete case of Gateway (not opposite).

Facade is applied when we think that working with subsystems is complex or if we want to group several subsystem calls into one [method] execution. However, this does not necessarily mean that subsystems are not accessible, or that they are complex enough. It simply means that we have subsystems.

Gateway is applied when we want to wrap some things and expose them into different way. Gateway may be wrapping not a subsystem, but just a relatively complex functionality. Gateway is a general pattern which can be thought as a basis for Facade, Proxy, and other patterns.

If example is still needed for clarification:

Facade can calculate credit worthiness of a customer by querying checking accounts subsystem, credit accounts subsystem, savings subsystem, and back office subsystem (I guess I have seen similar example in GOF books).

class MortgateFacade {
    bool IsCreditWorth(string customerName) {
        return !_checkingAccSystem.HasNegativeBalance(customerName) && !_creditAccSystem.HasNegativeCredit(customerName) && !_backOfficeSystem.IsInBlackList(customerName);
    }
}

Gateway can query the database table and return customer by ID. (Yes, that simple!)

class CustomersGateway {
    Customer GetCustomer(int id) {
        return _db.ExecuteOne("SELECT TOP 1 FROM CUSTOMERS WHERE CUSTOMER_ID="+id).AsCustomer();
    }
}

[Obviously this is a pseudo code]

小镇女孩 2024-10-14 05:33:56

Facade 的意图由 http://c2.com/cgi/wiki?FacadePattern 给出

为集合提供统一的接口
子系统中的接口。正面
定义了一个更高级别的接口
使子系统更易于使用。
这可以用来简化数字
复杂的对象交互
进入单一界面。

这里的重点是设计一个隐藏复杂性的界面,我认为关键思想是将多个细粒度的交互隐藏在一个更可用的交互中。因此,Facade 的重点是面向客户。

网关模式不是原始的 GOF 模式之一,我将其更多地视为一种企业集成模式,即处于比 Facade 更高的级别。请参阅 Fowler 的定义

我认为网关主要是为了隐藏技术复杂性而不是接口复杂性 - 隐藏连接到大型机的细节以及外部系统。事实上,我经常期望网关成为某种请求路由器,甚至可能根据请求详细信息选择不同的后端系统。所以我认为 Gateway 专注于它所通往的事物。

显然,非正式地网关是一个外观,因为它隐藏了细节,但我认为当您实现 GOF 外观和 Fowler 网关时,您最终会做完全不同的事情。

The Intent of Facade is given by http://c2.com/cgi/wiki?FacadePattern as

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.
This can be used to simplify a number
of complicated object interactions
into a single interface.

The focus here is on designing an interface which hides complexity, and I think the key idea is to hide multiple fine-grained interactions in a single more usable interaction. Hence the focus of Facade is client-facing.

The gateway pattern is not one of the original GOF patterns, and I see it more as an Enterprise Integration Pattern, ie at a rather higher level than the Facade. See Fowler's definition

I see the Gateway as principally about hiding technological complexity rather then interface complexity - hiding the details of connecting to mainframes and external systems. In fact I often expect the gateway to become something of a request router, perhaps even selecting different backend systems on the basis of request details. So I see Gateway as being focused on the things it's a gateway to.

Obviously, informally a Gateway is a Facade, in that it hides detail, but I think when you implement a GOF Facade and a Fowler Gateway you end up doing quite different things.

各自安好 2024-10-14 05:33:56

这可能有点简化,但这是我的看法。

  • 使用外观模式时,您提供了其他人可以用来与您的应用程序对话的界面。 示例:您已经实现了一些具有多个“模块”的应用程序,为了更轻松地访问“模块”,您创建了一个外观,以便更轻松地与模块进行交互...单点的接触。
  • 使用网关模式时,您可以封装一些您想要使用的外部部分。 示例:您想要使用日志记录,但不想绑定到特定的日志记录框架,在这种情况下,您可以定义网关来定义您想要使用的功能,并让网关处理交互与您想要使用的日志框架。这使得将来更改日志框架变得容易。

This might be somewhat simplified but here is my take on it.

  • When using the Facade pattern you provide the interface that others can use to talk to your application. Example: You have implemented some application that has multiple "modules", to make the access to the "modules" easier you create a Facade to make it easier to interact with the modules... a single point of contact.
  • When using the Gateway pattern you encapsulate some external part that you want to use. Example: you want to use logging but don't want to bound to a specific logging framework, in that case you can define your gateway that defines the functionality you want to use and let the gateway handle the interaction with the logging framework you want to use. This make it easy to change logging framework in the future.
山田美奈子 2024-10-14 05:33:56

以下是福勒书中的直接引用:

虽然 Facade 简化了更复杂的 API,但它通常由
通用服务的编写者。网关是由
客户的特定用途。此外,Facade 总是意味着
与其所覆盖的接口不同,而网关可以复制
完全包裹的立面,用于替换或测试
目的。

[第18章]

Here's the direct quote from Fowler's book:

While Facade simplifies a more complex API, it's usually done by the
writer of the service for general use. A Gateway is written by the
client for its particular use. In addition, a Facade always implies a
different interface to what it's covering, whereas a Gateway may copy
the wrapped facade entirely, being used for substitution or testing
purposes.

[Chapter 18]

吾家有女初长成 2024-10-14 05:33:56

我认为网关是外观的一个特例——外部系统上的外观。

I think Gateway is a specific case of Facade - a facade over an external system.

风吹过旳痕迹 2024-10-14 05:33:56

简单来说,Facade是一种设计模式,而Gateway是一种架构模式。

例如,应用程序网关是一种基础架构架构模式。该节点驻留在 DMZ 中,并将内部节点与只能连接到应用程序网关的外部客户端隔离。

当您考虑架构模式时,请考虑节​​点。当您考虑设计模式时,请考虑类/对象。

节点是以下内容的抽象:设备 - 硬件和系统软件 - 例如操作系统、平台/框架等。
系统软件被“分配”给设备。节点“封装”设备和系统软件,并与构成该体系结构的其他节点相关。

网关是将服务器节点与客户端节点隔离的节点——客户端节点不能直接连接到服务器节点。网关接收连接,然后自行建立到目标节点的连接。

Simply put, Facade is a design pattern while Gateway is an architectural pattern.

Application Gateway, for example, is an infrastructure architecture pattern. The node resides in DMZ and insulates internal nodes from external clients who can only connect to application gateway.

When you think about architecture patterns, think about nodes. When you think about design patterns, think about classes/objects.

Node is an abstraction of: device - hardware stuff and system software - e.g. OS, platform/framework etc.
System software is "assigned" to the device. Node "encapsulates" both device and system software and is related to other nodes comprising the architecture.

Gateway is a node that insulates server nodes from client nodes - client node cannot directly connect to a server node. Gateway receives the connection and then establishes connection itself to the destination node.

叹沉浮 2024-10-14 05:33:56

外观模式的主要价值是“简化”内部组件(外观后面)的使用。外观中的一个入口点或函数可能会使用内部组件的多个函数。如果网关带来了与“简化”API 或组件使用相同的价值,那么它可以被视为外观。在其他情况下,网关可以仅仅是架构的中间件、适配器、包装器或呼叫转发元件。或者网关可以扮演多种角色,例如简化一些流程、转发一些呼叫,同时也并行充当身份验证或授权中间件。因此,恕我直言,网关是一种高度抽象的模式,可以包含一个或多个特定的结构模式,例如外观、适配器、包装器、装饰器或中间件等。。

Martin Fowler 对网关的定义本质上是狭隘的(至少此处)并且更接近API网关,其作用类似于格式装饰器

就实现而言,网关可以做什么和不能做什么没有限制。它实际上是一个自己的应用程序,可以提供任何功能。

A facade pattern main value is to 'simplify' use of internal components (behind the facade). It could be so that one entry point or function in the facade will use multiple functions of the internal components. If a gateway is bringing the same value of 'simplifying' usage of APIs or components behind it then it could be considered a facade. In other situations, a gateway could be merely a middleware, adapter, wrapper or a call forwarding element of the architecture. Or a gateway could be wearing multiple hats, like simplifying few flows, forwarding some calls while acting as an authentication or authorisation middleware in parallel too. Thus, IMHO gateway is a highly abstract pattern that could encompass one or more specific structural patterns like facade, adapter, wrapper, decorator or middleware etc..

Martin Fowler definition of gateway is narrow in nature (at least the one here) and is closer to API Gateways acting like format decorators.

As far the implementation is concerned, there is no limit on what a gateways could and could not do. It is virtually an application of its own and could deliver any functionality.

骄傲 2024-10-14 05:33:56

外观用于处理某些对象的图形,就像单个对象一样,而网关用于连接两个不同的模块/系统。

Facade used for working with some Object's graph as with single object and Gateway for connecting two different modules/systems.

七色彩虹 2024-10-14 05:33:56

为了回答你的问题,我不会说Facade==Gateway,而是说Facade≈Gateway。我的意思是它们大致相等,根据上述不同意见,它们的不同之处尚不清楚。

您需要记住,设计模式和术语的关键组成部分通常是帮助更轻松地传达您的想法。话虽这么说,如果你总是用门面来说话,你就会有更大的机会被理解,因为这是最常用的术语。

To answer your question, I would not say that Facade==Gateway, but that Facade≈Gateway. By that I mean that they are approximately equal, how they differ is not apparently clear based upon the differing opinions above.

You need to remember that one of the key components of design patterns and terminology in general is to help more easily communicate your ideas. With that being said if you always speak in terms of a facade you will a greater chance of being understood as that is the term used most frequently.

2024-10-14 05:33:56

我倾向于将许多模式视为代理模式的特殊情况,并且不太担心具体是哪一种模式。

即:

  • Facade 是您的简单代理
    一堆复杂的类。

  • 适配器是部分的代理
    接口不兼容的系统
    正如我们现在需要的那样

  • etc...

从我在 Google 搜索“网关模式”中发现的内容来看,似乎 Gateway == Proxy :D

I tend to think of many of patterns as of special cases of Proxy pattern, and don't worry much which one specifically it is.

I.e:

  • Facade is your simple proxy to a
    bunch of complicated classes.

  • Adapter is a proxy to parts of the
    system with incompatible interfaces
    as the one we need at the moment

  • etc...

Judging from what I've found on a Google search for "gateway pattern" it seems that Gateway == Proxy :D

唱一曲作罢 2024-10-14 05:33:56

好吧,就像这样,DAO 是数据库的抽象,

facade 是 Web 服务的抽象,

两者都是网关

“网关是封装对外部系统的访问的类”

well, like this DAO is an abstraction of databases and

facade is an abstraction of web services

both are gateways

"gateway is a class encapsulating access to the external system"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文