How to Use the Façade Pattern in Java

By Doug Lowe

The InetAddress class is an example of a commonly used design pattern in Java called the Façade pattern. In this pattern, you use a single class to hide the details of a complex object or set of related classes.

The InetAddress class combines the representation of an IP address with a variety of tasks that are commonly associated with IP addresses, such as using DNS to get the IP address based on a host name. (An even better example of the Façade pattern is the URL class, which provides a single class to access the complex features of URLs.)

The Façade pattern is commonly used in Java programming, especially in systems that need to incorporate access to complicated existing systems. Suppose that you have a fully integrated order-handling system in place, and you want to provide a way for web users to order directly from your company via this system.

Unfortunately, the existing system is way too complicated for your customers to use directly. It’s designed to be used by trained sales personnel and provides access to many sales features that your customers don’t need and shouldn’t be exposed to.

One way to do this is to create a facade for the ordering system. This facade includes an interface that provides just the classes and methods needed to implement your ordering system online, hiding as much of the complexity of the real ordering system as possible.