Java Reference
In-Depth Information
The hierarchy partitions fireworks into types of fireworks but also includes
the FireworkSimulator subclass. A class's subclasses should usually not overlap.
In this example, it's easy to imagine a class that is both a simulator and, say, a rocket.
SOLUTION 26.5
Whether LSP applies in this case is a matter of judgment. LSP specifically maintains that
an object should function as an instance of its superclass. If you think of interfaces as pure
abstract classes, LSP implies that an instance of a class that implements an interface should
function as an "instance" of the interface. Given this interpretation of LSP,
the Arrays.asList() method, at least through JDK 1.4, breaks LSP.
It seems inconsistent to me to declare that a class implements an interface but then throw
an exception when some methods of the interface are called. An interface is a contract for
the behavior of the class. Exceptions to LSP should be rare, as should the use of
UnsupportedOperationException .
SOLUTION 26.6
To add observability to a class, you can provide it with an attribute of type Observable
from the java.util package or type PropertyChangeSupport from the java.beans
package. Add the operations you need to your class, and implement them by forwarding each
call to the attribute. For example, if you call your class's attribute changeSupport and
initialize it to a PropertyChangeSupport object, you can implement the method:
protected void firePropertyChange(
String propertyName,
Object oldValue,
Object newValue)
{
changeSupport.firePropertyChange(
propertyName,
oldValue,
newValue);
}
Note that you have no obligation to reimplement every operation from the class to which you
delegate.
SOLUTION 26.7
A. True . To provide operations that manipulate strings, the simplest approach is to create
a utility class with operations that accept a string and return a transformed version of
it.
B. True . Multiple references to OoString objects would be affected by one another's
changes to the object.
C. False . You can generally create simple subclasses of classes that expect strings. For
example,
Search WWH ::




Custom Search