Facade Pattern

A common design goal is to minimize the communication and dependencies between subsystems. [GoF, "Design Patterns"]

As you can see from the simplified diagram below the facade pattern is one way to achieve this, it provides a unified class which reduces communication between the complex subsystem and its clients. Clients do not have to be aware of the actual implementation of the subsystem and are provided with a simpler interface to this subsystem.

usage of the facade pattern in UML

Applicability / Uses

Use the facade pattern when:

  • you want to provide a simple interface to a complex subsystem. The application of design patterns often results in a lot of small classes which makes subsystems more flexible and customizable. A facade can provide a default view for clients which don't need to customize.
  • you want to reduce coupling between clients-subsystems or subsystems-subsystems.
  • you want to layer your subsystems. Use a facade to define an entry point to each subsystem level and make them communicate only through their facades, this can simplify the dependencies between them.

Related Patterns

  • Abstract Factory can be used with Facade to provide an interface for creating subsystem objects.
  • Facades are often implemented as Singletons when only one facade per subsystem is needed/desired.
  • Mediator is similar to Facade in that in abstracts functionality of existing classes. [GoF, "Design Patterns"].

    However while a Mediator centralizes functionality which doesn't belong in its colleague objects, a Facade does not define new functionality and the subsystems classes don't need to know anything about the facade object(s).

  • Facade is very similar to Adapter, they are both wrappers and while you will often find facades wrapping multiple objects and adapters wrapping single objects, keep in mind this is not the difference between them.

    The intent of Facade is to produce a simpler interface, and the intent of Adapter is to design to an existing interface. [Alan Shalloway and James Trott, "Design Patterns Explained"]


The sample code is based on the JavaWorld Article "Facade clears complexity", by David Geary, 05/30/03. We will be taking a look at the class javax.swing.JOptionPane, which is a Facade class, it provides a simple interface to different Dialogs (Confirm, Input, Message and OptionDialogs) of the Java Swing subsystem.

UML diagram of the facade pattern in a Java Swing/JOptionPane context.


First lets look at how to create our own Dialog box without the use of JOptionPane:

        JFrame frame = new JFrame("My Message Dialog");
        JButton button = new JButton("Quit");
        JLabel label = new JLabel("This is My Message Dialog!");

        ActionListener actionListener = new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {

        Container contentPane = frame.getContentPane();
        contentPane.add(label, BorderLayout.CENTER);
        contentPane.add(button, BorderLayout.SOUTH);
        frame.setSize(300, 200);

The code above creates a new JFrame and adds a new JButton and JLabel object to its ContentPane. It also defines an Actionlistener for the Exit button. Please note that we have not done any visual improvements here except use the default BorderLayout from JFrame's ContentPane. When the above code is compiled and run, the JFrame looks like this:

Screenshot of the executed code above.


Now lets take a look at doing this with the help of the JOptionPane Facade class. The same goal as above is accomplished - a pop-up Message Dialog box, however this time with better visual formatting, some eyecandy and less lines of code:

        JOptionPane optionPane = new JOptionPane(
                "This Dialog is generated by the JOptionPane Facade!",
        JDialog dialog = optionPane.createDialog(frame.getContentPane(),
                "JOptionPane Message Dialog");

Alternatively, you can also use the static method provided by JOptionPane to create the Message Dialog:

                "JOptionPane Message Dialog",
                "Error!", JOptionPane.ERROR_MESSAGE);

The resulting pop-up Message Dialog looks like this:

Screenshot of the executed code above.

As you can see, the JOptionPane Facade in Java Swing provides an easy interface to programmers who simply want to create a pop-up Message Dialog.

References / More Info

Design Patterns, Elements of Reusable Object-Oriented Software, Gang of Four

data & object factory

JavaWorld - "Facade clears complexity", by David Geary, 05/30/03