Page 167 - DCAP308_OBJECT_ORIENTED_ANALYSIS_AND_DESIGN
P. 167
Unit 13: Class Design
define the public protocol between the associated classes in the application. This is a set of Notes
messages that a class of a certain generic type must understand, although the interpretation and
implementation of each message is up to the individual class.
A class also might have a set of methods that it uses only internally, messages to itself. Thus, the
private protocol (visibility) of the class includes messages that normally should not be sent
from other objects; it is accessible only to operations of that class. In private protocol, only the
class itself can use the method. The public protocol (visibility) defines the stated behavior of the
class as a citizen in a population and is important information for users as well as future
descendants, so it is accessible to all classes.
If the methods or attributes can be used by the class itself or its subclasses; a protected protocol
can be used.
Did u know? In a protected protocol (visibility), subclasses can use the method in addition
to the class itself.
Lack of a well-designed protocol can manifest itself as encapsulation leakage. The problem of
encapsulation leakage occurs when details about a class’s internal implementation are disclosed
through the interface. As more internal details become visible, the flexibility to make changes
in the future decreases. If an implementation is completely open, almost no flexibility is retained
for future carefully controlled. However, do not make such a decision lightly because that could
impact the flexibility and therefore the quality of the design.
Example: Public or protected methods that can access private attributes can reveal an
important aspect of your implementation. If anyone uses these functions and you change their
location, the type of attribute, or the protocol of the method, this could make the client application
inoperable.
Design the interface between a superclass and its subclasses just as carefully as the class’s interface
to clients; this is the contract between the super- and subclasses. If this interface is not designed
properly, it can lead to violating the encapsulation of the superclass. The protected portion of
the class interface can be accessed only by subclasses. This feature is helpful but cannot express
the totality of the relationship between a class and its subclasses.
Other important factors include which functions might or might not be overridden and how
they must behave. It also is crucial to consider the relationship among methods. Some methods
might need to be overridden in groups to preserve the class’s semantics.
!
Caution Design your interface to subclasses so that a subclass that uses every supported
aspect of that interface does not compromise the integrity of the public interface.
The following sections summarize the differences between these layers.
13.2.3 Private and Protected Protocol Layers: Internal
Items in these layers define the implementation of the object. Apply the design axioms and
corollaries to decide what should be private: what attributes (instance variables)? What methods?
Remember, highly cohesive objects can improve coupling because only a minimal amount of
essential information need be passed between object.
LOVELY PROFESSIONAL UNIVERSITY 161