What is the difference between Facade and Gateway

2019-01-30 13:48发布

or Facade==Gateway?

11条回答
Evening l夕情丶
2楼-- · 2019-01-30 14:21

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.

查看更多
Anthone
3楼-- · 2019-01-30 14:22

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]

查看更多
Anthone
4楼-- · 2019-01-30 14:22

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.

查看更多
戒情不戒烟
5楼-- · 2019-01-30 14:24

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.

查看更多
Animai°情兽
6楼-- · 2019-01-30 14:26

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]

查看更多
做自己的国王
7楼-- · 2019-01-30 14:27

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

查看更多
登录 后发表回答