You are on page 1of 46

eMacao- 14- 03eMacao- 14- 03- 1

Object-Oriented Principles
Robert P. Biuk-Aghai BiukDepartment of Computer and Information Science Faculty of Science and Technology University of Macau robertb@umac.mo

eMacao- 14- 03eMacao- 14- 03- 2

Overview
Why Object-Orientation? ObjectThe Object Model OO vs. Procedure -Oriented Approach ProcedureFundamental OO Concepts Examples Conclusions

eMacao- 14- 03eMacao- 14- 03- 3

Why Object-Orientation?

eMacao- 14- 03eMacao- 14- 03- 4

Why Object-Orientation?
The software crisis
n

Software is of poor quality Software is expensive to produce Software complexity is continuously increasing

eMacao- 14- 03eMacao- 14- 03- 5

Why Object-Orientation? (cont (cont d.)


Specific problems:
n

30% of large software projects are cancelled before completion 50% of software projects are over budget by more than 200% The majority of completed projects deliver 60% or less of prescribed functionality Many delivered information systems are under - used underbecause they don meet user needs and/or expectations don t Legacy systems are a serious and growing bottleneck to organizational evolution

ObjectObject-orientation: a possible way out of the crisis

eMacao- 14- 03eMacao- 14- 03- 6

Key Quality Concerns


Correctness
n

The ability of software products to perform their exact tasks, as defined by their specifications. [Mey97] specifications. The ability of software systems to react appropriately to abnormal conditions. [Mey97] conditions. Producing bug -free software Correcting software bugs

Robustness
n

Difficulties:
n n

Common problem: software reliability

eMacao- 14- 03eMacao- 14- 03- 7

Key Quality Concerns (cont (cont d.)


Reusability
n

The ability of software elements to serve for the construction of many different applications. [Mey97] applications. The ease of adapting software products to changes of specification. specification. [Mey97] Changing software is hard Hard to apply software outside its original intended use

Extendibility
n

Difficulties:
n n

Common problem: software modularity

eMacao- 14- 03eMacao- 14- 03- 8

Other Quality Concerns


Compatibility
n

The ease of combining software elements with others. The ease of transferring software products to various hardware and software environments The ease with which people of different backgrounds can learn to use software products The ability of a software system to place as few demands as possible on hardware resources

Portability
n

Ease of use
n

Efficiency
n

Timeliness, economy and functionality

eMacao- 14- 03eMacao- 14- 03- 9

Object -Orientation
A way of thinking about a problem
n n

Regard the world as consisting of interacting objects Aim to understand objects, their properties, operations, and interactions with other objects

A way of solving problems


n n

Build systems consisting of representations of objects Objects interact with each other

eMacao- 14- 03eMacao- 14- 03- 10

Objects Example
Loudspeaker Data Projector

Microphone PC

OHP Chair

eMacao- 14- 03eMacao- 14- 03- 11

Strengths of Object Orientation


Describing large, complex systems as interacting objects make s them easier to understand than makes otherwise The behaviours of real world objects tend to be behaviou stable over time The different kinds of real world objects tend to be stable (i.e., new kinds appear slowly; old kinds disappear slowly) Changes tend to be localized to a few objects

eMacao- 14- 03eMacao- 14- 03- 12

Strengths of Object Orientation (cont (cont d)


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 n n

More accurately describe corporate data and processes Decomposed based on natural partitioning Easier to understand and maintain A small change in requirements does not mean massive changes in the system under development

Stability
n

eMacao- 14- 03eMacao- 14- 03- 13

Strengths of Object Orientation (cont (cont d)


Overall benefits of OO: Proper use of OO methods enhances key software quality factors of a system and its constituent components

eMacao- 14- 03eMacao- 14- 03- 14

OOA, OOD, OOP


Application of object-orientation throughout the objectsoftware life-cycle: lifeEvaluation Analysis Design Coding Testing Maintenance

eMacao- 14- 03eMacao- 14- 03- 15

OOA: Object-Oriented Analysis


Definition:
n

ObjectObject-oriented analysis is a method of analysis that examines requirements from the perspective of the classes and objects found in the vocabulary of the problem domain. [Boo94]

Supported by OO modeling methods:


n n n

UML OMT etc.

eMacao- 14- 03eMacao- 14- 03- 16

OOD: Object-Oriented Design


Definition:
n

ObjectObject-oriented design is a method of design encompassing the process of object-oriented objectdecomposition and a notation for depicting both logical and physical as well as static and dynamic models of the system under design. [Boo94]

Supported by OO design methods:


n n n

UML OMT etc.

eMacao- 14- 03eMacao- 14- 03- 17

OOP: Object-Oriented Programming


Definition:
n

ObjectObject-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships. [Boo94] relationships. C++ Java Smalltalk Eiffel etc.

Supported by OO programming languages:


n n n n n

eMacao- 14- 03eMacao- 14- 03- 18

The Object Model

eMacao- 14- 03eMacao- 14- 03- 19

The Object Model


A sound engineering foundation [Boo94] foundation A different way of thinking about software (i.e. as a set of interacting objects) Four basic principles:
1. 2. 3. 4.

Encapsulation

Abstraction

Modularity

eMacao- 14- 03eMacao- 14- 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 Abstraction is relative to the perspective of the viewer Many different views of the same object are possible

Hierarchy

Abstraction Encapsulation Modularity Hierarchy

Object Orientation

eMacao- 14- 03eMacao- 14- 03- 21

Different Viewpoints of an Object

eMacao- 14- 03eMacao- 14- 03- 22

Source: Boo94

Examples of Abstraction
Employee

Customer

Product

eMacao- 14- 03eMacao- 14- 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 Objects exhibit an interface through which others can interact with it Hides complexity from an object clients object s

eMacao- 14- 03eMacao- 14- 03- 24

Encapsulation (cont (cont d.)


Encapsulation separates the implementation of an object object behaviour from its public interface s It is called information hiding , it allows an object hiding object s behaviour to be used without knowing its implementation (black box) ( box Offers two kinds of security:
n

Protects object internal state from being changed by object s outside users Changes can be done to the behaviour implementation without affecting other objects

eMacao- 14- 03eMacao- 14- 03- 25

Encapsulation: Hiding Object Details

eMacao- 14- 03eMacao- 14- 03- 26

Source: Boo94

Example of Encapsulation
Hide implementation from clients
n

Clients depend only on the interface, not the implementation

eMacao- 14- 03eMacao- 14- 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]

Module: a collection of related classes of objects Key concepts:


n

Modules are cohesive (performing a single type of tasks) Makes modules more reusable Modules are loosely coupled (highly independent) Makes modules more robust and maintainable

eMacao- 14- 03eMacao- 14- 03- 28

Modularity (cont (cont d.)


Modularity can be defined as the process of breaking up of a complex system into small, self selfcontained pieces that can be managed easily Packages and subsystems support the definition of the modularity

eMacao- 14- 03eMacao- 14- 03- 29

Modularity: Separation into Pieces

eMacao- 14- 03eMacao- 14- 03- 30

Source: Boo94

Example of Modularity
The breaking up of something complex into manageable pieces
Order Entry

Order Processing System

Order Fulfillment

Billing

eMacao- 14- 03eMacao- 14- 03- 31

Hierarchy
Definition:
n

Hierarchy is a ranking or ordering of abstractions. [Boo94]

Types of hierarchies:
n n n n n n

Class Aggregation Containment Inheritance Partition Specialization

eMacao- 14- 03eMacao- 14- 03- 32

Hierarchy of Abstractions

Source: Boo94

eMacao- 14- 03eMacao- 14- 03- 33

Example of Hierarchy
Levels of abstraction
Increasing abstraction
Asset

BankAccount

Security

RealEstate

Savings

Checking

Stock

Bond

Decreasing abstraction

Classes at the same level of the hierarchy should be at the same level of abstraction

eMacao- 14- 03eMacao- 14- 03- 34

Object-Oriented vs. Procedure-Oriented Approach

What is Procedure-Oriented Programming?

eMacao- 14- 03eMacao- 14- 03- 35

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-down approach in program design top-

Relationship of Data & Functions in Procedural Programming

eMacao- 14- 03eMacao- 14- 03- 36

Global Data

Global Data

Function 1 Local Data

Function 2 Local Data

Function 1 Local Data

What is Object Oriented Programming?

eMacao- 14- 03eMacao- 14- 03- 37

Emphasis is on data rather than procedure Programs are divided into objects (abstracted into classes) classes ) Data is encapsulated in the objects Objects communicate with each other through functions (called methods ) methods) Follows bottom -up approach in program design bottom-

Organization of Data & Functions in Object -Oriented Programming


Object A Data Functions Object C Data Functions Object B Data Functions

eMacao- 14- 03eMacao- 14- 03- 38

eMacao- 14- 03eMacao- 14- 03- 39

Fundamental OO Concepts

eMacao- 14- 03eMacao- 14- 03- 40

Basic Concepts of Object Orientation


Object Class Attribute Operation State Behaviour Identity Relationships Polymorphism

eMacao- 14- 03eMacao- 14- 03- 41

Object
An object represents an entity Objects can be:
n n

Physical or conceptual Tangible or intangible

Objects are concrete Examples of objects:


n

A building An employee A form

eMacao- 14- 03eMacao- 14- 03- 42

Class
A class represents a type of object A class is a definition of an object
n

It defines object structure and behaviour behaviou

A class is a template (or blueprint) for producing blueprint objects of a certain type Every object is an instance of a class Examples of classes:
n n n

Building Employee Form

eMacao- 14- 03eMacao- 14- 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 n

Emphasizes relevant characteristics Hides other characteristics

A class is an encapsulation: encapsulation:


n n

A set of properties (attributes) A set of operations

eMacao- 14- 03eMacao- 14- 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] An attribute has a type that defines the type of its instances The given set of values of the attributes define s the defines state of the object Only the object itself should be able to change the value of its attributes

eMacao- 14- 03eMacao- 14- 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- 03eMacao- 14- 03- 46

Operation (Method)
An operation is the implementation of a service that can be requested from any object of the class to affect behavio ur. [Boo99] behaviou Boo99] An operation can be:
n n

Question (does not change the value of the object) Command (may change the value of the object)

eMacao- 14- 03eMacao- 14- 03- 47

Example: Operations
public class Employee { protected int employeeNo; protected String surName; protected String givenName; protected String jobTitle; protected int salary; public void assignWork(Task t) {} public void doWork() {} public void submitResult(Result r) {} }

Operations

eMacao- 14- 03eMacao- 14- 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

eMacao- 14- 03eMacao- 14- 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]

Properties of an object are fixed (static) Property values of an object change (dynamic)

eMacao- 14- 03eMacao- 14- 03- 50

Example: Object State


Example object state for employee Chan Tai Man: Man:
n n n n n

employeeNo: 1234 surName: "Chan" givenName: "Tai Man" jobTitle: "Tecnico 1a Classe" salary: 400

eMacao- 14- 03eMacao- 14- 03- 51

Behaviour
Definition:
n

Behaviour is how an object acts and reacts, in terms of its state changes and message passing. [Boo94] passing.

Example behaviours for employee Chan Tai Man: Man:


n

Updating value of property salary Assigning work (invoking method assignWork() in the corresponding object)

eMacao- 14- 03eMacao- 14- 03- 52

Example: Behaviour
Example of behaviour: object state change; salary of employee Chan Tai Man is increased

change of property value

An action that updates any of the object property values s causes a change of the object state s

eMacao- 14- 03eMacao- 14- 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 (in Macau, Portugal) Mans
oid = 1 oid = 2

eMacao- 14- 03eMacao- 14- 03- 54

Modeling Classes
Modeling more classes of the domain:

eMacao- 14- 03eMacao- 14- 03- 55

Class Relationships
Modeled classes can be related in multiple ways:
n n n n n n

Association Inheritance Aggregation Using Instantiation Metaclass

The most important and common types of relationships:


n

Association, inheritance

eMacao- 14- 03eMacao- 14- 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: One-to-one: one instance of the first class associated with one instance of the second class One-to-many: One-to-many: one instance of the first class associated with many instances of the second class Many-toMany-to- many: many instances of the first class associated with many instances of the second class

Associations are usually bidirectional (association applies both ways)

eMacao- 14- 03eMacao- 14- 03- 57

Association Example
Example associations:
n

Employee is head of agency / agency is headed by employee (1:1) Employee works for agency / agency provides work for employee (1:N)

eMacao- 14- 03eMacao- 14- 03- 58

Association Example 2
SelfSelf -association:
n

Agency is associated to agency (parent agency) (0 (0 1 : 0 N) 0 Gabinete Direc Direco Departamento Diviso Sec Seco

eMacao- 14- 03eMacao- 14- 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 -a relationship: isn n

The subclass is a specialization of the superclass The superclass is a generalization of the subclass

Inheritance is a key concept of object-orientation objectClass modeling seeks to identify inheritance relationships between classes

eMacao- 14- 03eMacao- 14- 03- 60

Inheritance Example
Multiple classes with common attributes:
n n

surName givenName

identify a common superclass

eMacao- 14- 03eMacao- 14- 03- 61

Inheritance Example (cont (cont d.)


Added common superclass Person Attributes surName, givenName moved to Person, Person, inherited by its subclasses
Attributes of Employee: employeeNo jobTitle salary agency + inherited: surName givenName

eMacao- 14- 03eMacao- 14- 03- 62

Inheritance Example (cont (cont d.)


public abstract class Person { protected String surName; protected String givenName; } public class Employee extends Person { protected int employeeNo; protected String jobTitle; protected int salary; protected Agency agency; }

eMacao- 14- 03eMacao- 14- 03- 63

Abstract / Concrete Classes


A class may be abstract or concrete Definitions of abstract class: class:
n n

A class that cannot be directly instantiated. [BRJ99] An abstract class is written with the expectation that its concrete subclasses will add to its structure and behaviour. [Boo94] A class that can be directly instantiated. [BRJ99] A class whose implementation is complete and thus may have instances. [Boo94]

Definitions of concrete class: class:


n n

eMacao- 14- 03eMacao- 14- 03- 64

Abstract / Concrete Classes Example


Abstract class: Person
n

No objects of this class can be created Possible to create objects of these classes

Concrete classes: Employee , Citizen , Visitor Employee, Citizen,


n

eMacao- 14- 03eMacao- 14- 03- 65

Abstract / Concrete Classes Inheritance . Concrete classes Example

Objects

Abstract class
eMacao- 14- 03eMacao- 14- 03- 66

Polymorphism
Polymorphism is the ability to hide many implementations behind the same interface Interface is the outside view of a class, consisting of its public attributes and operations. Example:
n n

All employees can apply for leave The process of leave application is implemented differently for different kinds of employees (e.g. agency heads vs. ordinary employees)

eMacao- 14- 03eMacao- 14- 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. [B RJ99] component. RJ99

An interface is a contract of related services and a set of conditions that must be true for the contract to be faithfully executed Interfaces formalize polymorphism, they allow us to define polymorphism in a declarative way unrelated to implementation

eMacao- 14- 03eMacao- 14- 03- 68

Examples

eMacao- 14- 03eMacao- 14- 03- 69

Example 1: Objects
Geometric shapes:
n

Many different shapes, but some commonalities (area, circumference, width, height, centre, etc.)

eMacao- 14- 03eMacao- 14- 03- 70

Example 1: Classes
Define different classes for different kinds of objects

Triangle

Pentagon

Hexagon

Octagon

Ellipse

Parallelogram

Trapezoid

Rectangle

Square

Circle

eMacao- 14- 03eMacao- 14- 03- 71

Example 1: Class Hierarchy


Organize different classes as a hierarchy
Shape Polygon Triangle Quadrilateral Pentagon Hexagon Ellipse Octagon Circle

Parallelogram Rectangle Square

Trapezoid

eMacao- 14- 03eMacao- 14- 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

eMacao- 14- 03eMacao- 14- 03- 73

Example 1: Polymorphism
Different implementations of getArea() method:
n n n

Circle (radius r ): p r 2 Rectangle (vertexes a, b, c , d): a b 2 Square (vertexes a , b, c, d): a

Different implementations of getCircumference() method:


n n n

Circle (radius r ): 2 p r Rectangle (vertexes a, b, c , d): 2 a + b ) ( Square (vertexes a , b, c, d): 4 a

eMacao- 14- 03eMacao- 14- 03- 74

Example 1: Abstract Class Shape


public abstract class Shape { public abstract double getArea(); public abstract double getCircumference(); }

eMacao- 14- 03eMacao- 14- 03- 75

Example 1: Class Circle


public class Circle extends Shape { protected double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { return radius * radius * Math.PI; } public double getCircumference() { return radius * 2 * Math.PI; } }

eMacao- 14- 03eMacao- 14- 03- 76

Example 1: Class Rectangle


public class Rectangle extends Shape { protected double width; protected double height; public Rectangle(double width, double height) { this.width = width; this.height = height; } public double getArea() { return width * height; } public double getCircumference() { return 2 * (width + height); } }

eMacao- 14- 03eMacao- 14- 03- 77

Example 1: Class Square


public class Square extends Rectangle { protected double width; protected double height; public Square(double vertex) { super(vertex, vertex); } public double getArea() { return width * height; } public double getCircumference() { return 4 * width; } }

eMacao- 14- 03eMacao- 14- 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 n n

inherit properties / operations override properties / operations add new properties / operations

Common interface allows different classes to be used interchangeably Different implementations through polymorphism

eMacao- 14- 03eMacao- 14- 03- 79

Example 2: Objects
A citizen registers a complaint against a government agency Objects (highly simplified view):
n n n n

Citizen Lei Wai Keong Complaint Agency DSSD Complaint Handler Chan Tai Man
Complaint Complaint

Agency DSSD Citizen Lei Wai Keong Complaint Handler Chan Tai Man

eMacao- 14- 03eMacao- 14- 03- 80

Example 2: Classes

eMacao- 14- 03eMacao- 14- 03- 81

Example 2: Classes (cont (cont d.)


Class Agency : Agency:
n n

Provide operation postComplaint() to citizens postComplaint() Queues complaints with the responsible ComplaintHandler Processes complaints May forward complaints to other agencies Allows Person to post complaint (citizen, visitor, employee, etc.)

Class ComplaintHandler: ComplaintHandler:


n n

Class Complaint: Complaint:


n

eMacao- 14- 03eMacao- 14- 03- 82

Example 2: Objects

eMacao- 14- 03eMacao- 14- 03- 83

Example 2: Class Complaint


public class { protected protected protected protected protected } Complaint Person complainant; Agency complainee; String subject; String description; DateTime complaintDateTime;

eMacao- 14- 03eMacao- 14- 03- 84

Example 2: Class Agency


public class { protected protected protected protected Agency String name; String address; Employee head; Agency parentAgency;

public void postComplaint(Complaint c) {} }

eMacao- 14- 03eMacao- 14- 03- 85

Example 2: Class ComplaintHandler


public class ComplaintHandler extends Employee { protected Queue q; public void queueComplaint(Complaint c) {} public void processNextComplaint() {} public void forwardComplaint(Agency a) {} }

eMacao- 14- 03eMacao- 14- 03- 86

Example 2: Interaction between Classes

Note: Diagram shows the case when the complaint needs to be forwarded to another agency

eMacao- 14- 03eMacao- 14- 03- 87

Example 2: Discussion
Models are generic
n n

Independent of any particular agency Basis for extension to specific requirements

Classes are loosely -coupled looselyn

Can be reused in other applications add other properties / operations

Classes interact through well -defined interfaces welln

If necessary, classes can modify their implementations (within the scope of specified interfaces) without affecting other classes

eMacao- 14- 03eMacao- 14- 03- 88

Conclusions

eMacao- 14- 03eMacao- 14- 03- 89

Virtues of Object-Orientation
Encapsulation & data abstraction
n

Easier to modularize code, for better development and debugging Functionality is localized where it is used

Inheritance
n n

Improves program organization Fosters code reuse, sharing of libraries

eMacao- 14- 03eMacao- 14- 03- 90

Summary
OO methods regard the world as composed of interacting entities
n n

Identify abstractions of entities classes Identify relationships between entities association, inheritance, etc. Hide details of implementation from clients, allow different implementations of interfaces polymorphism

OO methods have the potential to result in higher quality, more modular & reusable software

eMacao- 14- 03eMacao- 14- 03- 91

References
[Boo94] Grady Booch, Object-Oriented Analysis and Design With Booch, ObjectApplications, Applications , Benjamin Cummings, 1994 [BRJ99] Grady Booch, James Rumbaugh, Ivar Jacobson, The Unified Booch, Rumbaugh, Modeling Language User Guide, Addison Wesley, 1999 Guide, [Mey97] Bertrand Meyer, Object-Oriented Software Construction, 2nd ed., ObjectConstruction, Prentice Hall, 1997 [MK04] Jim McKim, Object Oriented Programming and Design: Object McKim, Oriented Concepts, 2004 Concepts, [Pap01] Petraq Papajorgji, Introduction to Object Oriented Design and Papajorgji, Unified Modeling Language (UML), 2001 (UML), [RBPEL91] James Rumbaugh, Michael Blaha, William Premerlani, Rumbaugh, Blaha, Premerlani, Frederick Eddy, William Lorensen, Object-Oriented Modeling and Lorensen, ObjectDesign, Design, Prentice Hall, 1991 [Sch00] Douglas C. Schmidt , Object-Oriented Design and Programming, Schmidt, ObjectProgramming, 2000

You might also like