Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 5,953 | Comments: 44,410

filter by tags archive

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.

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.

More posts in "Design patterns in the test of time" series:

  1. (21 Jan 2013) Mediator
  2. (18 Jan 2013) Iterator
  3. (17 Jan 2013) Interpreter
  4. (21 Nov 2012) Command, Redux
  5. (19 Nov 2012) Command
  6. (16 Nov 2012) Chain of responsibility
  7. (15 Nov 2012) Proxy
  8. (14 Nov 2012) Flyweight
  9. (09 Nov 2012) Façade
  10. (07 Nov 2012) Decorator
  11. (05 Nov 2012) Composite
  12. (02 Nov 2012) Bridge
  13. (01 Nov 2012) Adapter
  14. (31 Oct 2012) Singleton
  15. (29 Oct 2012) Prototype
  16. (26 Oct 2012) Factory Method
  17. (25 Oct 2012) Builder
  18. (24 Oct 2012) A modern alternative to Abstract Factory–filtered dependencies
  19. (23 Oct 2012) Abstract Factory

Comments

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

What about Guid.NewGuid() ?

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

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

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

DOes it mean all abstractions are infact Facade instances?

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

@bala: yes they are

Dan Turner

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

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

@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

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

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
  2. Special Offer (2):
    27 May 2015 - 29% discount for all our products
  3. RavenDB Sharding (3):
    22 May 2015 - Adding a new shard to an existing cluster, splitting the shard
  4. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  5. Interview question (2):
    30 Mar 2015 - fix the index
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats