1 / 12

Type Conformance and Closed Behavior

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

gordonb
Download Presentation

Type Conformance and Closed Behavior

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. Sales Department conductYearEndReview (Department) calcBonus calcBonus calcBonus Mary Jeff Employee (Manager) (Employee) An Example of Contravariance and Covariance Polymorphism!!

  9. Graphic Illustraction of Contravariance and Covariance Department conductYearEndReview Department conductYearEndReview perfEval bonusPct perfEval bonusPct

  10. 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

  11. 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

  12. 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

More Related