The goal of the Façade Design Pattern is to encapsulate a set of the complex process / rules. In other word, façade works like a manager. Suppose that you are a president of your company, you are going to hold a meeting with the vendors to discuss something. You just tell the manager, "I am going to hold a meeting, please arrange the schedule with the vendors". Then, the manager will take care of the more details and specific point. He'll book a meeting room, set the schedule with the vendors, prepare the material or proposal, tell the secretary to prepare others need, etc.
Now, I am going to give you another scenario. Suppose that, a customer that want to loan some money at a mortgage. Here's customer class:
class
Customer
{
private
string name;
// Constructor
public Customer(string name)
{
this.name = name;
}
// Property
public
string Name
{
get{ return name; }
}
}
To determine, whether the customer is eligible to loan some amount of money, the mortgage have to check with some parties. For example, the mortgage has to make sure that if the customer has sufficient savings in bank:
class
Bank
{
public
bool HasSufficientSavings(Customer c, int amount)
{
Console.WriteLine("Check bank for " + c.Name);
return
true;
}
}
Make sure that the customer has good credit:
class
Credit
{
public
bool HasGoodCredit(Customer c)
{
Console.WriteLine("Check credit for " + c.Name);
return
true;
}
}
Moreover, we have to make sure that the customer has no bad load.
class
Loan
{
public
bool HasNoBadLoans(Customer c)
{
Console.WriteLine("Check loans for " + c.Name);
return
true;
}
}
If the customer passes all these rules, he will be eligible to have his loan. To determine if the customer is eligible, we (in client code) don't have to check it one by one. Instead, let the mortgage do it. (Mortgage acts like Façade).
class
Mortgage
{
private
Bank bank = new
Bank();
private
Loan loan = new
Loan();
private
Credit credit = new
Credit();
public
bool IsEligible(Customer cust, int amount)
{
Console.WriteLine("{0} applies for {1:C} loan\n",
cust.Name, amount);
bool eligible = true;
// Check creditworthyness of applicant
if (!bank.HasSufficientSavings(cust, amount))
{
eligible = false;
}
else
if (!loan.HasNoBadLoans(cust))
{
eligible = false;
}
else
if (!credit.HasGoodCredit(cust))
{
eligible = false;
}
return eligible;
}
}
From the client code, all we have to do is:
class
MainApp
{
static
void Main()
{
// Facade
Mortgage mortgage = new
Mortgage();
// Evaluate mortgage eligibility for customer
Customer customer = new
Customer("Ann McKinsey");
bool eligable = mortgage.IsEligible(customer,125000);
Console.WriteLine("\n" + customer.Name +
" has been " + (eligable ? "Approved" : "Rejected"));
// Wait for user
Console.Read();
}
}
Now, we just call mortgage.IsEligible(customer, 125000) to evaluate it. The mortgage will just return us true or false. We don't have to evaluate by ourselves.
That's the example of façade pattern.
In ASP.NET 1.0, Microsoft introduced Repeater, a control to display data. The good sides of the repeater are simple, flexible, and light heavyweight; it just renders what we type. Unfortunately, repeater is too poor and too simple. It just tries to repeat a list of collection / array without any functionality. For instance, if we want to have paging and sorting, we have to code it manually. Furthermore, we have to build the template by ourselves.
ASP.NET 2.0 tried to build a powerful control, the GridView. GridView is cool. We can do Selecting, Updating, Deleting, Sorting, Paging, Auto formatted, etc in 30 seconds (I have been doing the demo to my audience quite often :D). Behind this powerful and rich data control, GridView received so critics. Yes, GridView is quite heavy. It takes some the process time in web server to render the records. The rendering process results some inappropriate html tags. Moreover, GridView is actually intended to display records in tabular way.
To answer this scenario, Microsoft tries to combine the light heavyweight Repeater and the powerful GridView. And the answer is ListView. ListView provides flexibility, simple, not too heavy, and powerful way to display the data. There are several advantages of ListView:
- ListView is lighter than GridView
- It just renders what we type in template field
- We can either design the template by ourselves or use the existing layout from the ListView
- We can sort the ListView
- Select, Insert, Update, Delete
- If we need Paging, we can integrate it with DataPager.
- Compatible with any datasource : LinqDataSource, SqlDataSource, etc
- It supports grouping
- etc
I am a basketball maniac. NBA is the site that I visit regulary. Usually it uses flash to display some picture / info.
But today, suddenly. i noticed that
http://www.nba.com/games/20080408/PHXMEM/gameinfo.html 
Seems like, Silverlight would have quite good future.