Java Reference
In-Depth Information
CHALLENGE 2.5
How can you change the secureOrder() method and its class to take advantage
of the ClassificationConstants interface?
A Java interface is essentially a purely abstract class, with the provision that a class can
implement multiple interfaces. If you think of it in this light, it is logical that a class "inherits"
the constants defined in any interface the class implements.
Summary
The basic purpose of a Java interface is to declare a set of methods that a class implements.
This usually implies that the class provides the services that the method names suggest. An
exception to this responsibility occurs when the interface lets an object register for event
notification. In this situation, the client of the interface bears responsibility for calling the
interface methods when the events implied by the method names occur. When you create a
registration interface, it is useful to pair the interface with an abstract class that provides
empty implementations of the interface methods, simplifying registration. Interfaces and
classes can also collaborate in the use of constants, with interfaces providing better
readability.
Beyond Ordinary Interfaces
You can simplify and strengthen your designs with appropriate application of Java interfaces.
Sometimes, though, the design of an interface has to go beyond the ordinary definition and
use of an interface.
If you intend to
Apply the pattern
Adapt a class's interface to match the interface a client expects
Adapter(Chapter 3)
Provide a simple interface into a collection of classes
Facade(Chapter 4)
Define an interface that applies to individual objects and groups
of objects
Composite(Chapter 5)
Decouple an abstraction from its implementation so that the
two can vary independently
Bridge(Chapter 6)
The intent of each design pattern is to solve a problem in a context. Interface-oriented patterns
address contexts in which you need to define or to redefine access to the methods of a class or
a group of classes. For example, when you have a class that performs a service you need but
with method names that do not match a client's expectations, you can apply the A DAPTER
pattern.
Search WWH ::




Custom Search