View RSS Feed

My Java Tips

The Facade Design Pattern

Rate this Entry
by , 11-20-2011 at 07:05 PM (2386 Views)
The Facade Design Pattern is a structural design pattern amongst the design patterns. A facade is actually an object. It provides an uncomplicated interface to a larger body of code, for example a class library.

As a facade has simple and tailored methods for common tasks. A facade can make a software library easier to use and understand. A facade also makes the code that uses the library more readable. A facade assists in developing a more flexible system. It reduces dependencies of outside code on the inner workings of a library. According to the requirement, the Facade Design Pattern wrap a poorly designed collection of APIs with a single well-designed API.

Choosing between Facade and Adapter

There is a definite difference between the Facade and Adapter. A facade is a better choice when one wants an easier or simpler interface to work with. Where as the Adapter Design Pattern is used when wrapper must support a polymorphic behavior and wrapper must respect a particular interface.

Structure of the Facade Design Pattern

Facade: It hides the parts of a complicated API behind a more easy to understand and user friendly facade. The facade class interacts with different Packages with the rest of the application.

Clients: The objects using the Facade Pattern to access resources from the Packages.

Packages: Software library or API collection are accessed through the Facade Class.

The example below demonstrates use of facade design pattern. If you want to add a few days to a date, you would require to use a complicated calendar API with not that easy to understand code. With the help of facade design pattern you can write easy to understand and easy to use methods to add days you want to a date.

Java Code:
import java.text.*;
import java.util.*;

// This is is facade
class MyFacadeDate
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public MyFacadeDate (String isodate_ymd) throws ParseException {
        Date date = sdf.parse(isodate_ymd);

    public void addDays (int days) {
        cal.add (Calendar. DAY_OF_MONTH, days);

    public String toString() {
        return sdf.format(cal.getTime());

// This is a client
class FacadeDemo
    public static void main(String[] args) throws ParseException
        MyFacadeDate d = new MyFacadeDate("2005-09-03");
        System.out.println ("Date: " + d.toString());
        System.out.println ("20 days after: " + d.toString());
Date: 2005-09-03
20 days after: 2005-09-23

Submit "The Facade Design Pattern" to Facebook Submit "The Facade Design Pattern" to Digg Submit "The Facade Design Pattern" to Submit "The Facade Design Pattern" to StumbleUpon Submit "The Facade Design Pattern" to Google