Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS224 Software Projects: Software Engineering and Research Methods Lecture 1b Object concepts (Based on Stevens and Pooley (2006), Chapter 2) David Meredith.

Similar presentations


Presentation on theme: "CIS224 Software Projects: Software Engineering and Research Methods Lecture 1b Object concepts (Based on Stevens and Pooley (2006), Chapter 2) David Meredith."— Presentation transcript:

1 CIS224 Software Projects: Software Engineering and Research Methods Lecture 1b Object concepts (Based on Stevens and Pooley (2006), Chapter 2) David Meredith d.meredith@gold.ac.uk www.titanmusic.com/teaching/cis224-2007-8.html

2 What is a good system like? A good system uses loosely coupled, encapsulated modules (components) that –have good interfaces allow access to important features hide irrelevant details of how module works –are good abstractions high cohesion appropriate information hiding simulate well behaviour and structure of identifiable things –are reusable –are replaceable

3 Why use an object-oriented approach? Supports the development of good systems Supports development that is –Architecture-centric –Component-based

4 What is an object? Object is a thing that has behaviour, state and identity ( Booch, G. (1991). Object-Oriented Design with Applications. Benjamin/Cummings. ) Thing –object should represent (i.e., be an abstraction of) an identifiable concept or physical thing State –all data encapsulated by object –object has a set of attributes/instance variables/data members –each attribute has a value –attribute can be mutable or immutable –set of attributes does not change, values of attributes may change Behaviour –the way an object acts and reacts in response to messages –set of messages understood by an object is fixed –way in which an object responds to a message may depend on its state Identity –Two objects with the same state are not necessarily the same object –Two objects can be equal without being the same object –An object occupies a particular place in memory and this gives it an identity –If two objects have identical state but they are not the same object, then the only thing that's different about them is their identity –An object retains its identity even when its state is changed

5 Example of an object myClock object that represents a clock myClock interface defines that it understands messages: getTime() setTime(newTime:Time) Time is a type whose elements are valid values of newTime How exactly myClock stores the time, gets the current time and sets a new time is not important to the user or client of the myClock object –This is implementational detail that is hidden by the interface, encapsulated within the object

6 Messages Selectors and arguments myClock understands two types of messages getTime() setTime(newTime:Time) every message has a selector getTime setTime a message may have arguments: getTime() has no arguments setTime(newTime:Time) has one argument, newTime, which must be of type Time arguments may be objects themselves –for example, Time may be a class of objects

7 Messages: Signatures every message has a signature –the combination of its selector and the pattern of types of its arguments for example, myClock might understand the messages readTime(string : String) –Reads a time value from a String readTime(file : File) –Reads a time value from a File These two messages have the same selector but different patterns of arguments: readTime(string : String) takes a String argument readTime(file : File) takes a File argument => signatures are different => could both be defined within a myClock object –Example of method overloading

8 Sending messages Message sent to an object by giving the object's name, followed by a full stop, followed by the message myClock.getTime() sends the message getTime() to myClock “object A sends the message msg() to object B” => message “B.msg()” occurs within one of A's methods Object can send messages to –itself –a value of one of its own attributes –an argument of one of its operations –a new object created by one of its operations

9 An object’s public interface object's public interface defines messages it will accept from anywhere interface usually provides signature (i.e., selector and arguments) and return type of each message –E.g., getTime() : Time public interface may also contain attributes but this is usually a bad idea –we may want to change object so that changing an attribute value has some other side effect –or may want to change the type of an attribute –can do this without changing interface if the attribute value is set or retrieved using operations –thus preserving encapsulation –Cf. example of polar and cartesian co-ordinate representations of position in a Point object

10 An object’s private interface methods within an object may need to access attributes directly –so an object may send itself any message that it understands and access any of its own attributes – even if they are private –e.g., a get() method will usually have to access an attribute directly within the same object => –object has a public interface which any part of the system can use and –larger private interface which the object itself and other privileged parts of the system can use an object may make different parts of its private interface available depending on its role within a particular context –it may then implement or realize two or more different public interfaces Cf. class implementing multiple interfaces in Java

11 Classes Each object has –Attributes Each attribute is a place to store data –Operations The messages that the object understands Each operation is implemented by a specific method Often want many objects with same attributes and methods –E.g., every object representing a customer in a company’s invoicing system –Therefore have a “template” that lets us make as many objects of a particular type as we want –This template is called a class definition

12 Classes and class definitions A class definition is an “object factory” or an object type specification –it defines the attributes and methods that are possessed by all objects of a particular type (or "class") Two objects are in the same class if they are created using the same class definition If the elements of a data type are objects, then that type is a class A class definition also specifies the visibility of each attribute and method –i.e., whether it is public or private Class definition therefore also defines the public and private interfaces of the objects in its class Instantiating a class means creating a new object of that class Every object is an instance of the class to which it belongs

13 Class example myClock is an object of class Clock with attribute - time : Time and operations + getTime() : Time + setTime(newTime : Time) Therefore each Clock object has –a private attribute called time which stores a value of type (or class) Time –a public operation with selector getTime which returns a value of type Time –a public operation with selector setTime which takes a single argument of type Time and returns no value the definition of class Clock would define specific methods to implement the operations, getTime() and setTime(newTime : Time) the public interface of class Clock would contain the two public operations, getTime() and setTime(newTime : Time) the private interface of class Clock would in addition contain the attribute, time

14 WRITE ONCE! A class definition can be used to make as many objects of a particular class as required Example of (probably) the most important principle in software engineering which is WRITE ONCE! Most important benefit of writing once is –You only have to define (and maintain) a thing in one place (where it is defined) – not everywhere it is used

15 Examples of the “Write Once” principle Using a loop to iterate over all the elements in an array –Only write once the operation that is performed on each element Defining a class and using its constructor to produce many objects of that class Factoring out code into subroutines, functions, methods or procedures

16 ALWAYS WRITE ONCE! ALWAYS LOOK FOR OPPORTUNITIES TO WRITE ONCE! If you write the same thing twice, that probably means you can improve your design If you only remember one thing from this course, it should be the “Write Once!” principle

17 Classes as components Classes (not objects) in an object-oriented system should ideally be loosely coupled, highly coherent, encapsulated modules that have good interfaces, are good abstractions and can therefore function as reusable, replaceable components Object-oriented programming languages usually support the construction of classes that can function as reusable and replaceable components

18 Inheritance Building a simulation of a zoo in which each animal is represented by an object Set of attributes depends on type of animal If define independent class for each animal then break “write once” principle

19 Inheritance Define class Animal which contains attributes common to all animals Define Bird to contain everything in Animal plus wingSpan Define Parrot to contain everything in Bird Define Quadruped to contain everything in Animal plus height, foreLegLength and hindLegLength Define Elephant to contain everything in Quadruped plus trunkLength and tuskLength Here only write each distinct attribute once Elephant inherits from Quadruped which inherits from Animal Bird inherits from Animal Quadruped is a subclass (or derived class) of Animal Quadruped is a superclass (or base class) of Elephant Quadruped is a specialization of Animal Animal is a generalization of Quadruped An Elephant is a Quadruped An Elephant is an Animal Inheritance is the process by which a class contains all the attributes and operations of its superclasses

20 Method overriding Each employee represented by an object in personnel system Way that net salary calculated depends on type of employee Managers can have stock options getNetSalary takes stock options into account in a Manager object but not an Employee object Method executed when Manager receives getNetSalary different from method executed when Employee receives getNetSalary message getNetSalary method in Employee is overridden in the Manager class

21 When to use inheritance Using inheritance increases coupling between classes which makes them less reusable So we should only allow a class to inherit from another class if it is a specialization of that other class For example –a German Shepherd is a special type of Dog, so it would be reasonable to define a class, GermanShepherd, that is a subclass of some more general class, Dog –A triangle is a special kind of polygon, so it would be reasonable to have a class, Triangle that inherits from a more general superclass Polygon

22 Substitutability The Liskov Substitution Principle: –If p is a variable that refers to an object of class x, then p may refer to any object from any subclass of x Liskov, B. & Wing, J. M (1994). A behavioral notion of subtyping. ACM TOPLAS, 16(6), 1811-1841. (Available online at http://www.cs.cmu.edu/~wing/publications/LiskovWing94.pdf) For example –We can use a Bird, a Quadruped or an Elephant anywhere that an Animal is used –We can use an Elephant anywhere that a Quadruped is used –We can use a Manager anywhere that an Employee is used –We can use a Triangle anywhere that a Polygon is used

23 Polymorphism The Liskov Substitution principle says that a variable that points or refers to an object of class x may point or refer to an object from any subclass of the class of x Such a variable is said to be polymorphic (Greek: poly = many, morph = shape) because it can refer to objects of many different “shapes” (i.e., classes)

24 Example of polymorphism We define a base class, Polygon, which is an abstraction of a polygon and three subclasses of Polygon: –Circle, Square and Triangle Suppose every Polygon knows how to display itself on a screen and that we invoke this behaviour in a Polygon (i.e., we get it to display itself) by sending it the message display() –i.e., every Polygon has an operation whose signature is display() The method executed when we send a display() message to a Triangle is different from the method executed when we send the display() message to a Square: aTriangle.display() draws the triangle represented by the variable, aTriangle aSquare.display() draws the square represented by the variable, aSquare

25 Example of polymorphism (cont.) Suppose that the variable polygonList refers to a collection of Polygon objects, which might include Squares, Triangles and Circles Say we want to iterate over polygonList, displaying each Polygon in turn: for each p in polygonList p.display() When this loop is executed, variable p might refer to a Circle, a Square or a Triangle –That is, p is polymorphic.

26 Dynamic binding When the following loop is executed for each p in polygonList p.display() –method executed when display() sent to p depends on class of p If p is a Triangle, a triangle is displayed If p is a Square, a square is displayed If p is a Circle, a circle is displayed At compile time, compiler only checks that p is a Polygon Does not know whether p is a Triangle, Square or Circle until run time Therefore specific method executed is only bound to the command p.display() at run time This is called dynamic or late binding

27 Summary Object-oriented design supports development of good systems An object is a thing with behaviour, state and identity An object has a public and a private interface A class definition is an object factory for making objects with a particular set of attributes and methods WRITE ONCE! A class that is a loosely coupled, coherent, encapsulated module with a good interface can function as a reusable, replaceable component Inheritance and method overriding The Liskov Substitution Principle Polymorphism and dynamic binding


Download ppt "CIS224 Software Projects: Software Engineering and Research Methods Lecture 1b Object concepts (Based on Stevens and Pooley (2006), Chapter 2) David Meredith."

Similar presentations


Ads by Google