This post gives a brief overview about the Facade Pattern. The post is part of a series about software design patterns and their UML representations with the help of PlantUML.

The article aims at providing a very short description of the general idea of the pattern in the first part. This also involves a descriptive UML diagram. Then, the second part provides the PlantUML code for the diagram so that these posts can also be used as a source of design patterns in PlantUML syntax.

What is the Facade Pattern?

According to Wikipedia, a facade can:

  • make a software library easier to use, understand, and test, since the facade has convenient methods for common tasks
  • make the library more readable, for the same reason
  • reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system
  • wrap a, subjectively, poorly-designed collection of APIs with a single well-designed API

Implementors often use the facade design pattern when a system is very complex or difficult to understand because the system has a large number of interdependent classes or because its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client.

What problems can the Facade design pattern solve?

  • To make a complex subsystem easier to use, a simple interface should be provided for a set of interfaces in the subsystem.
  • The dependencies on a subsystem should be minimized.

Clients that access a complex subsystem directly refer to (depend on) many different objects having different interfaces (tight coupling), which makes the clients hard to implement, change, test, and reuse.

What solution does the Facade design pattern describe?

Define a Facade object that

  • implements a simple interface in terms of (by delegating to) the interfaces in the subsystem and
  • may perform additional functionality before/after forwarding a request.

This enables to work through a Facade object to minimize the dependencies on a subsystem.

UML Diagram

The following diagram shows the Facade Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:

PlantUML Syntax:</p><p>@startuml<br /> class Client<br /> class Facade<br /> Client -> Facade</p><p>package “Subsystem Classes” <<rectangle>> {</p><p>note “More complex subsystem” as n1</p><p>Facade — “class 1”<br /> Facade — “class 2”</p><p>package “class 1” <<rectangle>> {<br /> }</p><p>package “class 2” <<rectangle>> {<br /> }</p><p>package “class 3” <<rectangle>> {<br /> }</p><p>package “class 4” <<rectangle>> {<br /> }</p><p>package “class 5” <<rectangle>> {<br /> }</p><p>package “class 6” <<rectangle>> {<br /> }</p><p>“class 1” — “class 2”<br /> “class 1” — “class 3”<br /> “class 2” — “class 4”<br /> “class 3” — “class 5”<br /> “class 3” — “class 6”<br /> “class 4” — “class 5”<br /> “class 4” — “class 6”<br /> }</p><p>note left of Client<br /> Happy client whose<br /> job just became<br /> easier because of<br /> the facade.<br /> end note</p><p>note right of Facade<br /> Unified interface<br /> that is easier to use.<br /> end note<br /> @enduml</p><p>

PlantUML Sources

PlantUML is a tool allowing users to create UML diagrams from a plain text language. Here are the PlantUML sources for the above software pattern:


@startuml
class Client
class Facade
Client -> Facade

package "Subsystem Classes" <<rectangle>> {

note "More complex subsystem" as n1

Facade -- "class 1"
Facade -- "class 2"

package "class 1" <<rectangle>> {
}

package "class 2" <<rectangle>> {
}

package "class 3" <<rectangle>> {
}

package "class 4" <<rectangle>> {
}

package "class 5" <<rectangle>> {
}

package "class 6" <<rectangle>> {
}

"class 1" -- "class 2"
"class 1" -- "class 3"
"class 2" -- "class 4"
"class 3" -- "class 5"
"class 3" -- "class 6"
"class 4" -- "class 5"
"class 4" -- "class 6"
}

note left of Client
Happy client whose
job just became
easier because of
the facade.
end note

note right of Facade
Unified interface
that is easier to use.
end note
@enduml

Other Design Patterns

In another article you find information about how to put together a single-side web application using PlantUML.

[display-posts category=”Software Design Patterns in PlantUML” include_excerpt=”true” excerpt_length=”20″ excerpt_more=”… Read More” excerpt_more_link=”true” image_size=”thumbnail” wrapper=”div”]