11 OOAD Overview
11 OOAD Overview
11 OOAD Overview
2
©Ian Sommerville 2000
OOAD
• Focuses on objects where system is broken down in terms of the
objects that exist within it.
3
Advantages of OOD
• Easier maintenance. Objects may be
understood as stand-alone entities
• Objects are appropriate reusable components
• For some systems, there may be an obvious
mapping from real world entities to system
objects
4
©Ian Sommerville 2000
OO Design vs. OO Programming
• Object-Oriented Design
• a method for decomposing software architectures
• based on the objects every system or subsystem manipulates
• relatively independent of the programming language used
• Object-Oriented Programming
• construction of software systems as
• Structured collection of Abstract Data Types (ADT)
• Inheritance
• Polymorphism
• concerned with programming languages and implementation issues
5
Objects and object classes
• Objects are entities in a software system that represent instances of
real-world and system entities
• Object classes are templates for objects. They may be used to create
objects
• Object classes may inherit attributes and services from other object
classes
6
©Ian Sommerville 2000
Objects
• Object is an abstraction of something in a problem domain, reflecting
the capabilities of the system to keep information about it, interact
with it, or both.
• Objects are entities in a software system that represent instances of
real-world and system entities
7
Objects
Object Identity Behaviors State
8
Object Class
• Class is a description of a set of objects that share the same
attributes, operations, methods, relationship and semantics.
• Object classes are templates for objects. They may be used to create
objects.
• An object represents a particular instance of a class.
9
Term of objects
• Attributes: data items that define object.
• Operations: functions in a class that combine to form the behavior of
class.
• Methods: the actual implementation of a procedure (the body of
code that is executed in response to a request from other objects in
the system).
10
The Unified Modeling Language
11
©Ian Sommerville 2000
Employee object & class
Class Object
Employee Employee16
name: string name: John
address: string address: M Street No.23
dateOfBirth: Date dateOfBirth: 02/10/65
employeeNo: integer employeeNo: 324
socialSecurityNo: string socialecurityNo:E342545
department: Dept department: Sale
ma nager: Employee manager: Employee1
salary: integer salary: 2340
status: {current, left, retired} stauts:current
taxCode: integer taxCode: 3432
. .. ….
join () Eployee16.join(02/05/1997)
leave () Eployee16.retire(03/08/2005)
retire ()
changeDetails () Eployee16.changeDetail(“X
Street No. 12”) 12
Encapsulation and Data Hiding
• Packaging related data and operations together is called
encapsulation.
• Data hiding: hides the internal data from external by methods
(interface).
• Important in most design paradigms (not just OOAD)
13
Encapsulation
When classes are defined, programmers can specify that certain
methods or state variables remain hidden inside the class.
Visible Methods
u These variables and methods are accessible from within the
Hidden
class, but not accessible outside it. State
u The combination of collecting all the attributes of an object Variables
and
into a single class definition, combined with the ability to hide Methods
some definitions and type information within the class, is
known as encapsulation. Visible Variables
Class
Definition
14
Graphical Model of an Object
Instance balance()
variables
Methods
accountNumber()
15
Encapsulation
l private attributes and methods are encapsulated within the
class, they cannot be seen by clients of the class
l public methods define the interface that the class provides
to its clients
Customer
- numCustomers = 0
private attributes - MIN_BUDGET = 200
- name: String
- address: String
- budget: int
Customer class
16
Object communication
• Conceptually, objects communicate by message passing.
• Messages
• The name of the service requested by the calling object.
• Copies of the information required to execute the service
and the name of a holder for the result of the service.
• In practice, messages are often implemented
by procedure calls
• Name = procedure name.
• Information = parameter list.
17
©Ian Sommerville 2000
Object communication
• Objects communicate with each other by sending
messages
• a message is a method call from a message-sending object to
a message-receiving object
• a message consists of
• an object reference which indicates the message receiver
• a method name (corresponding to a method of the receiver), and
• parameters (corresponding to the arguments of the calling method)
• a message-receiving object is a server to a message-sending
object, and the message-sending object is a client of the
server
18
Message Passing
message
19
Message Passing
Customer SalesPerson
- numCustomers = 0
- MAX_ PRICE = 200
- MIN_BUDGET = 200
- name: String
- name: String
- employeeNo: String
- address: String
- commission: int
- budget: int
takeOrder
alex lawrence
client server
20
Generalisation and inheritance
• Objects are members of classes that define attribute types and
operations
• Classes may be arranged in a class hierarchy where one class (a super-
class) is a generalisation of one or more other classes (sub-classes)
• A sub-class inherits the attributes and operations from its super class
and may add new methods or attributes of its own
• Generalisation in UML is implemented as inheritance in OO
programming languages
21
©Ian Sommerville 2000
A generalisation hierarchy
Employee
Ma nager Programmer
budgetsControlled project
progLanguage
dateAppointed
22
Library Item
Catalogue Number Library class
Acquisition date
Cost hierarchy
Type
Status
Number of copies
Acquire ()
Catalogue ()
Dispose ()
Issue ()
Return ()
Reader Borrower
Affiliation Items on loan
Max. loans
Staff Student
Department Major subject
Department phone Home address
24
Multiple inheritance
• Rather than inheriting the attributes and services from a single parent
class, a system which supports multiple inheritance allows object
classes to inherit from several super-classes
• Can lead to semantic conflicts where attributes/services with the
same name in different super-classes have different semantics
• Makes class hierarchy reorganisation more complex
25
Multiple inheritance
Book Voice recording
Author Speaker
Edition Duration
Publication date Recording date
ISBN
Talking book
# Tapes
26
Advantages of inheritance
• It is an abstraction mechanism which may be used to classify
entities
• It is a reuse mechanism at both the design and the
programming level
• The inheritance graph is a source of organisational
knowledge about domains and systems
27
Problems with inheritance
• Object classes are not self-contained. They cannot be
understood without reference to their super-classes
• Designers have a tendency to reuse the inheritance graph
created during analysis. Can lead to significant inefficiency
• The inheritance graphs of analysis, design and
implementation have different functions and should be
separately maintained
28
Inheritance and OOD
• There are differing views as to whether inheritance is
fundamental to OOD.
• View 1. Identifying the inheritance hierarchy or network is a
fundamental part of object-oriented design. Obviously this can only
be implemented using an OOPL.
• View 2. Inheritance is a useful implementation concept that allows
reuse of attribute and operation definitions. Identifying an
inheritance hierarchy at the design stage places unnecessary
restrictions on the implementation
• Inheritance introduces complexity and this is undesirable,
especially in critical systems
29
Objects Association
studies
Student Course
0..* 1..*
30
An association model
Employee
is-member-of Department
is-managed-by
manages
Manager
31
©Ian Sommerville 2000
Object aggregation
• Aggregation model shows how classes that are collections are
composed of other classes
32
Object aggregation
Study pack
Course title
Number
Year
Instructor
1 1 1 1
1 1
1 ..* 1 ..*
Exercises Solutions
#Problems Text
Description Diagrams
33
Object Cohesion & Coupling
• Cohesion describes the relationships within modules.
• Cohesion of a component is a measure of how well it fits together. Each
operation provides functionality that allows the attributes of the object to be
modified, inspected or used as a basis for service provision.
• Coupling describes the relationships between modules
• Coupling is an indication of the strength of interconnections between
program units. Highly coupled systems have strong interconnections, with
program units dependent on each other (shared variables, interchange
control function). Loosely coupled system have program units that are
independent.
34
Polymorphism
name
Shape getName( )
calculateArea( )
radius side
Circle Square
calculateArea( ) calculateArea( )
35
Example Polymorphism
class Shape {
private String name;
public Shape(String aName) { name=aName; }
public String getName( ) { return name; }
public float calculateArea( ) { return 0.0f; }
} // End Shape class
a generic action
inheritance
class Circle extends Shape {
private float radius;
public Circle(String aName) { super(aName); radius = 1.0f; }
public Circle(String aName, float radius) {
super(aName); this.radius = radius;
} overloading
public float calculateArea() { return (float)3.14f*radius*radius; }
} // End Circle class
overriding
36
class Square extends Shape {
private float side;
public Square(String aName) {
super(aName);
side = 1.0f;
}
public Square(String aName, float side) {
super(aName);
this.side = side;
}
public float calculateArea() {
return (float) side*side;
}
} // End Square class
37
Polymorphism Example
public class ShapeDemoClient {
public static void main(String argv[ ]) {
Shape c1 = new Circle("Circle C1");
Shape c2 = new Circle("Circle C2", 3.0f);
Shape s1 = new Square("Square S1");
Shape s2 = new Square("Square S2", 3.0f); rule of subtype
Shape shapeArray[ ] = {c1, s1, c2, s2};
for (int i = 0; i < shapeArray.length; i++) {
System.out.println("The area of " + shapeArray[i].getName( )
+ " is " + shapeArray[i].calculateArea( )
+ " sq. cm.");
} dynamic binding
} // End main
} // End ShapeDemoClient1 class
38
OO Analysis and Design
OO Analysis - examines requirements from the perspective of the classes and
objects found in the vocabulary of the problem domain. In other words, the
world (of the system) is modelled in terms of objects and classes.
39
Continuous cycles Sequential/linear stages
Small, high-functioning, collaborative teams Upfront planning and in-depth documentation
Flexible/continuous evolution Works best with well-defined deliverables
Customer involvement Close project manager involvement
40