Design Patterns Java™ Workbook

(Michael S) #1
Chapter 20. Introducing Operations

Summary......................................................................................................................................................


Although it is common to intermingle the meanings of operation, method, signature, and
algorithm, preserving distinctions among these terms helps to describe important concepts.
An operation is, like a signature, a specification of a service. The word operation applies
when talking about the idea that many methods may have the same interface. The word
signature applies when discussing method lookup rules. A method definition in-cludes its
signature—its name and parameter list—along with modifiers, return type, a throws clause,
and the method's body. A method has a signature and implements an operation.


An algorithm is a procedure that accepts inputs and produces outputs. Methods accept inputs,
produce outputs, and contain a procedural method body, so it is common to refer to a method
as an algorithm. However, an algorithm's procedure may involve many operations and
methods, or it may exist as part of another method. The word algorithm best applies when you
are discussing a procedure that produces a result.


Many design patterns involve distributing an operation across several classes. You can also
say that these patterns rely on polymorphism, the principle that method selection depends on
the class of the object that receives a method call. The decision of which method executes
depends on many rules and various aspects of method headers. In particular, it is important to
understand that the static modifier means that the decision of which method to execute
depends on the receiver's declared type, not the class of the object.


Methods execute until they return, unless they produce an exception. The potential for some
exceptions is so commonplace that methods need not declare them. Other, checked exceptions
must appear in a method's throws clause. The decision of whether an exception should be
checked or unchecked is a matter of judgment.


Beyond Ordinary Operations...................................................................................................................


Different classes can implement an operation in different ways. In other words, Java supports
polymorphism. The power of this seemingly simple idea appears in several design patterns.


If you intend to Apply the pattern



  • Implement an algorithm in a method, deferring the definition
    of some steps of the algorithm so that subclasses can redefine
    them

  • Distribute an operation so that each class represents a
    different state

  • Encapsulate an operation, making implementations
    interchangeable

  • Encapsulate a method call in an object

  • Distribute an operation so that each implementation applies
    to a different type of composition


Template Method
(Chapter 21)

State (Chapter 22)

Strategy (Chapter 23)

Command (Chapter 24)
Interpreter (Chapter 25)

Operation-oriented patterns address contexts in which you need more than one method,
usually with the same signature, to participate in a design. For example, the TEMPLATE

Free download pdf