SEF9 - 40 Abstraction in Object Oriented (OO) software design

(A) ABSTRACTION in Object Oriented Software Design
===========================================================

(1) A "class" is an abstract concept of "the thing" that provides modularity and structure in an object-oriented (OO) computer program.

(2) A class defines the abstract characteristics of "the thing" (i.e. of something), and it must define two(2) things below:

(2.1) its characteristics (also called its attributes, fields or properties, basically its static characteristics)

(2.2) its behaviors (also called its methods, operations, procedures, functions or features, basically what it can do, its dynamic behaviours).

(3) A class can be visualized as a "blueprint or template or factory" that describes the nature "the thing" (of something).

(4) The "class" by itself, is not executed when a program is running. The class must be "instantiated into objects" in computer memory, and it is these "objects" that get executed when the program runs. This is the reason the class is called the "blueprint or template or factory" of the thing.

object = a portion of computer memory which contains the information needed to model the real world thing.

class = defines the data structure used to store the objects which are instances of the class together with their behavior.

(5) Example in python programming language: A "person" is an abstract thing. Please see the attached "client1.py" and "person.py" programs for the person class below.

===========================================================
FILE: client1.py
===========================================================
#! /usr/bin/env python

# File: client1.py
# Date: Sat Dec 01 00:08:14 MYT 2012 WRY

"""
IMPORTANT NOTES:

(1) This program file is named "client1.py" and it imports an external file named "person.py". The external file contains the definition of the "person" class.

(2) The "person" class contains definitions of the person's attributes and methods.

(3) From the "person" class, we created (instantiated) new objects for the class. These new objects will automatically get all the attributes and methods defined by the class. For example, if we created four(4) objects from the same class, all the four(4) different objects will get the same attributes and methods defined by the same class.

(4) The names of the four(4) objects must be different. That means, we have "copied everything from the class" and make four(4) "differently-named" objects.

(5) In software terms, "creating 4 objects from the class" is said as "creating 4 instances of the class".

(6) In software, "objects" are different from normal "variables". A normal variable like "x = 3" is an integer and the variable "x" has only one value. An object like "y = person()" is an abstract and complex software construct because it contains attributes and methods. The object "y" now is not a single valued entity.

(7) To get access to the attributes and methods of the created objects, we use the "dot" notation. For example, let us create an object named "justine" from the "person" class. To do that we make a statement "justine = person()" and that is how simple it is. Now, the name of the unique object is "justine". To get an attribute like "weight" for "justine", we make the statement "justine.weight()", where we have used the dot notation. Similarly, to get a method like "welcome" for "justine", we we make the statement "justine.welcome()", again using the dot notation. Note: We must make sure that there is an attribute "weight" an a method "welcome" in the person class.

(8) We can create many objects from the same class simultaneously, making each object unique by giving unique names. For example, if we use the "person" class, then:

Nour = person() # "Nour" now becomes an object and "Nour" is the name of this object.
Lim = person() # "Lim" now becomes an object and "Lim" is the name of this object.
Richard = person() # "Richard" now becomes an object and "Richard" is the name of this object.

(9) This program (client1.py) then manipulates the objects created in item(8) above. As expected, it uses the "dot" technique to access (read/write) the attributes and methods of the three different objects.

(10) Run this client code and make sure that both files (client1.py and person.py) are located in the same directory. Otherwise, the client file (client1.py) cannot find the class file (person.py) that it imports.

(11) Study the codes of both files and study the resulting outputs. Learn by making changes and re-running the program.

"""
# =========================================================
import time
import datetime

import person
from person import *

globalStartTime = time.time()
print "STARTING: ", datetime.datetime.now(), \
"at program run time %s " %(time.time() - globalStartTime), "seconds."

# =========================================================
# CALLING PERSON CLASS (person.py) AND USING IT
# =========================================================
# INSTANTIATE "PERSON" CLASS TO BECOME "OBJECTS", TEMPLATED FROM THE person CLASS
# CREATE THREE(3) DIFFERENT NAMED PERSONS AS THREE(3) DIFFERENT OBJECTS

Nour = person()
Lim = person()
Richard = person()

# NOW WE HAVE THREE SEPARATE OBJECTS IN COMPUTER MEMORY (Nour, Lim and Richard)
# BASED ON THE EXECUTION OF THE ABOVE THREE LINES OF CODE.

print ""
print "Nour's object address in computer memory = ", Nour
print "Lim's object address in computer memory = ", Lim
print "Richard's object address in computer memory = ", Richard
print ""

# SET THE OBJECT ATTRIBUTES AND DISPLAY USING "set-get" METHODS
Nour.setName("Nour Maliki")
print "1. Nour's full name = ", Nour.getName()

Lim.setName("Lim Swee King")
print "2. Lim's full name = ", Lim.getName()

Richard.setName("Richard Burton")
print "3. Richard's full name = ", Richard.getName()
print""

# EXAMPLE CALLS ON THE OBJECT METHODS
print Nour.welcome()
print Lim.welcome()
print Richard.welcome()
print ""

# MAKE CHANGES AND MANIPULATE THE OBJECT
# DISPLAY BEFORE SETTING THE RESPECTIVE ID NUMBERS
print "4. Nour's ID number = ", Nour.getID_number()
print "5. Lim's ID number = ", Lim.getID_number()
print "6. Richard's ID number = ", Richard.getID_number()
print ""

# SET THE RESPECTIVE ID NUMBERS OF OBJECTS
Nour.setID_number("887766035555")
Lim.setID_number("991233554444")
Richard.setID_number("334455097777")

# DISPLAY AFTER SETTING THE RESPECTIVE ID NUMBERS
print "7. Nour's ID number = ", Nour.getID_number()
print "8. Lim's ID number = ", Lim.getID_number()
print "9. Richard's ID number = ", Richard.getID_number()
print ""

# SET THE RESPECTIVE WEIGHTS OF OBJECTS
Nour.setWeight(56.75)
Lim.setWeight(65.83)
Richard.setWeight(85.24)

print "10. Nour's Weight = ", Nour.getWeight(), "kilograms."
print "11. Lim's Weight = ", Lim.getWeight(), "kilograms."
print "12. Richard's Weight = ", Richard.getWeight(), "kilograms."
print ""

print "13. Nour's run attribute: ", Nour.running_rate, "meters per second."
print "14. Lim's walk attribute: ", Lim.walking_rate, "meters per second."
print "15. Richard's eat attribute: ", Richard.eating_rate, "kilograms per minute."
print ""

NR = Nour.run(5.3)
LW = Lim.walk(2.7)
RE = Richard.eat(1.5)

print "16. Nour's run : ", NR, "meters per second."
print "17. Lim's walk : ", LW, "meters per second."
print "18. Richard's eat : ", RE, "kilograms per minute."
print ""

print "19. Nour's run attribute: ", Nour.running_rate, "meters per second."
print "20. Lim's walk attribute: ", Lim.walking_rate, "meters per second."
print "21. Richard's eat attribute: ", Richard.eating_rate, "kilograms per minute."
print ""

Nour.farewell("Wailal loqoq.")
Lim.farewell("Will see you again.")
Richard.farewell("Alsta lavista baby!")
print ""

print "FINISHED: ", datetime.datetime.now(), \
"at program run time %s " %(time.time() - globalStartTime), "seconds."
# =========================================================
# RESULTS OF EXECUTION
# =========================================================
"""
wruslan@wruslan-ubuntu1004-rtai:~/Downloads/temp$ python client1.py
STARTING: 2012-12-01 08:12:44.462645 at program run time 3.09944152832e-05 seconds.

Nour's object address in computer memory = <person.person instance at 0xb773486c>
Lim's object address in computer memory = <person.person instance at 0xb773488c>
Richard's object address in computer memory = <person.person instance at 0xb77348ac>

1. Nour's full name = Nour Maliki
2. Lim's full name = Lim Swee King
3. Richard's full name = Richard Burton

Welcome Nour Maliki
Welcome Lim Swee King
Welcome Richard Burton

4. Nour's ID number = 860911035067
5. Lim's ID number = 860911035067
6. Richard's ID number = 860911035067

7. Nour's ID number = 887766035555
8. Lim's ID number = 991233554444
9. Richard's ID number = 334455097777

10. Nour's Weight = 56.75 kilograms.
11. Lim's Weight = 65.83 kilograms.
12. Richard's Weight = 85.24 kilograms.

13. Nour's run attribute: 0.0 meters per second.
14. Lim's walk attribute: 0.0 meters per second.
15. Richard's eat attribute: 0.0 kilograms per minute.

16. Nour's run : now runs at 10.6 meters per second.
17. Lim's walk : now walks at 2.7 meters per second.
18. Richard's eat : now eats at 0.75 kilograms per minute.

19. Nour's run attribute: 5.3 meters per second.
20. Lim's walk attribute: 2.7 meters per second.
21. Richard's eat attribute: 1.5 kilograms per minute.

Wailal loqoq.
Will see you again.
Alsta lavista baby!

FINISHED: 2012-12-01 08:12:44.462852 at program run time 0.000228881835938 seconds.
wruslan@wruslan-ubuntu1004-rtai:~/Downloads/temp$

"""

===========================================================
FILE: person.py
===========================================================
#! /usr/bin/env python

# File: person.py
# Date: Sat Dec 01 00:08:14 MYT 2012 WRY

class person():
"""A simple example for a person class"""

# INITIALIZATION FOR THIS CLASS
def __init__(self):

# DEFINE ATTRIBUTES WITH DEFAULT VALUES FOR THIS CLASS
self.name = "Mr Bismillah" # a string of characters with blank spaces allowed
self.ID_number = "860911035067" # some identification string
self.hair_color = "black" # black, brown, blond, red, brunette, etc
self.eye_color = "brown" # black, brown, hazel, blue, green etc
self.weight = 0.0 # kilograms
self.height = 0.0 # centimeters
self.running_rate = 0.0
self.walking_rate = 0.0
self.eating_rate = 0.0
self.farewell_message = "Bye Bye"

# DEFINE SET-GET METHODS FOR THIS CLASS
def setName(self,input_name):
self.name = input_name
def getName(self):
return (self.name)

def setID_number(self,input_ID_number):
self.ID_number = input_ID_number
def getID_number(self):
return (self.ID_number)

# DO SIMILARLY AS ABOVE FOR
# setHair_color()
# getHair_color()
# setEye_color()
# getEye_color()

def setWeight(self,input_weight):
self.weight = input_weight
def getWeight(self):
return (self.weight)

# DO SIMILARLY AS ABOVE FOR
# setHeight(),
# getHeight()

# DEFINE ADDITIONAL ACTION OR BEHAVIOUR METHODS FOR THIS CLASS
def welcome(self):
return "Welcome " + self.getName()

def run(self, run_speed):
self.running_rate = run_speed
return "now runs at " + str((2.0 * run_speed))

def walk(self, walk_speed):
self.walking_rate = walk_speed
return "now walks at " + str((1.0 * walk_speed))

def eat(self, eat_rate):
self.eating_rate = eat_rate
return "now eats at " + str((0.5 * eat_rate))

def farewell(self, message):
self.farewell_message = message
print self.farewell_message

# =========================================================

--
WASSALAM
wruslan.hahaha

Microsoft Word Version
Return to Software Engineering Fundamentals (SEF9MMUWRY)
Previous Topic: BSE1 - Tutorial Mon 19 Nov 2012 (Run these PyQt4 scripts, see the GUI)
Next Topic: 41 Inheritance in Object Oriented (OO) software design

0 comments:

Post a Comment