You are on page 1of 63

OO - Basics

29/09/2012

Introduction to Object Orientation Topics

Object technologies

Basic Principles of Object Orientation

Basic Concepts of Object Orientation

Strengths of Object Orientation

29/09/2012

Object-oriented programming

OOP means developing software based on objects


In

OOP, the data and the programming logic (the


code/functions), are both encapsulated in the object

Data

and code encapsulated (combined) in an object

Procedural or structural programming focused


mainly on functions
In

procedural programming, the data and the operations


that manipulate the data (the code) are separate

Data

is separate from the programming logic (the


code/functions)

Object-orientation puts the focus on the


combination of function and data

Evolution of general-purpose programming languages

Machine code (1940s and 1950s)


Dependent

No

on hardware architecture

distinction between of executable instructions and data

Assembler code (1950s1970s)


Relocatable code

- no fixed addresses in code but still


hardware-specific

Branch

or link instructions led to spaghetti code

Executable instructions and

data still

Programmable

in modules with link-load to create


executable whole
4

Evolution of general-purpose programming languages

Third generation (1960s to 1990)


Compiler

translate high level languages such as Fortran,


PL1, COBOL, RPC, C and many more to assembled code

Source

is platform dependent, executable code is hardware


specific

Separation of

code and data

Subprograms/subroutines/functions/methods

Interpreted languages (1980 to 1990)


Just-in-time translation from
BASIC,

source to executable code

LISP, APL, APL and many more


5

Evolution of programming styles

Third generation languages enabled coding


practices aimed at improving code quality,
reliability, readability and maintainability
Procedural:

Sequential execution
Conditional execution, loops, go to or branch to alter flow of
control
Subroutine call and return

Structured:

Decompose functionality into callable subprograms


Replace go to with nested blocks of code
Extensive use of subroutine call and return
6

Evolution of programming styles


Modular

programming:

Group subroutines with related functionality in to modules

Object oriented

Paradigm shift to reusable objects with data and behavior

Service oriented

Paradigm shift to reusable objects with data and behavior

Some object-oriented programming languages

Simula and Simula 67 (1960s)


Early precursor

to OO

Smalltalk (1970s to 1990s)


Pure

OO language in part derived in part from Simula

Introduced message

passing metaphor, class hierarchies

Distinctive integrated development

environment with

visual coding

Some object-oriented programming languages

C++ (1980s to 1990s)


A

hybrid of C grammar with object constructs added to the


language

Difficult to
Very

master: often used like C

difficult to implement an efficient compiler

Java (1990s to present day)


Architectural neutral interpreted OO

language from Sun

Microsystems
Syntax

similar to C++, but greatly simplified

Evolved

into dominant programming platform


9

Why object-oriented programming?

Object-oriented programming enables:


A

natural way of thinking


About the problem domain, not about processes and procedures.

Ability to

Adapt to changing requirements


Adopt new, better, or cheaper technology with minimal impact
Manage the extensibility of a system

Code

support change:

reuse

Not just copy, paste and then edit


Use existing as is and build upon it

Creation of

code that can be maintained and updated


10

Considering data and function

Procedural approach:
Data

No

and function are handled separately.

common way to access data can be enforced easily.

Object-oriented approach:
Data

and function are combined in objects.

Function becomes the behavior of objects.

The

use of an object only requires knowledge of the


function names.

This

hiding the data and methods (functions) inside objects


is called encapsulation.
11

The procedural approach to data and function

12

The OO approach to data and function

13

Approaches to modeling and coding

Procedural approach

Model only the business processes or the data or each separately.

The two are not modeled together.

The concept of the business and the pieces of the business are lost.

The solution becomes just a collection of functions.

OO approach

Model the business: Both structure and process.

Allows for better communication from user to analyst to designer to


programmer because they are all speaking of the business.

Allows the ability to more easily capture changes to the structure and to
the process.

14

Modeling and coding example

Requirement:
Model

withdrawing money from a savings account.

Procedural approach
Identify where
List the

the data is stored.

algorithmic steps necessary.

Object approach
Identify what

objects are involved.

Example: Bank, savings account, teller and transaction


Show

how these objects interact to:

Enforce business rules for withdrawals.


Decrement the balance.

15

Code reuse is a key benefit of OOP

Procedural

Hard to identify opportunities to reuse code because of the lack of


encapsulation

Reusability happens at the procedural level

Objects

Reuse of code, writing functions once and using them again in other
objects (similar to procedural).

Reuse of objects or a set of objects, since these are business objects


they can be reused throughout the enterprise.

Reuse of function names or sets of function names, in the procedural


approach multiple functions cannot have the same name, however with
objects, a function name can be reused for different objects with
different implementations.
16

Code reuse is limited in procedural code

17

OOP facilitates reuse of code

18

OOP facilitates reuse of objects

19

OOP facilitates reuse of names

20

Communication in the development team

Procedural approach

Differences documents and styles of working are used during system


analysis, design, and programming.

Each stage ends with a milestone or sign off.

The resulting throw it over the wall approach can lead to


miscommunication.

Object-oriented approach

The same language is used for analysis, design, implementation

The language is objects


It is often written visually in the Unified Modeling Language (UML)

This leads to better communication between analyst, designer and


programmer.

21

Benefits provided by objects

22

Basic Principles of Object Orientation

23

29/09/2012

Hierarchy

Modularity

Encapsulation

Abstraction

Object Orientation

What Is Abstraction?

The essential characteristics of an entity that


distinguishes it from all other kinds of entities.

Defines a boundary relative to the perspective of


the viewer.

Is not a concrete manifestation, denotes the ideal


essence of something.

24

29/09/2012

What is Abstraction?

Salesperson
Not saying
Which
salesperson
just a
salesperson
in general!!!
Customer

Product

Manages Complexity
25

29/09/2012

What Is Encapsulation?

Hides implementation from clients.


Clients depend

on interface.

Improves Resiliency
26

29/09/2012

Encapsulation

"Encapsulation is a mechanism used to hide the


data, internal structure, and implementation details
of an object. All interaction with the object is
through a public interface of operations."
(Craig Larman)

A boundary exists around each object; the


boundary encapsulates the objects characteristics
(data elements) and behaviors (functionality).

The reason for hiding features is to:

27

(1) keep users from touching parts of the object they


shouldnt touch;
(2) allows creator of the object to change the objects internal
29/09/2012
working without affecting
the users of the object.

What is Modularity?

The breaking up of something complex into


manageable pieces

Helps people understand complex systems.


Order
Entry
Order Processing
System

Order
Fulfillment
Billing

Manages Complexity
28

29/09/2012

What is Hierarchy?

Levels of abstraction

Asset

Increasing
abstraction

Security

BankAccount

Savings Checking
Decreasing
abstraction
29

Stock

RealEstate

Bond

Elements at the same level of the hierarchy


should be at the same level of abstraction
29/09/2012

Basic Concepts of Object Orientation

Object

Class

Attribute

Operation

Interface (Polymorphism)

Component

Package

Subsystem

Relationships

30

29/09/2012

What is an Object?

Informally, an object represents an entity, either


physical, conceptual, or software
Physical entity

Conceptual entity

Software

Truck

Chemical Process

entity
Linked List

31

29/09/2012

A More Formal Definition

An object is a concept, abstraction, or thing with


sharp boundaries and meaning for an application

An object is something that has:


State
Behavior

Identity

32

29/09/2012

A real live object

33

A person object

34

What is a Class?

A class is a description of a group of objects with


common properties (attributes), behavior
(operations), relationships, and semantics
An

object is an instance of a class

A class is an abstraction in that it:


Emphasizes
Suppresses

relevant characteristics

other characteristics

OO Principle: Abstraction
35

29/09/2012

What is necessary to define a class

Name
The

class name needs to specify what the abstraction is


capturing. The name of the class should:

Be singular
Not be too vague or general

Clearly identify the concept


Be short and concise

List of data elements


The

pieces of data that need to be captured for the


abstraction.

List of messages
The

messages (and their implementation, methods) that the


abstraction can receive.
36

Sample Class
Class
Course
Properties
Name
Location
Days offered
Credit hours
Start time
End time

37

Behavior
Add a student
Delete a student
Get course roster
Determine if it is full

a + b = 10

29/09/2012

Representing Classes in the UML

A class is represented using a rectangle with three


compartments:
The

The

The
38

Professor

class name

structure (attributes)

- name
- employeeID : UniqueId
- hireDate
- status
- discipline
- maxLoad

behavior (operations)

+ submitFinalGrade()
+ acceptCourseOffering()
+ setMaxLoad()
+ takeSabbatical()
+ teachClass()

29/09/2012

Classes of Objects

39

How many classes do you see?

29/09/2012

The difference between a class and a table

40

The Relationship Between Classes and Objects

A class is an abstract definition of an object


It

defines the structure and behavior of each object in


the class

It

serves as a template for creating objects

Objects are grouped into classes


Object
s
Clas
s
Professor

Professor Smith

41

Professor Jones

29/09/2012

What Is an Attribute?

An attribute is a named property of a class that describes


the range of values that instances of the property may
hold.

A class may have any number of attributes or no attributes


at all.

Attributes

42

Student
- name
- address
- studentID
- dateOfBirth

29/09/2012

What Is an Operation?

A service that can be requested from an object to effect


behavior. An operation has a signature, which may restrict
the actual parameters that are possible.

A class may have any number of operations or none at all.


Student

Operations

43

+ get tuition()
+ add schedule()
+ get schedule()
+ delete schedule()
+ has prerequisites()

29/09/2012

What Is Polymorphism?

The ability to hide many different implementations behind


a single interface.

Manufacturer A

Manufacturer B

OO Principle:
Encapsulation
44

Manufacturer C

Remote Control

29/09/2012

Polymorphism

Occurs with inheritance.

Different subclasses may have different implementations of


the identical operations

Allows you to treat an object as the base class, rather than


as a specific inherited type.

Programmer doesnt have to keep track of the specific


subclasses, the system selects the correct operation based
on the object type.

Accomplished at run time using dynamic binding.

45

lets you substitute objects that have identical interfaces for each other at
run-time.

29/09/2012

What is an Interface?

Interfaces formalize polymorphism

Interfaces support plug-and-play architectures


Tube
<<interface>>

Shape
Pyramid
Draw
Move
Scale
Rotate

Cube

Realization relationship
46

29/09/2012

(stay tuned for realization relationships)

Interface
How do you get an object to do useful work for you?

Objects are known only through their interfaces

Every object presents an interface to the world.

Interface determines what you can get an object to


do.

Represent a "contract" with other objects.

Communicate with objects by sending messages to


them; an objects interface is the set of messages
an object will respond to.

47

29/09/2012

What is a Package?

A package is a general purpose mechanism for


organizing elements into groups

A model element which can contain other model


elements
Package Name

Uses
Organize the
A

48

OO Principle:
Modularity

model under development

unit of configuration management


29/09/2012

What is a Subsystem?

A combination of a package (can contain other


model elements) and a class (has behavior)

Realizes one or more interfaces which define its


behavior
Realization

Subsystem

<<subsystem>>
Subsystem Name

Interface

Interface

OO Principles: Encapsulation and Modularity


49

29/09/2012

What is a Component?

A non-trivial, nearly independent, and replaceable


part of a system that fulfills a clear function in the
context of a well-defined architecture

A component may be
A

source code component

run time components or

An

executable component

Source File
Name

50

<<EXE>>
Executable
Name

29/09/2012

OO Principle:
Encapsulation

Component
Interface

<<DLL>>
Component
Name

Subsystems and Components

Components are the physical realization of an


abstraction in the design

Subsystems can be used to represent the


component in the design
Design Model

Implementation Model
Component
Name

<<subsystem>>

Component Name
Component
Interface

Component
Interface

OO Principles: Encapsulation and Modularity


51

29/09/2012

Relationships

Association

Aggregation

Dependency

Generalization

Realization
generalization

Window
open()
close()
move()
display()
handleEvent()

ConsoleWindow

52

Event

association

DialogBox

29/09/2012

Control

Relationships: Association

Models a semantic connection among classes


Association
Name

Professor

Works for

Association
Class

53

29/09/2012

University

Relationships: Association Aggregation


Company

Department

To model a whole/part relationship, use


aggregation.

Aggregation represents a has-a or is a partof relationship, meaning that an object of the


whole has objects of the part.
[Martin Fowler]

54

29/09/2012

Association: Multiplicity and Navigation

Multiplicity defines how many objects participate in a


relationships

Specified for each end of the association

Associations are bi-directional by default, but it is often


desirable to restrict navigation to one direction

If navigation is restricted, an arrowhead is added to indicate the


direction of the navigation
Multiplicity

Student

0..*

Navigation
55

29/09/2012

Schedule

Relationships: Dependency

A relationship between two model elements where a change


in one may cause a change in the other

E.g. Class A depends on class B: method of A manipulates objects of B


in the parameters, local variables, or return types

Class

Client

Packag
e
ClientPackage

56

Supplier

Dependency
relationship

Client

SupplierPackage

29/09/2012

Componen
t

Supplier

Dependency
relationship

Relationships: Realization
Tube
<<interface>>

Shape
Pyramid
Draw
Move
Scale
Rotate

Cube

Realization relationship

57

29/09/2012

(stay tuned for realization relationships)

Inheritance
Shape
origin
draw()
erase()
move()
setColor()
getColor()

Circle
draw()
erase()

Square
draw()
erase()
move()
[Martin Fowler]

58

29/09/2012

Polymorphism
Bird

FlockManager

move()

reLocate()

Goose
move()

Penguin
move()

[Martin Fowler]
59

29/09/2012

Example: Multiple Inheritance

A class can inherit from several other classes


FlyingThing

Animal
multiple
inheritance

Airplane

Helicopter

Bird

Wolf

Use multiple inheritance only when needed, and


always with caution !
60

29/09/2012

Horse

What Gets Inherited?

A subclass inherits its parents attributes,


operations, and relationships

A subclass may:
Add

additional attributes, operations, relationships

Redefine

inherited operations (use caution!)

Common attributes, operations, and/or


relationships are shown at the highest applicable
level in the hierarchy

Inheritance leverages the similarities among classes


61

29/09/2012

Example: What Gets Inherited


GroundVehicle

Superclass
(parent)

weight
licenseNumber

owner
0..*

Person

register( )

generalization

Subclass

Truck

Car
size

tonnage
getTax( )

62

29/09/2012

Trailer

The Strengths of Object Technology

Provides a single paradigm

A single language used by users, analysts, designers, and implementers

Facilitates architectural and code reuse

Models more closely reflect the real world

More accurately describes corporate entities

Decomposed based on natural partitioning

Easier to understand and maintain

Provides stability

63

A small change in requirements does not mean massive changes in the


system under development

Is adaptive to change
29/09/2012

You might also like