Java Reference
In-Depth Information
CHALLENGE 7.4
In this situation, how could you prevent other developers from taking advantage of
protected fields by subclassing into classes in your package?
Summary
As a Java developer, you take on responsibility for creating classes that form a logical
collection of attributes and associated behaviors. You are also responsible for ensuring that
your methods perform the services implied by their names. You can limit your responsibility
with the proper use of visibility, but to be a developer implies that some of your code must be
available to other developers.
Beyond Ordinary Responsibility
Like developers, objects have certain responsibilities but in a different sense. Objects cannot
bear a moral, ethical, or professional responsibility, but we do say that object-oriented
development distributes responsibility to individual objects. This is primarily a way of
characterizing encapsulation, the idea that an object works on its own data. Object-oriented
development ordinarily distributes responsibility as far as it will go, meaning that each object
does its own work.
Distributed responsibility is the norm, but several design patterns oppose this and move
responsibility to an intermediary or to a particular object. A central figure may absorb certain
responsibilities, or you may also need policies for escalating requests to other authorities.
And, although objects are normally highly responsible, you may want to observe their
behavior without their knowing it. You can think of the intent of the O BSERVER pattern and
several other patterns as exceptions to the ordinary rule of distributed responsibility.
If you intend to
Apply the pattern
Centralize responsibility in a single instance of a class
Singleton (Chapter 8)
Decouple an object from awareness of which other
objects depend on it
Observer (Chapter 9)
Centralize responsibility in a class that oversees how a
set of other objects interact
Mediator (Chapter 10)
Let an object act on behalf of another object
Proxy (Chapter 11)
Allow a request to escalate up a chainof objects until one
handles it
Chain of Responsibility
(Chapter 12)
Centralize responsibility in shared, fine-grained objects
Flyweight (Chapter 13)
Search WWH ::




Custom Search