130 likes | 150 Views
Type Conformance and Closed Behavior. Constructing class hierarchies need to be careful two helpful principles: type conformance and closed behavior Class versus Type Principle of Type Conformance Principle of Closed Behavior. Type. Description of objects with the same characteristics
E N D
Type Conformance and Closed Behavior • Constructing class hierarchies • need to be careful • two helpful principles: type conformance and closed behavior • Class versus Type • Principle of Type Conformance • Principle of Closed Behavior
Type • Description of objects with the same characteristics • A type is the abstract or external view of a class • Type includes • class purpose • class invariant • class attributes • class operations, with preconditions, postconditions, definitions, and signatures
Class • The implementation of a type • A class includes the internal design details that implements external characteristics of the class (type) • A single type may be implemented as several classes • Figure 11.1 • <<type>> - the stereotype to denote type • <<implementation>> - denote relationship • {abstract} - no need for implementation
Class versus Type • The concept of subtype is distinct from that of subclass • a class can be a subclass of any other syntactically • legal class/subclass relationship may not be true type/subtype relationship • a subtype needn’t be a subclass • Object-oriented design principle • a class should be a true subtype of its superclass
The Principle of Type Conformance • Comes from theory of ADT • If S is a true subtype of T, then S must conform to T • an object of S can be provided in any context as objects of T • correctness of all accessor operations is preserved • example: Circle and Ellipse • Inheritance hierarchy should follow the principle of type conformance
Principles of Contravariance and Covariance • Ensure that the invariant of a subclass is at least as strong as that of its superclass • Three constraints on operations • every superclass operation has a corresponding operation in subclass with the same name and signature • precondition - no stronger than the corresponding superclass operation • postcondition - no weaker than the corresponding superclass operation • satisfied - if an operation is inherited
An Example of Contravariance and Covariance • Manager is a subclass of employee • Figure 11.2 • both has the operation calcBonus(perfEval,out bonusPct) • The legal ranges of Manager’s perfEval should be no larger than that of Employee • The legal ranges of Manager’s bonusPct should be no smaller than that of Employee
Sales Department conductYearEndReview (Department) calcBonus calcBonus calcBonus Mary Jeff Employee (Manager) (Employee) An Example of Contravariance and Covariance Polymorphism!!
Graphic Illustraction of Contravariance and Covariance Department conductYearEndReview Department conductYearEndReview perfEval bonusPct perfEval bonusPct
Requirements for Type Conformance • Apply to the whole class • state-space of a subclass must have the same dimensions as its superclass (may have additional dimensions by extension) • state-space of a subclass must equal to or lie within the state-space of its superclass • Apply to the corresponding operations • should have the same signature • preconditions - should be no stronger • postconditions - should be no weaker
Second-Order Design • When message arguments have level-2 encapsulation (object reference) • First-Order Design • pass arguments with level-1 encapsulation (functions) • Zeroth-Order Design • pass simply data • Second-order design is only for object-oriented design
The Principle of Closed Behavior • The execution of any operation of a class should obey its invariant (including the inherited operations) • To avoid wrongly modification • example: polygon and triangle • Possible corrective actions: • avoid inheriting modifier methods • override modifier methods • allow reclassification