1 / 20

Object-Oriented Software Construction

Object-Oriented Software Construction. Bertrand Meyer. Lecture 6: Genericity. Genericity. Unconstrained LIST [G] e.g. LIST [INTEGER], LIST [PERSON] Constrained HASH_TABLE [G ―> HASHABLE] VECTOR [G ―> NUMERIC]. SET _OF_STOCKS. LIST_OF_ COMPANIES. LIST_OF_. PERSONS.

abirdwell
Download Presentation

Object-Oriented Software Construction

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. Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity OOSC - Summer Semester 2005

  2. Genericity Unconstrained LIST [G] e.g. LIST [INTEGER], LIST [PERSON] Constrained HASH_TABLE [G―> HASHABLE] VECTOR [G―> NUMERIC] OOSC - Summer Semester 2005

  3. SET_OF_STOCKS LIST_OF_COMPANIES LIST_OF_ PERSONS LINKED_LIST_OF_STOCKS Extending the basic notion of class Inheritance Abstraction Genericity Type parameterization Type parameterization LIST_OF_STOCKS Specialization OOSC - Summer Semester 2005

  4. SET_OF_PERSONS SET_OF_STOCKS LIST_OF_COMPANIES LIST_OF_ PERSONS LINKED_LIST_OF_COMPANIES LINKED_LIST_OF_STOCKS Extending the basic notion of class Inheritance Genericity LIST_OF_STOCKS OOSC - Summer Semester 2005

  5. Genericity: Ensuring type safety How can we define consistent “container” data structures, e.g. list of accounts, list of points? Dubious use of a container data structure: c : COMPANY a : PERSONcompanies: LIST ... people: LIST ... companies.extend (c) people.extend (a) c := people.last c.change_recommendation (Buy) OOSC - Summer Semester 2005

  6. Possible approaches • Wait until run time; if types don’t match, trigger a run-time failure. (Smalltalk) • Cast to a universal type, such as “pointer to void” in C. • Duplicate code, manually or with help of macro processor. • Parameterize the class, giving an explicit name G to the type of container elements. This is the Eiffel approach. OOSC - Summer Semester 2005

  7. Formal generic parameter Actual generic parameter A generic class classLIST [G] featureextend (x: G)is... last: Gis... end To use the class: obtain a generic derivation, e.g. companies: LIST [COMPANY] OOSC - Summer Semester 2005

  8. Conformance rule • B [U] conforms to A [T]if and only if B is a descendant of A and U conforms to T. OOSC - Summer Semester 2005

  9. Using generic derivations companies: LIST [COMPANY] people: LIST [PERSON] c: COMPANY p: PERSON ... companies.extend (c) people.extend (p) c := companies.last c. change_recommendation (Buy) ... OOSC - Summer Semester 2005

  10. Genericity and static typing Compiler will reject people.extend (c)companies.extend (p) To define more flexible data structures (e.g. stack of figures): use inheritance, polymorphism and dynamic binding. OOSC - Summer Semester 2005

  11. Typing in an O-O context An object-oriented language is statically typed if and only if it is possible to write a “static checker” which, if it accepts a system, guarantees that at run time, for any execution of a feature call x.f, the object attached to x (if any) will have at least one feature corresponding to f. OOSC - Summer Semester 2005

  12. Constrained genericity class VECTOR [G ] feature infix "+" (other: VECTOR [G]): VECTOR [G] is -- Sum of current vector and other require lower = other.lower upper = other.upper local a, b, c: G do ... See next ... end ... Other features ... end OOSC - Summer Semester 2005

  13. = i c a b + Adding two vectors u v = w + OOSC - Summer Semester 2005

  14. Constrained genericity Body of infix "+": createResult.make (lower, upper) from i := lower until i > upper loop a := item (i) b := other.item (i) c := a + b-- Requires “+” operation on G! Result.put (c, i) i := i + 1 end OOSC - Summer Semester 2005

  15. The solution Declare class VECTOR as classVECTOR [G –> NUMERIC] feature ... The rest as before ... end Class NUMERIC (from the Kernel Library) provides features infix"+", infix"*" and so on. OOSC - Summer Semester 2005

  16. Improving the solution Make VECTOR itself a descendant of NUMERIC, effecting the corresponding features: classVECTOR [G –> NUMERIC] inherit NUMERIC feature ... The rest as before, including infix "+"... end Then it is possible to define v : VECTOR [INTEGER] vv : VECTOR [VECTOR [INTEGER]] vvv : VECTOR [VECTOR [VECTOR [INTEGER]]] OOSC - Summer Semester 2005

  17. A generic library class: Arrays Using arrays: a: ARRAY [REAL] ...createa.make (1, 300) a.put (3.5, 25) x := a.item (i) -- Alternatively: x := a @ i -- Using the function infix "@" Also: ARRAY2 [G] etc. OOSC - Summer Semester 2005

  18. Class ARRAY (1) classARRAY [G] create make feature lower, upper: INTEGER count: INTEGER make (min: INTEGER, max: INTEGER) is-- Allocate array with bounds min and max.do ... end OOSC - Summer Semester 2005

  19. Class ARRAY (2) item, infix "@" (i: INTEGER): Gis-- Entry of index irequirelower <= i i <= upperdo ... endput (v: G; i: INTEGER) is-- Assign the value of v to the entry of index i.requirelower <= i i <= upperdo ... end invariantcount = upper – lower + 1 end OOSC - Summer Semester 2005

  20. End of lecture 6 OOSC - Summer Semester 2005

More Related