Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects, Classes and ADTs Reference vs. Expanded Types

Similar presentations


Presentation on theme: "Objects, Classes and ADTs Reference vs. Expanded Types"— Presentation transcript:

1 Objects, Classes and ADTs Reference vs. Expanded Types
11/8/2018 1:05 PM COSC3311 – Software Design Objects, Classes and ADTs Reference vs. Expanded Types Object Oriented Software Construction /11/2018 1:05 PM 0

2 An object is a run-time instance of some class
11/8/2018 1:05 PM Objects vs. Classes An object is a run-time instance of some class An class is an abstract data type (ADT) with a possible partial implementation Object Oriented Software Construction /11/2018 1:05 PM 1

3 11/8/2018 1:05 PM Avoid “objectspeak” The run-time structures, some of them corresponding to “objects” of the modeled system, are objects. The software modules, each built around a type of objects, are classes. A system does not contain any “objects” (although its execution will create objects). Object Oriented Software Construction /11/2018 1:05 PM 2

4 Why the top class is not Object
11/8/2018 1:05 PM Why the top class is not Object ANY CONTAINER * BOX COLLECTION TRAVERSABLE FINITE INFINITE UNBOUNDED COUNTABLE RESIZABLE BAG SET HIERARCHICAL LINEAR TABLE ACTIVE INTEGER_ INTERVAL BILINEAR INDEXABLE CURSOR_ STRUCTURE DISPENSER SEQUENCE STRING HASH_TABLE STACK QUEUE BOUNDED Category error ARRAY Object Oriented Software Construction /11/2018 1:05 PM 3

5 Abstract data type POINT
11/8/2018 1:05 PM Abstract data type POINT x: POINT  REAL y: POINT  REAL : POINT  REAL : POINT  REAL Class POINT: Choose a representation (polar, cartesian) In polar representation,  and  are attributes, x and y are routines. y x Object Oriented Software Construction /11/2018 1:05 PM 4

6 Abstract data type POINT
11/8/2018 1:05 PM Abstract data type POINT x: POINT  REAL y: POINT  REAL : POINT  REAL : POINT  REAL Axiom: y x Object Oriented Software Construction /11/2018 1:05 PM 5

7 What is OO programming? OO = ADTs + inheritance
Object Oriented Software Construction /11/2018 1:05 PM 6

8 A simple class class POINT feature
11/8/2018 1:05 PM A simple class class POINT feature x, y: REAL -- Point cartesian coordinates ro: REAL is -- Distance to origin (0, 0) do Result := sqrt (x^2 + y^2) end theta: REAL is -- Angle to horizontal axis Object Oriented Software Construction /11/2018 1:05 PM 7

9 Uniform access through feature call
11/8/2018 1:05 PM Uniform access through feature call To access a property of a point p1, the notation is the same regardless of the representation, e.g. p1.x which is applicable both in cartesian representation (x is an attribute) and in polar representation (x is a function without arguments). In the first case the feature call is a simple field access; in the second it causes a computation to be performed. There is no difference for clients (except possibly in terms of performance). uniform access principle Object Oriented Software Construction /11/2018 1:05 PM 8

10 Class POINT (cont’d) distance (p: POINT): REAL is -- Distance to p do
11/8/2018 1:05 PM Class POINT (cont’d) distance (p: POINT): REAL is -- Distance to p do Result := sqrt ((x – p.x)^2 + (y – p.y)^2) end move (a, b: REAL) is -- Move by a horizontally and by b vertically. x := x + a y := y + b scale (factor: REAL) is -- Scale by factor. x := factor * x y := factor * y Object Oriented Software Construction /11/2018 1:05 PM 9

11 Use of the class in a client (1/5)
11/8/2018 1:05 PM Use of the class in a client (1/5) class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p create q end p 0.0 0.0 (POINT) q 0.0 0.0 (POINT) Object Oriented Software Construction /11/2018 1:05 PM

12 Use of the class in a client (2/5)
11/8/2018 1:05 PM Use of the class in a client (2/5) class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p create q p.move (4.0, -2.0) -- Compare with Pascal, C, Ada: -- Move (p, 4.0, -2.0) end p 4.0 -2.0 (POINT) q 0.0 0.0 (POINT) Object Oriented Software Construction /11/2018 1:05 PM

13 Use of the class in a client (3/5)
11/8/2018 1:05 PM Use of the class in a client (3/5) class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p create q p.move (4.0, -2.0) -- Compare with Pascal, C, Ada: -- Move (p, 4.0, -2.0) p.scale (0.5) end p 2.0 -1.0 (POINT) q 0.0 0.0 (POINT) Object Oriented Software Construction /11/2018 1:05 PM

14 Use of the class in a client (4/5)
11/8/2018 1:05 PM Use of the class in a client (4/5) class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p create q p.move (4.0, -2.0) -- Compare with Pascal, C, Ada: -- Move (p, 4.0, -2.0) p.scale (0.5) u := p.distance (q) v := p.x p := q end p 2.0 -1.0 (POINT) q 0.0 0.0 (POINT) Object Oriented Software Construction /11/2018 1:05 PM

15 Use of the class in a client (5/5)
11/8/2018 1:05 PM Use of the class in a client (5/5) class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p create q p.move (4.0, -2.0) -- Compare with Pascal, C, Ada: -- Move (p, 4.0, -2.0) p.scale (0.5) u := p.distance (q) v := p.x p := q p.scale (-3.0) end p 2.0 -1.0 (POINT) q 0.0 0.0 (POINT) Object Oriented Software Construction /11/2018 1:05 PM

16 The module-type merge A class is both: A module A type
11/8/2018 1:05 PM The module-type merge A class is both: A module A type Much of the conceptual power of the method comes from the fusion of these two notions. From the module viewpoint: Set of available services (“features”). From the type viewpoint: Description of set of possible run-time objects (its instances). Connection: The services of the class, viewed as a module, are the operations applicable to the instances of the class, viewed as a type. Object Oriented Software Construction /11/2018 1:05 PM

17 Applying abstraction principles
11/8/2018 1:05 PM Applying abstraction principles Privileges of a client C of a class A on query balance: Read access if attribute is exported. a.balance is a legal query in class C. a.balance := 800 is an illegal assignment read privleges (but not write) C A a: A Object Oriented Software Construction /11/2018 1:05 PM

18 Creating an object With the class POINT as given: ... create my_point
11/8/2018 1:05 PM Creating an object With the class POINT as given: my_point: POINT ... create my_point Effect of such a creation instruction: Allocate new object of the type declared for my_point. Initialize its fields to default values (0 for numbers, false for booleans, null for characters, void for references). Attach it to the instruction’s target, here my_point. Without a creation clause Object Oriented Software Construction /11/2018 1:05 PM

19 Specific creation procedures
11/8/2018 1:05 PM Specific creation procedures class POINT create make_cartesian, make_polar feature -- Initialization make_cartesian (a, b: REAL) is Initialize to abscissa a, ordinate b. do x := a y := b end make_polar(a, b: REAL) is ... feature ... The rest as before ... Object Oriented Software Construction /11/2018 1:05 PM

20 If there is a creation clause
11/8/2018 1:05 PM If there is a creation clause Creation instructions must be “creation calls”, such as create my_point.make_polar (1, Pi / 2) Object Oriented Software Construction /11/2018 1:05 PM

21 If there is no creation clause
11/8/2018 1:05 PM If there is no creation clause An absent creation clause, as in class POINT -- No creation clause feature … The rest as before … end is understood as one that would only list default_create, as if it had been written create default_create Procedure default_create is defined in ANY as doing nothing; any class can redefine it to provide proper default initializations. Object Oriented Software Construction /11/2018 1:05 PM

22 Associated convention
11/8/2018 1:05 PM Associated convention The notation create x is understood (if permitted) as an abbreviation for create x.default_create Object Oriented Software Construction /11/2018 1:05 PM

23 To allow both forms To make both forms valid:
11/8/2018 1:05 PM To allow both forms To make both forms valid: create my_point.make_cartesian (0, 1) as well as create my_point.make_polar (1, Pi / 2) it suffices to make default_create (redefined or not) one of the creation procedures: class POINT create make_cartesian, make_polar, default_create feature ... The rest as before ... Object Oriented Software Construction /11/2018 1:05 PM

24 To prohibit instantiating a class
11/8/2018 1:05 PM To prohibit instantiating a class class NOT_CREATABLE create -- Nothing here! feature ... The rest as before ... end Object Oriented Software Construction /11/2018 1:05 PM

25 Forms of assignment and copy
11/8/2018 1:05 PM Forms of assignment and copy Reference assignment (a and b of reference types): b := a Object duplication (shallow): c := a.twin Object duplication (deep): d := a.deep_twin Also: shallow field-by-field copy (no new object is created): e.copy (a) Object Oriented Software Construction /11/2018 1:05 PM

26 Shallow and deep cloning
11/8/2018 1:05 PM Shallow and deep cloning a O1 Initial situation: Result of: b := a c := a.twin d := a.deep_twin name “Almaviva” landlord loved_one O3 O2 “Figaro” “Susanna” b O4 “Almaviva” c The Barber of seville Almavia – local nobelman d name “Almaviva” O5 landlord loved_one O7 O6 “Figaro” “Susanna” Object Oriented Software Construction /11/2018 1:05 PM

27 Equality – Eiffel vs. Java
11/8/2018 1:05 PM Equality – Eiffel vs. Java Eiffel Expanded vs. Reference types (You choose) Reference equality: x = y Object equality: x ~ y x.is_equal(y) Deep equality: deep_equal(x, y) Change the default by redefining is_equal STRING redefines is_equal to one level deeper than shallow is_equal Java Primitives vs. Reference types (No choice) Reference equality: x == y Object equality: x.equals(y) (assuming x /= Void) No built-in deep equality Change the default by over-riding equals java.lang.String overrides the java.lang.Object equals() (same as ==) Java 1.5 equals public boolean equals(Object obj) Indicates whether some other object is "equal to" this one. The equals method implements an equivalence relation on non-null object references: It is reflexive: for any non-null reference value x, x.equals(x) should return true. It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes. Parameters: obj - the reference object with which to compare. Returns: true if this object is the same as the obj argument; false otherwise. See Also: hashCode(), Hashtable java.lang.String overrides the java.lang.Object equals() method to return true if and only if the objects being compared contain the same sequence of characters. String s0 = "Hello"; String s1 = new String("Hello"); // force new string object String s2 = s0; s0.equals(s1) // true (diff objects, same chars) s0.equals(s2) // true (same chars, coincidence // they are same objects) Object Oriented Software Construction /11/2018 1:05 PM

28 Reference vs. Expanded Every entity must be declared to be of a certain type (based on a class) Every type is either a reference type or an expanded type how to treat attachments in assignments, argument passing & comparisons In reference types x := y: x attaches to same object as y x = y: compares references In expanded types x := y: copy contents of y into x x = y: compares value (x ~ y) Object Oriented Software Construction /11/2018 1:05 PM

29 A simple book – basic types
A typical instance b of a BOOK is class BOOK feature title: STRING in_print: BOOLEAN pages: INTEGER end Object Oriented Software Construction /11/2018 1:05 PM

30 Basic types insufficient
class BOOK feature title: STRING in_print: BOOLEAN pages: INTEGER author: WRITER end Object Oriented Software Construction /11/2018 1:05 PM

31 author: WRITER It wastes memory space – the author is the same “Stendhall” in either case. Does not express sharing. The author field refers to the same instance of WRITER. If you update the WRITER object (e.g. to record year_of_death), you will have to change each sub-object. Object Oriented Software Construction /11/2018 1:05 PM

32 Reference types Object Oriented Software Construction /11/2018 1:05 PM

33 Reference types [OOSC2, p224]
A reference is a run-time value that is either Void or attached. If attached, a reference identifies a single object (and is said to be attached to that object). Object Oriented Software Construction /11/2018 1:05 PM

34 The dynamic model for references
11/8/2018 1:05 PM The dynamic model for references States of a reference: Operations on references: create p p := q p := Void if p = Void then ... create p p := q (where q is attached) p ATTACHED VOID ATTACHED p VOID p := Void p := q (where q is void) Object Oriented Software Construction /11/2018 1:05 PM

35 11/8/2018 1:05 PM Modelling Fidelity A void reference can denote an unknown author. “Candide” was published anonymously. Voltaire’s parady of Leibniz Object Oriented Software Construction /11/2018 1:05 PM

36 Aliasing Dynamic aliasing is not just a consequence of a programmer’s dirty tricks — it is a consequence of the human ability to name things, and to give more than one name to the same thing. However, aliasing is shocking – changing author_relativity has the side effect of changing nobel_winner_1921 – this causes many programmer errors! Object Oriented Software Construction /11/2018 1:05 PM

37 Aliasing problems Blurb from the International Workshop on Aliasing, Confinement and Ownership in Object-Oriented programming (IWACO) July 22, 2003: “The power of objects lies in the flexibility of their interconnection structure. But this flexibility comes at a cost. Because an object can be modified via any alias, object-oriented programs are hard to understand, maintain, and analyse. Aliasing makes objects depend on their environment in unpredictable ways, breaking the encapsulation necessary for reliable software components, making it difficult to reason about and optimize programs, obscuring the flow of information between objects, and introducing security problems.” Object Oriented Software Construction /11/2018 1:05 PM

38 Why aliasing? Modelling Fidelity (see earlier slide)
We need references (particularly sharing) just to do certain kinds of data structures. Consider a LINKED_CIRCULAR_LISTS — with a node first linked to one of the nodes (say n) in the circle. Then first and node (n-1) both share a reference to n. Object Oriented Software Construction /11/2018 1:05 PM

39 Expanded vs. References
Object Oriented Software Construction /11/2018 1:05 PM

40 Expanded [OOSC2, 8.7 and 8.8] class BOOK feature title: STRING
11/8/2018 1:05 PM Expanded [OOSC2, 8.7 and 8.8] class BOOK feature title: STRING in_print: BOOLEAN pages: INTEGER author: expanded WRITER end Conceptually, a reference to a WRITER is not the same thing as the WRITER themselves. Conclusion: WRITER should be expanded Alternate format: expanded class WRITER pseudonym: STRING name: STRING end Stendhal ( ) - Pseudonym of Marie-Henri Beyle  One of the most original French writers of the first half of the 19th century, who played a major role in the development of the modern novel. Stendhal is best known for his masterpieces LE ROUGE ET LE NOIR (1830) and LA CHARTREUSE DE PARME (1839), sharp and passionate chronicles of the intellectual and moral climate of France after Napoleon's defeat. Stendhal also wrote travel books, literature and art reviews, and biographies about such composers as W.A. Mozart and Joseph Haydn. Stendhal's subjects are often melodramatic, but they form a fascinating frame for his psychologically deep stories of selfishness and different paths towards self-discovery. "A novel is a mirror that strolls along a highway. Now it reflects the blue of the skies, now the mud puddles underfoot." (from Le Rouge et le Noir) Object Oriented Software Construction /11/2018 1:05 PM

41 Reference POINT vs. Expanded POINT
Reference: sharing Aliasing Expanded: no sharing Sub-object a Object Oriented Software Construction /11/2018 1:05 PM

42 BON diagram class WORKSTATION feature k: expanded KEYBOARD
c: expanded CPU m: expanded MONITOR n: NETWORK end Object Oriented Software Construction /11/2018 1:05 PM

43 BON: Associations vs. Aggregations
Reference: association Expanded: aggregation Object Oriented Software Construction /11/2018 1:05 PM

44 extra slides Object Oriented Software Construction /11/2018 1:05 PM

45 A related mechanism: Persistence
11/8/2018 1:05 PM A related mechanism: Persistence a.store (file) .... b ?= retrieved (file) Storage is automatic. These features come from the library class STORABLE. The above is only an approximate form (see typing discussion). Object Oriented Software Construction /11/2018 1:05 PM

46 Java Strings are Immutable
11/8/2018 1:05 PM Java Strings are Immutable public class EqualityTest2 { public static void main(String[] args) { char c1, c2; String s1,s2,s3; c1 = 'c'; c2 = 'c'; s1 = "hello"; s2 = "hello"; //same string literal s3 = s1; String s4 = new String("hello"); check("t1", c1 == c2); //PASS check("t2", new Character(c1).equals(new Character(c2))); //PASS check("t4",s1 == s2); //PASS check("t5",s1.equals(s2)); //PASS check("t7",s1 == s3); //PASS check("t8",s4 == "hello"); //FAIL //above are different objects check("t9",s4.equals("hello")); } //PASS public static void check(String s, boolean b) { String st = (b)?"passed":"failed"; System.out.println(s+": "+st); }} String objects are read-only or immutable ie the contents of a String object never change String str = "Hello"; str = "Goodbye"; in the above example, the second assignment of "Goodbye" to String, what actually happens is that a new string "Goodbye" is created and the object reference of the new string is stored in the variable str operations that seem to modify a String object actually create new read-only String objects; leaving the original object unchanged the StringBuffer class provides mutable or flexible string handling Object Oriented Software Construction /11/2018 1:05 PM

47 Java: String vs. StringBuffer
Java provides the StringBuffer and String classes, and the String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified. The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations. Object Oriented Software Construction /11/2018 1:05 PM

48 Eiffel Strings are Mutable
11/8/2018 1:05 PM Eiffel Strings are Mutable test_string_equality: BOOLEAN is local s1, s2, s3: STRING do check s1 = Void end s1 := "hello" s2 := "hello“ check s1 /= s2 end s3 := s1 check s1 = s3 end create s2.make_from_string ("hello") check s1.is_equal (s2) end check s1 = s3 and s3 /= s2 end Result := true end Object Oriented Software Construction /11/2018 1:05 PM

49 CHARACTER is expanded test_character_equality: BOOLEAN is local
11/8/2018 1:05 PM CHARACTER is expanded test_character_equality: BOOLEAN is local c1,c2, c3: CHARACTER c4, c5, c6: CHARACTER_REF do check c1 = '%U' and c2 = '%U' end c1 := 'c' c2 := 'c' c3 := c1 Result := c1 = c2 and equal(c1,c2) and c1 = c3 check Result end c4 := 'c' c5 := 'c' c6 := c4 Result := c4 /= c5 and equal(c4,c5) and c4 = c6 end Object Oriented Software Construction /11/2018 1:05 PM

50 11/8/2018 1:05 PM Uniform type system A reference to an integer is different than the integer itself expanded class INTEGER inherit INTEGER_REF ... end In INTEGER, all features of INTEGER_REF become expanded. Same for all basic types: BOOLEAN, REAL, CHARACTER, DOUBLE etc. Uniform type system for primitives or any other type Object Oriented Software Construction /11/2018 1:05 PM

51 Reference types implies Aliasing
11/8/2018 1:05 PM Reference types implies Aliasing Bad io.read_line -- “Jesse” s1 := io.last_string io.read_line -- “Helen” s2 := io.last_string check s1 = s2 and s1 = “Helen” end Good io.read_line s1 := io.last_string.twin s2 := io.last_string.twin io is from ANY Object Oriented Software Construction /11/2018 1:05 PM


Download ppt "Objects, Classes and ADTs Reference vs. Expanded Types"

Similar presentations


Ads by Google