Concepts+Techniques+and+Models+of+Computer+Programming_Part20

Concepts+Techniques+and+Models+of+Computer+Programming_Part20

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
528 Object-Oriented Programming class ListClass meth isNil(_) raise undefinedMethod end end meth append(_ _) raise undefinedMethod end end meth display raise undefinedMethod end end end class NilClass from ListClass meth init skip end meth isNil(B) B= true end meth append(T U) U=T end meth display {Browse nil} end end class ConsClass from ListClass attr head tail meth init(H T) head:=H tail:=T end meth isNil(B) B= false end meth append(T U) U2={@tail append(T $)} in U={New ConsClass init(@head U2)} end meth display {Browse @head} {@tail display} end end Figure 7.13: Lists in object-oriented style 7.4.2 Constructing a hierarchy by following the type When writing programs with recursion, we saw in Section 3.4.2 that it is a good idea to define first the type of the data structure, and then to construct the recursive program by following the type. We can use a similar idea to construct inheritance hierarchies. For example, consider the list type ± List T ² ,wh i chi s defined as: ± List T ² ::= nil | T ´ | ´ ± List T ² This says that a list is either nil or a list pair. Let us implement the list ADT in the class ListClass . Following the type definition means that we define two other classes that inherit from ListClass i chw ecanca l l NilClass and ConsClass . Figure 7.12 shows the hierarchy. This hierarchy is a natural design to respect the substitution principle. An instance of NilClass is a list, so it is easy to use it wherever a list is required. The same holds for ConsClass . Figure 7.13 defines a list ADT that follows this hierarchy. In this figure, ListClass is an abstract class : a class in which some methods are left unde- fined. Trying to call the methods isNil , append ,and display will raise an Copyright c ³ 2001-3 by P. Van Roy and S. Haridi. All rights reserved.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
7.4 Programming with inheritance 529 class GenericSort meth init skip end meth qsort(Xs Ys) case Xs of nil then Ys = nil [] P|Xr then SL in { self partition(Xr P S L)} {Append { self qsort(S $)} P|{ self qsort(L $)} Ys} end end meth partition(Xs P Ss Ls) case Xs of nil then Ss=nil Ls=nil [] X|Xr then Sr Lr in if { self less(X P $)} then Ss=X|Sr Ls=Lr else Ss=Sr Ls=X|Lr end { self partition(Xr P Sr Lr)} end end end Figure 7.14: A generic sorting class (with inheritance) exception. Abstract classes are not intended to be instantiated, since they lack some methods. The idea is to define another class that inherits from the ab- stract class and that adds the missing methods. This gives a concrete class , which can be instantiated since it defines all the methods it calls. NilClass and ConsClass are concrete classes. They define the methods isNil , append ,and display .Th ec a l l {L1 append(L2 L3)} binds L3 to the concatenation of L1 and L2 , without changing L1 or L2 .Theca l l {L display} displays the list. Let us now do some calculations with lists: L1={New ConsClass init(1 {New ConsClass init(2 {New NilClass init})})} L2={New ConsClass init(3 {New NilClass init})} L3={L1 append(L2 $)} {L3 display} This creates two lists L1 and L2 and concatenates them to form L3 .I tt h e n displays the contents of L3 in the browser, as 1, 2, 3, nil .
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 08/10/2011.

Page1 / 30

Concepts+Techniques+and+Models+of+Computer+Programming_Part20

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online