Previous Table of Contents Next


The following sketch (from the book Object-Oriented Software Construction [Meyer, 1997]) illustrates these ideas on the example of scheduling the programs of a TV station. This is pure modeling of an application domain; no computers or software are involved yet. The class describes the notion of program segment.

Note the use of assertions to define semantic properties of the class, its instances, and its features. Although often presented as high-level, most object-oriented analysis methods (with the exception of Waldèn’s and Nerson’s BON) have no support for the expression of such properties, limiting themselves instead to the description of broad structural relationships:

   indexing
       description:”Individual fragments of a broadcasting schedule”
   deferred class
       SEGMENT
   feature  -- Access
       schedule: SCHEDULE is deferred end
                -- Schedule to which segment belongs
       index: INTEGER is deferred end
                -- Position of segment in its schedule
       starting_time, ending_time: INTEGER is deferred end
                -- Beginning and end of scheduled air time
       next: SEGMENT is deferred end
                -- Segment to be played next, if any
       sponsor: COMPANY is deferred end
                -- Segment’s principal sponsor
       rating: INTEGER is deferred end
                -- Segment’s rating (for children’s viewing etc.)
       Minimum_duration: INTEGER is 30
                -- Minimum length of segments, in seconds
       Maximum_interval: INTEGER is 2
                -- Maximum time between two successive segments, in seconds
   feature  -- Element change
       set_sponsor (s: SPONSOR) is
           require
               not_void: s /= Void
           deferred
           ensure
               sponsor_set: sponsor = s
           end
       ... change_next, set_rating omitted ...
   invariant
       in_list:(1 <= index) and (index <= schedule.segments.count)
       in_schedule: schedule.segments.item (index)= Current
       next_in_list:(next /= Void) implies
           (schedule.segments.item (index + 1)= next)
       no_next_iff_last:(next = Void)=(index = schedule.segments.count)
       non_negative_rating: rating >= 0
       positive times:(starting_time > 0) and (ending_time > 0)
       sufficient_duration: ending_time--starting_time>=Minimum_duration
       decent_interval:(next.starting_time)--ending_time<=Maximum_interval
   end

9.9.7. Structural Property Classes

An interesting category of deferred classes includes classes whose purpose is to describe a structural property, which may be useful to the description of many other classes. Typical examples are covered by classes of the kernel library:

  NUMERIC describes objects on which the arithmetic operations +, --, *, and / are available, with the properties of a ring (associativity, distributivity, zero elements, and so on). Kernel library classes such as INTEGER and REAL—but not, for example, STRING—are descendants of NUMERIC. An application that defines a class MATRIX may also make it a descendant of NUMERIC.
  COMPARABLE describes objects on which the comparison operations <, <=, >, and >= are available, with the properties of a total preorder (transitivity and irreflexivity). Kernel library classes such as CHARACTER, STRING and INTEGER—but not our MATRIX example—are descendants of NUMERIC.

For such classes, it is again essential to permit the inclusion of effective features in a deferred class and to include assertions. For example, class COMPARABLE declares infix “<” as deferred and expresses the other features effectively in terms of it. The type like Current is explained in section 9.9.16; it may be considered equivalent, in the following class text, to the type COMPARABLE:

   indexing
       description:”Objects that can be compared
           according to a total preorder relation”
   deferred class
       COMPARABLE
   feature  -- Comparison
       infix “<” (other: like Current): BOOLEAN is
                -- Is current object less than other?
           require
               other_exists: other /= Void
           deferred
           ensure
               asymmetric: Result implies not (other < Current)
           end
       infix “<=” (other: like Current): BOOLEAN is
                -- Is current object less than or equal to other?
           require
               other_exists: other /= Void
           do
               Result := (Current < other) or is_equal (other)
           ensure
               definition: Result = (Current < other) or is_equal (other)
           end
   ... Other features: infix “>”, min, max, ...
   invariant
       irreflexive: not (Current < Current)
   end  -- class COMPARABLE

Note how <= is defined in terms of < and >= in terms of <=.

9.9.8. Multiple Inheritance and Feature Renaming

It is often necessary to define a new class in terms of several existing ones:

  The kernel library classes INTEGER and REAL must inherit from both NUMERIC and COMPARABLE.
  A class TENNIS_PLAYER, in a system for keeping track of player ranking, inherits from COMPARABLE, as well as from other domain-specific classes.
  A class COMPANY_PLANE may inherit from both PLANE and ASSET.
  Class ARRAYED_LIST, describing an implementation of lists through arrays, may inherit from both LIST and ARRAY.


Previous Table of Contents Next