Design patterns in the test of timeFaçade
A façade is an object that provides a simplified interface to a larger body of code, such as a class library.
The intent of the façade was good: to wrap APIs that are poorly designed in something shiny and nice.
In the real world, however, it is an evil pattern that is used to needlessly add abstractions for no particular reason. For example, look at this article.
That sound you just heard is your architecture, it is hiding in the closet, weeping silent tears about emotional and physical abuse that just doesn’t end.
I have yet to see a real case were façade was actually used properly. In most cases, people built a façade because That Is How We Do Things. And because of that, they ended up with things like the one above. It adds exactly nothing, and it horrifyingly complicates the code.
Recommendation: Avoid this, you really don’t need to do this most of the time, and most implementations are bad.
More posts in "Design patterns in the test of time" series:
- (21 Jan 2013) Mediator
- (18 Jan 2013) Iterator
- (17 Jan 2013) Interpreter
- (21 Nov 2012) Command, Redux
- (19 Nov 2012) Command
- (16 Nov 2012) Chain of responsibility
- (15 Nov 2012) Proxy
- (14 Nov 2012) Flyweight
- (09 Nov 2012) Façade
- (07 Nov 2012) Decorator
- (05 Nov 2012) Composite
- (02 Nov 2012) Bridge
- (01 Nov 2012) Adapter
- (31 Oct 2012) Singleton
- (29 Oct 2012) Prototype
- (26 Oct 2012) Factory Method
- (25 Oct 2012) Builder
- (24 Oct 2012) A modern alternative to Abstract Factory–filtered dependencies
- (23 Oct 2012) Abstract Factory
Comments
One could argue whether High-Level APIs to complex object relationships aren't Facades, but I think the common term is Object Builder...
The example shown in the referenced article is not really a facade, so the problem is with the example and not the pattern itself. I would consider something like WebClient or WebRequest class as a good example of facade. Imagine working with raw sockets, authentication, compression, caching and other stuff, instead of using it
Would you consider a View Model from the .Net MVC world a facade? It seems to fit the pattern exactly.
@Charles Flatt I too think that View Model fits the pattern. But we should be careful when to use it.
I think it depends whether the actual interface of your application look exactly like the data structure you're using. If that is the case View Models and Facade bring no value. They just copy the data model.
However in most real applications, the interface of the application will almost never look like the data model, so View Models and Facade come in handy.
The View Model is not a Facade at all. It's an adapter. It adapts the model information into something that a view can use.
For instance a list of users are converted into a SelectListemItem array
TcpClient/TcpListener are facades making it easier to work with sockets. imho they are good examples of facades.
A facade doesn't have to hide complexity of several classes.
Another example is nhibernate ;) It hides the ADO.NET/DB plumbing
I was also thinking of ISession from NHibernate, which seems to me like a facade over the internal structure of NHibernate.
I think the main problem is that in the real world it is a pattern that is often misused and many things are incorrectly called a facade. A facade is supposed to provide a simplified interface over things like a library. In the ISession example, it hides all the co-ordination with the identity map, cache, database, persisters, change tracking, etc. This leaves the user with a single, simpler interface that matches their intent, get me an object, run this query, commit the transaction.
I agree that the problem with this post is the example, not the pattern. The example provides no value, that is true, but the pattern itself is very useful. Let's not throw the baby out with the bath water.
Come to think of it, most of the posts in this series end on a negative note about what not to do. The patterns have withstood the test of time for a reason, they work. The problem is most devs, myself included, are not properly informed/trained in how to apply them.
I think that's exactly the usefulness of these posts - the developers should not apply a pattern unless they truly understand when and why it's useful, not just because they heard from other people that it's cool to use patterns..
I agree with Zdeslav 100%. Don't really see the example as a Facade. Facades are useful if you need to interact with a large class library/API in a very limited and specific way. It's very useful and I don't see that I would give it up any time soon.
In my opinion, anything that hides complexity implements the Facade pattern. That means every class/method implements this pattern. Def: A facade is an object that provides a simplified interface to a larger body of code.
Agree with Zdeslav, WebRequest class is a way better example to show off the pattern
I'm guilty of implementing a façade to System.IO in FluentPath. It adds nothing in terms of functionality and is an additional abstraction layer, yes, but I would argue that beyond being a million times nicer to work with than the mess of static APIs that System.IO is, it also helps for testability because it's possible to mock the file system whereas the original API makes that very hard.
Web services or REST-style interfaces are often implemented as facades over underlying data types and request-scoped session management. Quite common and also correct.
In my eyes jQuery provides great facades to support most common browsers. I don't think the facade pattern is evil. Abuse is evil ;)
I think a good example of a facade is JsonConvert class in Json.NET. It hides away having to create a serializer and readers/writers and gives people 1 line methods to serialize and deserialize JSON strings, which is really what most people want.
What about Guid.NewGuid() ?
In fact, any method or object is a great example.
Guid.NewGuid() is a factory method (not the GOF factory pattern). It is responsible for creating Guids, not hiding a low-level API. Going by that logic, most methods hide some code complexity.
WebClient is an example of a facade over WebRequest/WebResponse.
Dmitry: NewGuid() is both facade and factory method. You say it does not hide low level api? Well..I strongly disagree.
Yes, most methods hide complexity and therefore implements facade pattern. All patterns are are subpatterns of Facade. Facade is the most powerful pattern - and most boring - which is awesome.
DOes it mean all abstractions are infact Facade instances?
One place where I felt a Facade came in useful was writing code against a IO card which accepted commands at a fairly low level (e.g. read/write bytes X to/from port Y). The facade allowed me to expose behaviour like EnableFoo(), DisableBar(), ReadBaz(). This is just the first example of where the usefulness of this pattern was obvious.
@bala: yes they are
@Chris Chilvers absolutely 110% spot on! I couldn't have said it better myself.
@lroal,
You are describing general abstractions. Abstraction is one of the OO principles meaning objects were invented to abstract code.
All GOF patterns are created for specific scenarios. Facade pattern is designed to abstract a low level API that is specific to your application, not HTML, TCP/IP or operating system calls.
See Oren's todays post.
@dmitry: Just define the right sub system (set of classes) and any abstraction will fit the pattern. It could be your own classes, an external lib or a mix.
I have seen code just like this too many times for it to be a sick joke.
Comment preview