Ayende @ Rahien

It's a girl

Design patterns in the test of time: Façade

A façade is an object that provides a simplified interface to a larger body of code, such as a class library.

More about this pattern.

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.

Business Facade - Detailed View Data Access - Detailed View

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.

Comments

Frank Quednau
11/09/2012 10:34 AM by
Frank Quednau

One could argue whether High-Level APIs to complex object relationships aren't Facades, but I think the common term is Object Builder...

Zdeslav
11/09/2012 11:32 AM by
Zdeslav

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

Charles Flatt
11/09/2012 11:46 AM by
Charles Flatt

Would you consider a View Model from the .Net MVC world a facade? It seems to fit the pattern exactly.

Apostol
11/09/2012 12:14 PM by
Apostol

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

jgauffin
11/09/2012 12:34 PM by
jgauffin

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

jgauffin
11/09/2012 12:37 PM by
jgauffin

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

Chris Chilvers
11/09/2012 01:06 PM by
Chris Chilvers

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.

Jason Meckley
11/09/2012 01:20 PM by
Jason Meckley

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.

Tudor
11/09/2012 02:43 PM by
Tudor

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

J.P. Hamilton
11/09/2012 04:48 PM by
J.P. Hamilton

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.

@lroal
11/09/2012 05:37 PM by
@lroal

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.

Ale Miralles
11/09/2012 06:34 PM by
Ale Miralles

Agree with Zdeslav, WebRequest class is a way better example to show off the pattern

Bertrand Le Roy
11/09/2012 06:39 PM by
Bertrand Le Roy

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.

Rafal
11/09/2012 06:53 PM by
Rafal

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.

Falko Janak
11/09/2012 08:18 PM by
Falko Janak

In my eyes jQuery provides great facades to support most common browsers. I don't think the facade pattern is evil. Abuse is evil ;)

James Newton-King
11/09/2012 08:59 PM by
James Newton-King

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.

@lroal
11/09/2012 09:08 PM by
@lroal

What about Guid.NewGuid() ?

In fact, any method or object is a great example.

Dmitry
11/10/2012 02:56 AM by
Dmitry

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.

@lroal
11/10/2012 02:36 PM by
@lroal

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.

bala
11/11/2012 08:06 AM by
bala

DOes it mean all abstractions are infact Facade instances?

Joshka
11/11/2012 02:18 PM by
Joshka

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.

@lroal
11/11/2012 07:12 PM by
@lroal

@bala: yes they are

Dan Turner
11/12/2012 01:22 AM by
Dan Turner

@Chris Chilvers absolutely 110% spot on! I couldn't have said it better myself.

Dmitry
11/12/2012 07:30 PM by
Dmitry

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

@lroal
11/13/2012 04:28 PM by
@lroal

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

Nick
11/13/2012 07:39 PM by
Nick

I have seen code just like this too many times for it to be a sick joke.

Comments have been closed on this topic.