SDD Topic has been refreshed!
Course Content
By the end of this lesson you will be able to:
Describe, exemplify, and implement UML for the following:
class diagrams:
class name
instance variables and data types
Methods
public and private
Inheritance
constructor
array of objects
Describe, exemplify, and implement the following object-oriented constructs
object
property
method
class/sub-class
encapsulation
Inheritance
Instantiation
polymorphism
If we wanted to store the details of a person
forename
surname
age
place of birth
We would already be able to put these into records to store the data but then would need to write sub routines and use parameter passing to alter the data.
This is a different programming paradigm where objects will be used and not records
Objects are the fundamental building block of = Object Oriented programming. Up until now you have used functions/subroutines with parameter passing but now we will look at creating objects which are capable of holding data and of performing operations on that data.
An object is an instance of a class
An object has methods and properties/instance variables
The sweet wrapper is an analogy for encapsulation which we will look at as we progress further into OO programming. It is a concept whereby the methods are hidden in the class.
A watch (object) has the following properties:
Time
Date
It is possible to change the time and date of the watch by taking the glass cover off and moving the hands/date indicator manually.
This is likely to cause problems
The better way to change the time and date is to use the dial on watch
This can be thought as the methods.
AdjustTime and AdjustDate
These in turn are setting the Time and Date properties
We can also consider looking at the Date or Time as ‘getting’ the Date and Time properties. Our original attempt when we changed the properties directly was problematic. It would be much less likely to cause problems if we use the 'methods' available
An object (one cookie) is one instance of a class.
When an object is instantiated it will have:
its own set of instance variables (properties)
and have access to all the methods described in the class definition
Getter and setter methods allow developers to access the properties.
We will use the image of the sweet wrapper to indicate that we can't see under it - encapsulation.
A UML class diagram is the design for a class. It will define the methods and properties contained in the class. A sample UML class diagram is shown below.
+ indicates that this property or method is visible from outside of the class (public)
- indicates that this property or method is hidden from outside of the class (private)
As we have mentioned the properties in the class should only be accessible by methods. So if we consider the Person class as shown above.
To the programmer the UML looks like above
To other modules in the program the UML looks like above
What do the following terms mean?
object class property method UML encapsulation getter method setter method
Object - is a group of data and associated program routines used within an object-oriented programming system.
Class - defines the methods and properties for a group of similar objects. It is a template specifying program routines used for the methods and the data types of the properties.
Property - is data within an object.
Method - is a program routine contained within an object (see above) designed to perform a particular task on the data within the object.
UML - Unified Modelling Language is a notation of symbols and diagrams to communicate object-oriented analysis and design.
Encapsulation - where the only means to manipulate or retrieve a property is through a method.
Getter Method - a method within a property that returns the value of that property/instance variable
Setter Method - a method which assigns/updates the value of a property/instance variable
We have updated the UML Class diagram as show to the right with the addition of the new method Person()
We will now take a look at how the UML Class diagram will be implemented as a class in Python.
Although we refer to properties in the UML Class diagram once they are implemented and an object is created they are referred to as instance variables. As each object that is created will have its own set of instance variables which correspond with the properties in the UML class diagram.
The Constructor method, shown by Person() in the UML diagram to the right is a method that is executed when an instance of a given class (object) is created.
A constructor method initialises the instance variables of the new object
A constructor method can run only once when a class is created and will run before all other code related to the class
Initialises instance variables using any supplied parameters or setup those with defaults - such as the age property of the code below.
No value for the age property is supplied to the constructor but the value for the age property is initialised as 0 in line number 5.
Person class constructor method
Whenever we call an method of object, the Python compiler (implicitly) passes an object reference as the first argument (self).
So for example when we call the getAge() method of the person class (shown below)
So when we use the line print(character2.getAge()) this ensures that only the getAge() method is carried out on the object that is referenced by the variable character2.
Unlike some languages Python cannot formally set a property as private. Encapsulation is when the instance variables cannot be accessed outside of the class and have to be accessed through methods.
Python cannot do this as formally as other languages, where you can declare a property as private. But one measure we can is you can place a single underscore _ before the property names in the class definition.
This will allow us to mimic the private properties such as below.
Python is saying that the Person object referenced by the variable character2 has no property called age (which it doesn't)
To promote encapsulation we should only access and modify the properties through methods such as below.
The methods getAge and setAge will allow the user to display the value of the age property (getAge) or assign a new value to the age property (setAge).
We will now look at implementing the Person class ( shown in the UML class diagram to tthe right of the code) in Python and then testing it with some code
Lines 2-5 create the object and initialise the instance variables with any supplied parameters.
Lines 7-11 are getter methods used to return the values of the instance variables.
There is no output from these two lines as this code is creating two variables character1 and character2 which each point to an object which is an instance of the Person class.
0
Hi, my name is Freddo, I am 34
Hi, my name is Freddo, I am 35
Line 1 calls the setAge method of the Person class to change the value of the age instance variable of the character1 object to 34.
Line 2 calls the getAge method of the character2 object to display the age. This returns the value 0.
Line 3 calls the sayHello method of the character1 object which provides the message with the person's name and age which we set to 34 in line 1.
Line 5 calls the setAge method of the character1 class and as parameters it supplies the value of the characters current age +1 (which is retrieved using the getAge method)
So in line 6 when the sayHello method is called the value of the age instance variable is now 35.