Академический Документы
Профессиональный Документы
Культура Документы
eMacao 14-- 03
03-- 1
Object-Oriented Principles
Robert P. Biuk-
Biuk-Aghai
Department of Computer and Information Science
Faculty of Science and Technology
University of Macau
robertb@umac.mo
eMacao-- 14
eMacao 14-- 03
03-- 2
Overview
Why Object-
Object-Orientation?
The Object Model
OO vs. Procedure-
Procedure -Oriented Approach
Fundamental OO Concepts
Examples
Conclusions
1
eMacao-- 14
eMacao 14-- 03
03-- 3
Why Object-Orientation?
eMacao-- 14
eMacao 14-- 03
03-- 4
Why Object-Orientation?
The software crisis
2
eMacao-- 14
eMacao 14-- 03
03-- 5
Specific problems:
n 30% of large software projects are cancelled before
completion
n 50% of software projects are over budget by more than
200%
n The majority of completed projects deliver 60% or less of
prescribed functionality
n Many delivered information systems are under-
under - used
because they don’
don’t meet user needs and/or expectations
n Legacy systems are a serious and growing bottleneck to
organizational evolution
eMacao-- 14
eMacao 14-- 03
03-- 6
3
eMacao-- 14
eMacao 14-- 03
03-- 7
Reusability
n “The ability of software elements to serve for the
construction of many different applications.”
applications. ” [Mey97]
Extendibility
n “The ease of adapting software products to changes of
specification.”” [Mey97]
specification.
Difficulties:
n Changing software is hard
n Hard to apply software outside its original intended use
à Common problem: software modularity
eMacao-- 14
eMacao 14-- 03
03-- 8
4
eMacao-- 14
eMacao 14-- 03
03-- 9
Object -Orientation
A way of thinking about a problem
n Regard the world as consisting of interacting objects
n Aim to understand objects, their properties, operations,
and interactions with other objects
eMacao-- 14
eMacao 14-- 03
03-- 10
Objects Example
Loud-
speaker
Data
Projector
Microphone OHP
PC Chair
5
eMacao-- 14
eMacao 14-- 03
03-- 11
eMacao-- 14
eMacao 14-- 03
03-- 12
A single paradigm
n Single language used by users, analysts, designers,
implementers
Facilitates architectural and code reuse
Models more closely reflect the real world
n More accurately describe corporate data and processes
n Decomposed based on natural partitioning
n Easier to understand and maintain
Stability
n A small change in requirements does not mean massive
changes in the system under development
6
eMacao-- 14
eMacao 14-- 03
03-- 13
eMacao-- 14
eMacao 14-- 03
03-- 14
Evaluation
Analysis
Design
Coding
Testing
Maintenance
7
eMacao-- 14
eMacao 14-- 03
03-- 15
eMacao-- 14
eMacao 14-- 03
03-- 16
8
eMacao-- 14
eMacao 14-- 03
03-- 17
eMacao-- 14
eMacao 14-- 03
03-- 18
9
eMacao-- 14
eMacao 14-- 03
03-- 19
Encapsulation
Hierarchy
Abstraction
4.
Modularity
Hierarchy
eMacao-- 14
eMacao 14-- 03
03-- 20
Abstraction
Definition:
n An abstraction denotes the essential characteristics of an
object that distinguish it from all other kinds of objects
and thus provide crisply defined conceptual boundaries,
relative to the perspective of the viewer. [Boo94]
Key concepts:
n Concentrating only on essential characteristics
à Allows complexity to be more easily managed
n Abstraction is relative to the perspective of the viewer
à Many different views of the same object are possible
10
eMacao-- 14
eMacao 14-- 03
03-- 21
Source: Boo94
eMacao-- 14
eMacao 14-- 03
03-- 22
Examples of Abstraction
Employee
Customer
Product
11
eMacao-- 14
eMacao 14-- 03
03-- 23
Encapsulation
Definition:
n Encapsulation is the process of compartmentalizing the
elements of an abstraction that constitute its structure
and behaviour; encapsulation serves to separate the
contractual interface of an abstraction and its
implementation. [Boo94]
Key concepts:
n Packaging structure and behaviour together in one unit
à Makes objects more independent
n Objects exhibit an interface through which others can
interact with it
à Hides complexity from an object’
object’s clients
eMacao-- 14
eMacao 14-- 03
03-- 24
Encapsulation (cont
(cont’’d.)
12
eMacao-- 14
eMacao 14-- 03
03-- 25
Source: Boo94
eMacao-- 14
eMacao 14-- 03
03-- 26
Example of Encapsulation
Hide implementation from clients
n Clients depend only on the interface,
not the implementation
13
eMacao-- 14
eMacao 14-- 03
03-- 27
Modularity
Definition:
n Modularity is the property of a system that has been
decomposed into a set of cohesive and loosely coupled
modules. [Boo94]
Key concepts:
n Modules are cohesive (performing a single type of tasks)
à Makes modules more reusable
n Modules are loosely coupled (highly independent)
à Makes modules more robust and maintainable
eMacao-- 14
eMacao 14-- 03
03-- 28
Modularity (cont
(cont’’d.)
14
eMacao-- 14
eMacao 14-- 03
03-- 29
Source: Boo94
eMacao-- 14
eMacao 14-- 03
03-- 30
Example of Modularity
The breaking up of something complex into
manageable pieces
Order
Entry
Order Processing
System Order
Fulfillment
Billing
15
eMacao-- 14
eMacao 14-- 03
03-- 31
Hierarchy
Definition:
n Hierarchy is a ranking or ordering of abstractions.
[Boo94]
Types of hierarchies:
n Class
n Aggregation
n Containment
n Inheritance
n Partition
n Specialization
eMacao-- 14
eMacao 14-- 03
03-- 32
Hierarchy of Abstractions
Source: Boo94
16
eMacao-- 14
eMacao 14-- 03
03-- 33
Example of Hierarchy
Levels of abstraction
Increasing Asset
abstraction
eMacao-- 14
eMacao 14-- 03
03-- 34
Object-Oriented vs.
Procedure-Oriented
Approach
17
eMacao-- 14
eMacao 14-- 03
03-- 35
What is Procedure-Oriented
Programming?
Emphasis is on doing things (algorithms)
Large programs are divided into smaller programs
(functions)
Most of the functions share global data
Data move openly around the system from function
to function
Functions transform data from one form to another
Employs top
top--down approach in program design
eMacao-- 14
eMacao 14-- 03
03-- 36
18
eMacao-- 14
eMacao 14-- 03
03-- 37
eMacao-- 14
eMacao 14-- 03
03-- 38
Object A Object B
Data Data
Functions Functions
Object C
Data
Functions
19
eMacao-- 14
eMacao 14-- 03
03-- 39
Fundamental OO Concepts
eMacao-- 14
eMacao 14-- 03
03-- 40
20
eMacao-- 14
eMacao 14-- 03
03-- 41
Object
An object represents an entity
Objects can be:
n Physical or conceptual
n Tangible or intangible
Objects are concrete
Examples of objects:
n A building
n An employee
n A form
eMacao-- 14
eMacao 14-- 03
03-- 42
Class
A class represents a type of object
A class is a definition of an object
n It defines object structure and behaviou
behaviour
A class is a template (or “blueprint
blueprint””) for producing
objects of a certain type
Every object is an instance of a class
Examples of classes:
n Building
n Employee
n Form
21
eMacao-- 14
eMacao 14-- 03
03-- 43
Class (cont
(cont’’d.)
Definition:
n A class is a set of objects that share a common structure
and a common behaviour. [Boo94]
A class is an abstraction
abstraction::
n Emphasizes relevant characteristics
n Hides other characteristics
A class is an encapsulation
encapsulation::
n A set of properties (attributes)
n A set of operations
eMacao-- 14
eMacao 14-- 03
03-- 44
Attribute (Property)
An attribute is a named property of a class that
describes the range of values that instances of the
property may hold. [Boo99
Boo99]]
22
eMacao-- 14
eMacao 14-- 03
03-- 45
Example: Attributes
public class Employee {
protected int employeeNo;
protected String surName;
protected String givenName;
protected String jobTitle;
protected int salary;
}
Attributes
eMacao-- 14
eMacao 14-- 03
03-- 46
Operation (Method)
An operation is the implementation of a service that
can be requested from any object of the class to
affect behaviou
behavio ur. [Boo99
Boo99]]
23
eMacao-- 14
eMacao 14-- 03
03-- 47
Example: Operations
eMacao-- 14
eMacao 14-- 03
03-- 48
Object Definition
Definition:
n An object has state, behaviour, and identity; the structure
and behaviour are defined in their common class; the
terms instance and object are interchangeable. [Boo94]
Example object:
n The employee Chan Tai Man
24
eMacao-- 14
eMacao 14-- 03
03-- 49
Object State
Definition:
n The state of an object encompasses all of the (usually
static) properties of the object plus the current (usually
dynamic) values of each of these properties. [Boo94]
eMacao-- 14
eMacao 14-- 03
03-- 50
25
eMacao-- 14
eMacao 14-- 03
03-- 51
Behaviour
Definition:
n Behaviour is how an object acts and reacts, in terms of
its state changes and message passing.
passing. [Boo94]
Example behaviours for
employee Chan Tai Man:
Man:
n Updating value of
property salary
n Assigning work
(invoking method
assignWork()
in the corresponding
object)
eMacao-- 14
eMacao 14-- 03
03-- 52
Example: Behaviour
Example of behaviour: object state change;
salary of employee Chan Tai Man is increased
26
eMacao-- 14
eMacao 14-- 03
03-- 53
Identity
Definition:
n Identity is that property of an object which distinguishes
it from all other objects. [Boo94]
Distinct objects remain distinct even when all their
property values are identical
Every object has a unique object identifier
Example: two Chan Tai Mans
Mans (in Macau, Portugal)
oid = 1 oid = 2
eMacao-- 14
eMacao 14-- 03
03-- 54
Modeling Classes
Modeling more classes of the domain:
27
eMacao-- 14
eMacao 14-- 03
03-- 55
Class Relationships
Modeled classes can be related in multiple ways:
n Association
n Inheritance
n Aggregation
n Using
n Instantiation
n Metaclass
eMacao-- 14
eMacao 14-- 03
03-- 56
Association
Association:
n A relationship denoting a semantic connection between
two classes. [Boo94]
Associations may have different cardinality
(multiplicity)
n One-to
One- to--one
one:: one instance of the first class associated
with one instance of the second class
n One--to
One to--many
many:: one instance of the first class associated
with many instances of the second class
n Many--to
Many to-- many: many instances of the first class
associated with many instances of the second class
Associations are usually bidirectional (association
applies both ways)
28
eMacao-- 14
eMacao 14-- 03
03-- 57
Association Example
Example associations:
n Employee is head of agency / agency is headed by
employee (1:1)
n Employee works for agency / agency provides work for
employee (1:N)
eMacao-- 14
eMacao 14-- 03
03-- 58
Association Example 2
Self--association:
Self
n Agency is associated to agency (parent agency)
(0…… 1 : 0…
(0 0 … N)
Gabinete
Direcçção
Direc
Departamento
Divisão
Secçção
Sec
29
eMacao-- 14
eMacao 14-- 03
03-- 59
Inheritance
Inheritance:
n A relationship among classes, wherein one class shares
the structure or behaviour defined in one or more other
classes. [Boo94]
A subclass inherits attributes and operations from
its superclass
Inheritance represents the is
is--a relationship:
n The subclass is a specialization of the superclass
n The superclass is a generalization of the subclass
Inheritance is a key concept of object-
object-orientation
Class modeling seeks to identify inheritance
relationships between classes
eMacao-- 14
eMacao 14-- 03
03-- 60
Inheritance Example
Multiple classes with common attributes:
n surName
n givenName
à identify a common superclass
30
eMacao-- 14
eMacao 14-- 03
03-- 61
Attributes of
Employee:
• employeeNo
• jobTitle
• salary
• agency
+ inherited:
• surName
• givenName
eMacao-- 14
eMacao 14-- 03
03-- 62
31
eMacao-- 14
eMacao 14-- 03
03-- 63
eMacao-- 14
eMacao 14-- 03
03-- 64
32
eMacao-- 14
eMacao 14-- 03
03-- 65
Objects
Abstract class
eMacao-- 14
eMacao 14-- 03
03-- 66
Polymorphism
Polymorphism is the ability to hide many
implementations behind the same interface
Example:
n All employees can apply for leave
n The process of leave application is implemented
differently for different kinds of employees (e.g. agency
heads vs. ordinary employees)
33
eMacao-- 14
eMacao 14-- 03
03-- 67
Interfaces
Definition:
n An interface is a collection of operations that are used to
specify a service of a class or a component.
component. [B RJ9
RJ999]
eMacao-- 14
eMacao 14-- 03
03-- 68
Examples
34
eMacao-- 14
eMacao 14-- 03
03-- 69
Example 1: Objects
Geometric shapes:
n Many different shapes, but some commonalities (area,
circumference, width, height, centre, etc.)
eMacao-- 14
eMacao 14-- 03
03-- 70
Example 1: Classes
Define different classes for different kinds of objects
Ellipse
Triangle Pentagon Hexagon Octagon
35
eMacao-- 14
eMacao 14-- 03
03-- 71
Polygon Ellipse
Parallelogram Trapezoid
Rectangle
Square
eMacao-- 14
eMacao 14-- 03
03-- 72
Example 1: Class
Hierarchy (cont
(cont’’d.)
Put common attributes
and operations in
superclasses
Put specializations of
attributes and
operations in
subclasses
n Subclasses inherit from
superclasses
36
eMacao-- 14
eMacao 14-- 03
03-- 73
Example 1: Polymorphism
Different implementations of getArea() method:
n Circle (radius r ): p ×r 2
n Rectangle (vertexes a, b, c , d): a ×b
n Square (vertexes a , b, c, d): a2
eMacao-- 14
eMacao 14-- 03
03-- 74
37
eMacao-- 14
eMacao 14-- 03
03-- 75
eMacao-- 14
eMacao 14-- 03
03-- 76
38
eMacao-- 14
eMacao 14-- 03
03-- 77
eMacao-- 14
eMacao 14-- 03
03-- 78
Example 1: Discussion
Design maximizes use of inheritance & polymorphism
Properties / operations are placed in classes as
high up in the class hierarchy as possible
Subclasses:
n inherit properties / operations
n override properties / operations
n add new properties / operations
Common interface allows different classes to be
used interchangeably
Different implementations through polymorphism
39
eMacao-- 14
eMacao 14-- 03
03-- 79
Example 2: Objects
A citizen registers a complaint against a
government agency
Objects (highly simplified view):
n Citizen Lei Wai Keong
n Complaint
n Agency DSSD
n Complaint Handler Chan Tai Man
Complaint Complaint
Agency
DSSD
eMacao-- 14
eMacao 14-- 03
03-- 80
Example 2: Classes
40
eMacao-- 14
eMacao 14-- 03
03-- 81
Class Agency
Agency::
n Provide operation postComplaint
postComplaint()
() to citizens
n Queues complaints with the responsible
ComplaintHandler
Class ComplaintHandler
ComplaintHandler::
n Processes complaints
n May forward complaints to other agencies
Class Complaint
Complaint::
n Allows Person to post complaint (citizen, visitor,
employee, etc.)
eMacao-- 14
eMacao 14-- 03
03-- 82
Example 2: Objects
41
eMacao-- 14
eMacao 14-- 03
03-- 83
eMacao-- 14
eMacao 14-- 03
03-- 84
42
eMacao-- 14
eMacao 14-- 03
03-- 85
eMacao-- 14
eMacao 14-- 03
03-- 86
43
eMacao-- 14
eMacao 14-- 03
03-- 87
Example 2: Discussion
Models are generic
n Independent of any particular agency
n Basis for extension to specific requirements
eMacao-- 14
eMacao 14-- 03
03-- 88
Conclusions
44
eMacao-- 14
eMacao 14-- 03
03-- 89
Virtues of Object-Orientation
Encapsulation & data abstraction
n Easier to modularize code, for better development and
debugging
n Functionality is localized where it is used
Inheritance
n Improves program organization
n Fosters code reuse, sharing of libraries
eMacao-- 14
eMacao 14-- 03
03-- 90
Summary
OO methods regard the world as composed of
interacting entities
n Identify abstractions of entities à classes
n Identify relationships between entities à association,
inheritance, etc.
n Hide details of implementation from clients, allow
different implementations of interfaces à polymorphism
45
eMacao-- 14
eMacao 14-- 03
03-- 91
References
[Boo94] Grady Booch
Booch,, Object
Object--Oriented Analysis and Design With
Applications,, Benjamin Cummings, 1994
Applications
[BRJ99] Grady Booch
Booch,, James Rumbaugh
Rumbaugh,, Ivar Jacobson, The Unified
Modeling Language User Guide,
Guide, Addison Wesley, 1999
[Mey97] Bertrand Meyer, Object Construction, 2nd ed.,
Object--Oriented Software Construction,
Prentice Hall, 1997
[MK04] Jim McKim
McKim,, Object Oriented Programming and Design: Object
Oriented Concepts,
Concepts, 2004
[Pap01] Petraq Papajorgji
Papajorgji,, Introduction to Object Oriented Design and
Unified Modeling Language (UML),
(UML), 2001
[RBPEL91] James Rumbaugh
Rumbaugh,, Michael Blaha
Blaha,, William Premerlani
Premerlani,,
Frederick Eddy, William Lorensen
Lorensen,, Object
Object--Oriented Modeling and
Design,, Prentice Hall, 1991
Design
[Sch00] Douglas C. Schmidt,
Schmidt , Object
Object--Oriented Design and Programming,
Programming,
2000
46