Java Reference
In-Depth Information
Chapter 20. Introducing Operations
When you write a Java method, you produce a fundamental unit of work that is a level up
from writing a statement. Your methods have to participate in an overall design, architecture,
and test plan, but no activity is more central to programming than writing methods. Ironically,
despite the central role of methods, it is easy to get confused about what methods are and how
they function. You need to be aware of a few syntactic subtleties in Java methods. But more
confusing is the tendency of many developers and authors to slosh together the meaning of the
words method, operation , and algorithm . By distinguishing the meanings of these terms, you
can express important concepts that surface in many design patterns.
Having a clear understanding of what methods are will also help you understand several
design patterns. In particular, S TATE , S TRATEGY , and I NTERPRETER all work by
implementing an operation in methods across several classes, but such observations are useful
only if we agree on the meaning of method and operation.
Operations, Methods, and Algorithms
It is useful to distinguish operation from method and to then discuss these concepts' relations
to algorithm. Fortunately, the UML defines the difference between an operation and method
(Booch, Rumbaugh, and Jacobson 1999, p. 128).
An operation is a specification of a service that can be requested from an instance of a
class.
A method is an implementation of an operation. 1
An operation specifies something that a class does and specifies the interface for calling on
this service. Multiple classes may implement the same operation in different ways. For
example, many classes implement the toString() operation. Every class that implements
an operation implements it with a method, the code that makes the operation work for that
class.
The definitions of method and operation help to clarify the structure of many design patterns.
The meaning of operation is a level of abstraction up from the idea of method. Because design
patterns are also a level up from classes and methods, it is no surprise that operations feature
prominently in many patterns. For example, C OMPOSITE arranges for operations to apply to
both items and groups. P ROXY lets an intermediary that has the same operations as a target
object interpose itself to manage access to the target.
CHALLENGE 20.1
Explain how C HAIN OF R ESPONSIBILITY implements an operation.
If we agree on the UML definitions of these words, it is illuminating to say that a method is an
implementation of an operation. How, then, do operations and methods come together to
implement an algorithm? Before answering that, we should agree on what algorithm means.
1 For definitions of the related terms abstract method and static method , see the Glossary .
Search WWH ::




Custom Search