Course Software Engineering Design I Classes and Objects

Course: Software Engineering - Design I Classes and Objects Aims and Objectives ØDefine: • Class schemas to specify object classes (i. e. “types” of objects). » models are possible object instances of a class. • Bag-schemas to specify collections of object instances of a given class. » models are collections of object instances i. e. bags of models of a class schema. Ø Illustrate these two types of schema using a Library example. Unit 3: Classes and Objects 1

Course: Software Engineering - Design I Classes • Class, in object-oriented approach, specifies: – object’s variables – “state” (i. e. “type”) of objects; – methods – “operations” on objects of a given type. • Overall system is more complicated: – – a set of objects each object has a type is a model of its class schema system operations can operate on individual objects … or can change collection of objects (e. g. create, destroy) Class schemas for types of objects Ø Operation schemas for operations on objects. Ø “Bag-schemas” for bags or multisets of objects Ø Unit 3: Classes and Objects 2

Course: Software Engineering - Design I Bags … half way between sets and lists. • In a list: order and multiplicity all matter. – [3, 5], [5, 3], [3, 3, 5] are all different lists • In a set: order and multiplicity make no difference – {3, 5}, {5, 3}, {3, 3, 5}are all the same set • In a bag: multiplicity matters, order doesn’t. – {|3, 5|}, {|5, 3|} are the same bag, – {|3, 3, 5|} is different The system has bag of models of class schemas, as collection of different objects of the same class. Unit 3: Classes and Objects 3

Course: Software Engineering - Design I How to define a bag e. g. A bag of integers: Define a set X (“indexing set”) and a family of integers indexed by X. e. g. – a={|3, 5|}: X any 2 -element set, e. g. {Harry, Tom} a. Harry = 3, a. Tom = 5, where “a” denotes the bag – b={|3, 3, 5|}: X any 3 -element set, e. g. {Tom, John, Harry} b. Tom= b. John= 3, b. Harry= 5, where “b” denotes the bag Why the funny X’s? Why not just {1, 2} or {1, 2, 3}? – Just as example to emphasize that X does not have to have any intrinsic ordering – if it did we’d just have lists. Unit 3: Classes and Objects 4

Course: Software Engineering - Design I About the indexing set • Indexing set X provides “individual identities” for elements of bag. » cf. addresses of objects: X = {addresses used} • Two different objects may have same type, but they’ll still be distinguished by their different addresses. • Finite bag – when X is finite. Unit 3: Classes and Objects 5

Course: Software Engineering - Design I Schemas whose models are bags Assume a set of elements Y and an indexing set X, then: Fin. Bag(Y) [X] X 0: FX f: X Y x: X. x X 0 Bag(Y) [X] f: X Y Model = bag of elements Y. Model = finite bag of elements Y. What’s X 0 for? • It’s just a trick to make sure X is finite: – X 0 is declared as a finite subset of X; – the axiom says X is a subset of X 0. – Hence X 0 = X and both are finite. Unit 3: Classes and Objects 6

Course: Software Engineering - Design I Bags of models of schemas » What we saw before was bags of elements of sets. » Given one schema S, we can define a new “bagschema” whose models are bags of models of S. How to define them » Simple, when S does not include any base sort. – Add a new base sort X denoting the indexing set. – Constants become functions from X. – Functions and predicates gain an extra argument of sort X. Unit 3: Classes and Objects 7

Course: Software Engineering - Design I Example: a library Making many simplifying assumptions: • Books are described by ISBNs (International Standard Book Numbers) • Library may have more than one copy of the same book. • Each copy has a location comprising a library (e. g. central or departmental) and a shelf-mark. • Some copies may be identical twins of each other (same book, same location) but they are still distinct. • Borrowers have id codes and upper limit on no. books they can borrow. Unit 3: Classes and Objects 8

Course: Software Engineering - Design I Copies as objects in a class schema • LOCATION = LIBRARY SHELFMARK – (e. g. Library = {Maths, Physics, …} – SHELFMARK = … whatever. – We don’t need to know the details. ) • COPYSTATUS = {in, out} Copy isbn: seq. CHAR location: LOCATION status: COPYSTATUS Unit 3: Classes and Objects 9

Course: Software Engineering - Design I Initialization, operations on copies New. Copy isbn? : seq. CHAR location? : LOCATION isbn = isbn? location = location? status = in Unit 3: Classes and Objects Move. Copy DCopy to? : LOCATION isbn' = isbn status' = status location' = to? 10
![Course: Software Engineering - Design I Bag of copies Collection [stock] stock 0: F(stock) Course: Software Engineering - Design I Bag of copies Collection [stock] stock 0: F(stock)](http://slidetodoc.com/presentation_image_h2/b50428df0c09d11d20994fea1c2181d8/image-11.jpg)
Course: Software Engineering - Design I Bag of copies Collection [stock] stock 0: F(stock) isbn: stock seq. CHAR location: stock LOCATION status: stock COPYSTATUS x: stock. x stock 0 (forces stock to be finite) • Elements of stock identify individual copies held. • For each x stock, values isbn(x), location(x) and status(x) are the values needed to define a model of Copy. • Collection is Fin. Bag(Copy): a model of Collection is a finite bag of models of Copy. Unit 3: Classes and Objects 11

Course: Software Engineering - Design I Promotion Operations on individual copies are “promoted” to give operations on the entire collection. e. g. : Move. Copy - moving one copy but in a collection Move. One. Copy DCollection moving? : stock to? : LOCATION stock' = stock isbn' = isbn status' = status x: stock. (location'(x)=location(x) x = moving? ) location'(moving? ) = to? Unit 3: Classes and Objects 12

Course: Software Engineering - Design I Creation New. Copy: creating one new copy in a collection Unit 3: Classes and Objects One. New. Copy DCollection isbn? : seq. CHAR location? : LOCATION new!: stock' new! stock' = stock {new!} x: stock. (isbn'(x) = isbn(x) location'(x) = location(x) status'(x) = status(x)) isbn'(new!) = isbn? location'(new!) = location? status'(new!) = in 13

Course: Software Engineering - Design I Other classes System is more than just a collection of objects from a single class schema. Suppose it also has Borrower id: seq. CHAR max: N • a class schema Borrower: • a corresponding bag-schema – Borrowers = Fin. Bag(Borrower), with (max = max. no. books to be borrowed) indexing set “borrower”. Library Collection Borrowers borrowed: F(borrower stock) x: stock. (status(x) = out $y: borrower. (y, x) borrowed) y: borrower. |{x: stock | (y, x) borrowed}| max(y) Unit 3: Classes and Objects 14

Course: Software Engineering - Design I Example Operation Borrow DLibrary x? : stock y? : borrower status(x? ) = in |{x: stock. (y? , x) borrowed}| < max(y? ) stock' = stock borrower' = borrower isbn' = isbn location' = location id' = id max' = max x: stock. (status'(x) = status(x) x = x? ) status'(x? ) = out borrowed' = borrowed {(y? , x? )} Unit 3: Classes and Objects 15
- Slides: 15