您的位置:首页 > Web前端

What is the difference between Facade and Gateway design patterns?

2016-12-16 01:18 931 查看


What
is the difference between Facade and Gateway design patterns?

up
vote30down
votefavorite
13

or Facade==Gateway?

design-patterns gateway facade
shareimprove
this question
asked Dec 12 '10 at 14:49





antz
154123

 
3 
You should accept an answer. :) – JoeCool Aug
23 '13 at 17:48
add
a comment


10 Answers

activeoldestvotes

up vote45down
vote
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.

shareimprove
this answer
answered Nov 21 '12 at 15:52





Brian Drummond
12.6k11427

 
 
That is indeed an important difference! Thanks for putting it in such a lucid way! – TheSilverBullet Nov
22 '12 at 8:07
 
If you can give me examples through simple classes/diagrams, I would vote the bounty for your answer, Brian... – TheSilverBullet Nov
26 '12 at 14:17
 
The problem with the Facade Pattern is that it cannot be localized and can only be applied to the architecture and NOT
the application. This creates problems in that anytime security, methods, or anything else has to be checked on a forwarded URL INTERNAL to the application, it has to GO BACK OUT through the facade through a new request and come back in. This same issue plagues
api gates as well. 'API Abstraction' however (slideshare.net/bobdobbes/edit_my_uploads)
answers this issue – Orubel Nov
2 '14 at 21:32 
 
and, in case of 'internals' to internals? lets say we have already gotten complex data structure and we need to extract
data from that structure? – someUser 2
days ago 
add
a comment
up vote19down
vote
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.

shareimprove
this answer
answered Jan 5 '11 at 14:06





djna
42.8k85384

 add
a comment
up vote16down
vote
+50

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 havesubsystems.

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 generalpattern
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]

shareimprove
this answer
edited Nov
27 '12 at 21:16

answered Nov 27 '12 at 21:06





Tengiz
3,9151324

 
 
Thanks for this. Facade is a specific case of gateway - I will explore further on this thought. If I may also add to
the gateway points, a gateway is used to decouple the external systems from our core system. It helps express the external API differently so that tomorrow another API can come in its place with minimum impact. It would greatly help me if you would concur
or correct. :) thanks again! Let me give you that bounty before it expires... – TheSilverBullet Nov
28 '12 at 2:44
 
Yes, Gateway can be used for transforming the external API into more convenient format. However, theoretically, don't
limit your understanding of Gateway to this. Well, the word "Gateway" itself tells you that this is just a gate[way] to somewhere, or yet another way to see things differently. Yes, you can replace Gateway implementation in the future, but that's not related
to Gateway pattern at all. I would call that "D" (Dependency Inversion) from SOLID principles. Call "wrapper" a Gateway and stop on that. Now, you're exactly right! – Tengiz Nov
28 '12 at 13:35
add
a comment
up vote5down
vote
I think Gateway is a specific case of Facade - a facade over an external system.

shareimprove
this answer
answered Dec 12 '10 at 14:51





Armand
8,303105194

 add
a comment
up vote3down
vote
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.

shareimprove
this answer
answered May 20 '11 at 8:58





Tomas Jansson
12.1k34486

 add
a comment
up vote3down
vote
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]

shareimprove
this answer
answered Jan 23 '13 at 18:41





JoeCool
2,18183661

 add
a comment
up vote2down
vote
Facade used for working with some Object's graph as with single object and Gateway for connecting two different modules/systems.

shareimprove
this answer
answered Dec 12 '10 at 14:52





Stas Kurilin
7,6231556109

 add
a comment
up vote1down
vote
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

shareimprove
this answer
answered Dec 12 '10 at 14:59





Goran Jovic
7,19332765

 add
a comment
up vote1down
vote
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.

shareimprove
this answer
answered Aug 17 '15 at 16:49





Bran
262

 
 
To spice it up a little bit more: Proxy is a design pattern, while Reverse Proxy is an architectural pattern – Bran Aug
17 '15 at 16:52
add
a comment
up vote0down
vote
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.

shareimprove
this answer
edited Nov
20 '14 at 23:18

answered Nov 20 '14 at 21:45





Rob
17125

 
1 
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment
below their post. – Ryan
Wheale Nov
20 '14 at 22:02
add
a comment
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: