CSIT111 Lecture 2
CSIT111 Lecture 2
1
Focus of this subject
To become a programmer
- you need to be familiar with advanced software
development concepts
- you need to know how to efficiently utilize these
concepts using appropriate programming languages
2
Software development challenge
Software development has unique challenges which are
not common for other industries
• When you design a car you can foresee how it will look like when it is made
• Reverse engineering can reproduce car design documents (identical to original ones)
However…
• When a program is executed, it How to design
is a sequence of invisible electrical programs
signals which represent binary when you can't
microprocessor instructions see the final
• It is practically impossible to product?
reproduce the original program
3
code from binary instructions
Imperative Programming Concept
The oldest concept where a program consists of a
sequence of commands for the microprocessor to execute
Example: Assembly language is a symbolic representation of
microprocessor instructions with one-to-one mapping
pushl %ebp 11010011
movl %esp, %ebp 01010010
addl 8(%ebp), %eax 10000011
How to increase
productivity of software
development?
• When you virtually have to 'live in the microprocessor' and think like a
machine to write even a simple program, you cannot be productive
• It is close to impossible to reuse assembly code. You need to start every
new project practically from scratch
4
How to find an efficient design methodology ?
Other examples:
8
Evolution of OOD languages
• The OOD methodology has substantially evolved since it was
introduced in the 60th
• Evolution of OOD programming languages reflects the evolution of
OOD programming concepts
1967 1980 1983 1995 2000
9
Object Based Design
• The main purpose of any program is to process data
• The major task of OOD is how to split system's data and
behaviour into a set of interacting objects
A functional view of the program An OOD view of the program
Main
Data processing object
methods
Object2 Object1
Data to be
processed Object4
Object3
10
What can an object do ?
• When you describe your program as a collection of
interacting objects, you need to have a clear idea what role
the objects will play in your application
• An object may play just one role, or several typical roles
Role Stereotypes
Collect, store and Provide services
provide data and computing
(data provider) (service provider)
An object
Object
• To interact with other objects, they Data
also include interface methods
• Interaction between objects is Interface Data
possible only through interface methods processing
methods methods
12
What does an object look like?
• Physically, it consists of binary data and Program memory layout
microprocessor instructions which occupy Object 1
a reserved block of memory
( its actual layout is system dependent )
Object 3
• Links between objects can be established
at the compilation time, or dynamically at System.out
run time
System.out is the standard output object that
• Logically, it must be declared in allows Java applications to display data
your program
• When an objects is declared it is class HelloWorldApp {
given a unique name public static void main(String[] args) {
// Display “Hello!"
• Looking through a program code, System.out.println(“Hello!");
you can see all objects declared }
there and follow the logical }
relationship between them
13
How can objects be created ?
Parts of mechanical systems Objects of a software system
are made based upon their are generated based upon their
description – blueprints description – classes
public class Circle {
public float x, y, r;
public Circle(float r) { this(0.0, 0.0, r); }
public float getArea() { return 3.14*r*r; }
}
Instantiation
15
Object intrinsic features
1. Identity
– Correct interaction between objects may not possible without
reliable identification of objects
– Each object has a unique identifier that is used to recognize it
public class Polygon {
. . . . .
Instantiation of objects presumes }
that all objects will be given
unique identifiers
• Objects instantiated from the same class have the same attributes,
but may have different states ( object IDs have to be different )
17
Object intrinsic features
3. Behaviour
• Instantiated objects are expected to act and react according to their
roles ( collect and store data, make decisions, provide services, etc )
• A set of supported actions defines object behaviour (what it does)
• Actions in OOD are called methods
ID: Circle1
• Some methods can change the
x: 3.0
object state (changeRadius, …).
y: 1.0
These methods are called mutators
r: 7.5
Methods • Some other methods can only
changeRadius() provide a value of an attribute
Determine
changeCoordinate() without changing the object state
what the
getMyRadius() object does (getMyRadius, …) These methods
getMyCoordinates() are called accessors.
calculateArea()
18
Fundamental OOD concepts
Encapsulation
• Some industrial products are sealed to prevent access to their
internal components. They can be used through external terminals
• OOD stipulates that to prevent unauthorized access to the most
critical object attributes and methods, they need to be placed into a
protective wrapper inside the object to make sure that:
- well designed and tested objects cannot be alternated or corrupted by
other poorly designed objects
- hidden components can be modified without affecting object interaction
( providing that interface methods are not affected by modifications)
• Attributes and Methods which are
specified as private are hidden inside private
objects and cannot be accessed from Attributes
outside public
methods private
• Methods specified as public can be Methods
used for interaction with other objects
19
Fundamental OOD concepts
Encapsulation
• According to OOD, interaction between objects can take place only
through public methods
• In general, all attributes should be specified as private. If they need to be
accessed from outside, this should be possible only through a limited set
of public methods ( accessors, or mutators )
• Methods which are irrelevant to object interaction also should be private
• If a private attribute, or a private method can be accessed directly from
outside, this indicates a serious design oversight (a safety bug)
Bank account
Internet Banking money
getBalance()
User interface transaction() payBill() Account
transfer() management
methods
20
Quiz
You need to implement an object Clock that
- counts time
- provides the current time on request
- can change on request the output format from 24hrs to AM/PM
- can set alarm
- can make beeping sound when alarm is on
21
Fundamental OOD concepts
Static attributes and static methods
Consider EnergyBill objects: EnergyBill1, EnergyBill2, EnergyBill3, . . .
ID: EnergyBill1 ID: EnergyBill2
BillerCode: 41225 BillerCode: 41225 • BillerCode will always
Rate: 0.25 Rate: 0.25 remain the same
energyUsed: 100 energyUsed: 200 therefore this attribute
totalAmount: 25.0 totalAmount: 50.0 should be a constant
dueDate: 10/11/17 dueDate: 15/11/17 • Rate may change, using
changeRate() method.
readEnegyUsed() readEnegyUsed()
This change has to be
calculateTotal() calculateTotal()
done simultaneously for
printBill() printBill()
all EnergyBill objects
changeRate() changeRate()
How to share a private attribute Rate among all objects, so
that if it is changed in one EneryBill, it will simultaneously
affect all other objects ?
22
Fundamental OOD concepts
Static attributes and static methods
Attributes and methods specified as static are shared
among all objects instances of the same class
ID: EnergyBill1 ID: EnergyBill2
BillerCode: 41225 BillerCode: 41225
static Rate: static Rate:
energyUsed: 100 0.25 energyUsed: 200
totalAmount: 25.0 changeRate() totalAmount: 50.0
dueDate: 10/11/17 dueDate: 15/11/17
readEnegyUsed() readEnegyUsed()
calculateTotal() calculateTotal()
printBill() printBill()
static changeRate() static changeRate()
a child object
Motorboat Smartphone
(or derived object)
Polymorphism
• Polymorphism means “many forms”
• Interaction between objects is not always 'hard-wired'
• Polymorphism allows interaction with a virtual object be redirected to
different actual objects depending on the context
an abstract device actual devices
playbackStream () StreamPlayer
27
Verbal description, ambiguity, confusion,…
30
UML class symbol
• The most basic class symbol is a box with the class name.
It is used when class properties are not important (usually at
initial stages of system design)
• Adding in more details may be needed at later stages
– Attributes, or data members
– Methods Java
31
UML class symbol
• Role stereotypes
• Visibility prefixes
Person
- name:
- age: + public attributes and methods
- height: - private attributes and methods
- rate: {read only}
constant
+ getName()
+ setName()
+ getHeight()
+ getAge()
+ setAge()
32
UML object symbol
<< Data Provider >> A class
Person
- name:
- age:
instantiation
Jack : Person
Specific values for
name = "Jack Smith" each attribute
age = 21 ( object state )
33
Class Relationship
34
Association
• Indicates that one class uses another class
• Association can be described as “has” or “uses”
type of relationship
uses
Lecturer Projector
has
Country Capital City
1
Name: name:
has
User Bank Account
0..*
35
Composition
• Composition is a special kind of association
• Composition reflects “contains” or “owns” type of
relationship
• Components “live” inside the container with their lifespan
synchronized with the container
• Deletion of the container destroys the component objects
Cube Vertex
8
PC
1..2 1..2 1 1
Processor Monitor Keyboard Mouse
36
Inheritance
• Inheritance implements an “is a” type of relationship
- Motorboat is a boat
- Smartphone is a mobile phone
• A child class inherits attributes and methods from its parent
class and adds new attributes and methods to implement
new properties and behaviours
Rifle
caliber:
length:
weight: M4 : Automatic Rifle
effective range: caliber = 5.56
instantiation length = 840
is a weight = 3.4
is a
effective range = 500
rate of fire = 950
Sniper Rifle Automatic Rifle
This instantiated object has inherited
telescopic site: rate of fire:
all properties of the parent class
37
Quiz
• What is the most appropriate relationship between
the following classes:
Table
Desktop
Legs
Table
1 3..4
Desktop Leg
38
Quiz
• What is the most appropriate relationship between
the following classes:
Passenger
Ticket
Timetable
uses 0..*
Passenger Timetable
has a
1..*
Ticket
39
Quiz
• What is the most appropriate relationship between
the following classes:
Passenger
Train passenger
Airplane passenger
Passenger
40
Quiz
• What is the most appropriate relationship between
the following classes:
Passenger
Airplane passenger
Luggage
Passenger
is a
has
Airplane passenger Luggage
41
Defining classes
42
Scenario: A big heavy red cat seats on a mat
• A question we need to answer:
– What objects are needed to implement this scenario
and what are their properties?
• Objects?
– Cat, Mat
• Cat:
– Attributes?
• Big Size? Width, Height, Depth?
• Heavy Weight
• Red Colour Association
– Methods/Behaviours? uses
• Measure Size Test for “bigness” Cat Mat
• Measure Mass Test for “heaviness”
• Show colour - size: - isSeated:
• Sitting on the mat (interaction) - weight:
+ checkIt()
- color:
• Mat: + seatOn()
– Behaviours? + getSize()
• Can be sat on + getWeight()
+ getColor()
• Classes: Cat, Mat, … ?
43
UML Design Tools
Enterprise Architect
• One of the most advanced and
most widely used tools in the
industry
• Supports all UML diagrams
• Checks correctness of models
• Dynamic model simulation
• Generation of documentation
and reports in a specified format
• Generation of Java, C++, C#
source code from UML models
• Helps to visualize your
applications by supporting
reverse engineering
44
Suggested reading
45