Applied Design Patterns with Java

Structural :: Facade (185) {C ch 13}

Implementation

Here are implementation issues to consider when using the Facade pattern:

  1. Reducing client-subsystem coupling. The coupling between clients and the subsystem can be reduced even further by making Facade an abstract class with concrete subclasses for different implementations of a subsystem. Then clients can communicate with the subsystem through the interface of the abstract Facade class. This abstract coupling keeps clients from knowing which implementation of a subsystem is used. An alternative to subclassing is to configure a Facade object with different subsystem objects. To customize the Facade, simply replace one or more of its subsystem objects.
  2. Public versus private subsystem classes. A subsystem is analogous to a class in that both have interfaces, and both encapsulate something - a class encapsulates state and operations, while a subsystem encapsulates classes. Just as it's useful to think of the public and private interface of a class, think of the public and private interface of a subsystem. The public interface to a subsystem consists of classes that all clients can access; the private interface is just for subsystem extenders. The Facade class is part of the public interface, but it's not the only part. Other subsystem classes are usually public as well (the classes Parser and Scanner in the compiler subsystem are part of the public interface).


Related Patterns

Abstract Factory (87) can be used with Facade to provide an interface for creating subsystem objects in a subsystem-independent way. Abstract Factory can also be used as an alternative to Facade to hide platform-specific classes.

Mediator (273) is similar to Facade in that it abstracts functionality of existing classes. Mediator's purpose is to abstract arbitrary communication between colleague objects, often centralizing functionality that doesn't belong in any one of them. A Mediator's colleagues are aware of and communicate with the Mediator instead of communicating with each other directly. In contrast, a Facade merely abstracts the interface to subsystem objects to make them easier to use; it doesn't define new functionality, and subsystem classes don't know about it.

Usually only one
Facade object is required. Thus Facade objects are often Singletons (127).

Catalog Structural Prev Next