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èns and Nersons 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 -- Segments principal sponsor rating: INTEGER is deferred end -- Segments rating (for childrens 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
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:
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 <=.
It is often necessary to define a new class in terms of several existing ones:
Previous | Table of Contents | Next |