What's a Facade, Anyway?

Published 24 February 08 11:14 PM | adrian

Some years ago, Norman wished that we conduct a tech talk with a real architect to get the real feel of what an architect does. Well, we never did that, but there's something (actually, a lot) you can learn from Singaporean architecture related to software engineering.

Right now I'm talking about a facade. I'm sure you all have heard or used this pattern. I've heard this once, and then again recently. Both are bad design, IMO. In both design, a business facade is merely a middleman between web service and O/RM. The facade was doing nothing other than being another layer of indirection (which is bad, if done overly). Imagine something like this:

Bad BF

Bad BF 2 

Object has a collection of ObjectB which needs to be populated on load (eager-loading). Every time an Object is retrieved from the web service, the client will need to call the second web service (that retrieves the collection of ObjectB). All calls made by the web service could've gone directly to O/RM, but in this case, we add another layer between them. We don't need it.

So what's a facade, anyway?

An example of a very distinctive facade is an old cinema redeveloped just downtown Singapore.

The Cathay

This is The Cathay. You can find the history of the building on Wikipedia, this is not a history blog.

Back to the topic, you can see the brown, art-deco style part on the center, this is called the facade of the building. It's the front face of the building, covering the massive construction behind it. The idea of a facade is an indirection so that you don't need to deal with the complexity of logic. Security check on the facade doors, for example, will almost screen all visitor to the building.

Taking the analogy to software engineering, a facade should hide the complexity of the logic, too. Fixing the diagram above, it should look like this:

Good BF

Now, the client will only need to call one web service to retrieve Object and all related ObjectB. The facade becomes a composition orchestrator. The facade have the knowledge of what objects required when retrieving another object, the facade have the knowledge of how to retrieve it, too. A facade can call more than one O/RM function, can include logic, but should not call another facade function.

Now that you know have learnt a good facade by example of a physical building architecture, I invite you to start using this analogy when creating a facade. Please save us, developers, from the need of writing a useless middleman code. Thank you.

Share this post: | | | |

Comments

# norman said on February 25, 2008 10:27 AM:

The goal of Facade is to hide complexity. Shield user code from underlying complexity.

Another name for Facade might be Manager. You ask the Manager to do something, then that Manager will coordinate his men to do your task. Many of his men maybe involved, but you don't care. You just deal with the Manager. Then, it's up you to design how many Managers are needed in your org. :) Too many Managers then it means no men are doing the real work. These men are the Domain Objects in your app.

As most of Manager, they do nothing, but delegate the task to lower level men. Okay, coordinate the task of many men. :)

In your case, one building needs only one Facade. But if you're building interconnected buildings, each face may need its own facade.

Ah, I'm still hoping that Software Architect can learn from Building Archtects.

# Maximilian Haru Raditya said on February 25, 2008 09:37 PM:

Though it might have (almost) the same understanding, I prefer to call this facade as a "shell". A shell provides a encapsulation of the complex systems inside it.

The idea, Norman brought, of multiple facades per building face, I can think of it as the shell's interfaces.

# Joe said on February 28, 2008 02:00 PM:

Menarik juga pembahasannya. Tolong dibahas juga dong tentang scafolding.

# Jemmy said on August 1, 2008 10:23 AM:

Menarik... apakah konsep facade itu juga berlaku pada winform application?

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: