Sunteți pe pagina 1din 23

Basic Principles of Object Orientation

Object Orientation

Encapsulation
Abstraction

Modularity

Hierarchy
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 1
What is Encapsulation?
 Hide implementation from clients
 Clients depend on interface

Improves Resiliency
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 2
What is Modularity?
 The breaking up of something complex into
manageable pieces
Order
Entry

Order Processing
System Order
Fulfillment

Billing

Manages Complexity
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 3
What is Hierarchy?
 Levels of abstraction Asset
Increasing
abstraction

BankAccount Security RealEstate

Savings Checking Stock Bond

Decreasing Elements at the same level of the hierarchy


abstraction should be at the same level of abstraction
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 4
What is an Object?
 Informally, an object represents an entity,
either physical, conceptual, or software
 Physical entity

Truck

 Conceptual entity
Chemical Process

 Software entity Linked List

OOAD Using the UML - Introduction to Object Orientation, v 4.2


Copyright  1998-1999 Rational Software, all rights reserved 5
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

OOAD Using the UML - Introduction to Object Orientation, v 4.2


Copyright  1998-1999 Rational Software, all rights reserved 6
Representing Objects
 An object is represented as rectangles with
underlined names

: Professor
a + b = 10

ProfessorClark
Class Name Only
Professor Clark
ProfessorClark :
Professor Object Name Only

Class and Object Name (stay tuned for classes)


OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 7
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 relevant characteristics
 Suppresses other characteristics

OO Principle: Abstraction
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 8
Sample Class

Class
Course
Properties Behavior
Name Add a student
Location Delete a student
a + b = 10
Days offered Get course roster
Credit hours Determine if it is full
Start time
End time

OOAD Using the UML - Introduction to Object Orientation, v 4.2


Copyright  1998-1999 Rational Software, all rights reserved 9
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
Objects Class

Professor

Professor Smith Professor Mellon

Professor Jones
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 10
What is Polymorphism?
 The ability to hide many different
implementations behind a single interface

Manufacturer B
Manufacturer A Manufacturer C

OO Principle:
Encapsulation

OOAD Using the UML - Introduction to Object Orientation, v 4.2


Copyright  1998-1999 Rational Software, all rights reserved 11
What is an Interface?
 Interfaces formalize polymorphism
 Interfaces support “plug-and-play”
architectures
Tube
<<interface>>
Shape
Pyramid
Draw
Move
Scale
Rotate Cube

Realization relationship (stay tuned for realization


OOAD Using the UML - Introduction to Object Orientation, v 4.2
relationships)
Copyright  1998-1999 Rational Software, all rights reserved 12
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 OO Principle:
 A run time components or Encapsulation
 An executable component

Source File <<EXE>> <<DLL>>


Name Executable Component
Name Component Name
Interface
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 13
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
OO Principle:
Package Name Modularity

 Uses
 Organize the model under development
OOAD Using the UML - Introduction to Object Orientation, v 4.2

 A unit of configuration management


Copyright  1998-1999 Rational Software, all rights reserved 14
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>>
Interface Subsystem Name
Interface

OO Principles: Encapsulation and Modularity


(stay tuned for realization relationship)
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 15
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

<<subsystem>> Component
Component Name Name
Component Component
Interface Interface

OO Principles: Encapsulation and Modularity


OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 16
Example: Single Inheritance
 One class inherits from another
Ancestor

Account
balance
name
Superclass number
(parent) Withdraw()
CreateStatement()
Generalization
Relationship

Checking Savings

Subclasses Withdraw() GetInterest()


Withdraw()

Descendents
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 17
Example: Multiple Inheritance
 A class can inherit from several other
classes
FlyingThing Animal

multiple
inheritance

Airplane Helicopter Bird Wolf Horse

Use multiple inheritance only when needed, and


always with caution !
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 18
What Gets Inherited?
 A subclass inherits its parent’s 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
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 19
Strengths of Object Orientation
 A single paradigm
 Facilitates architectural and code reuse
 Models more closely reflect the real world
 More accurately describe corporate data and
processes
 Decomposed based on natural partitioning
 Easier to understand and maintain
 Stability
 A small change in requirements does not mean
massive changes in the system under
development
OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 20
A Simple Sales Order Example

Order

Product

Ship via

OOAD Using the UML - Introduction to Object Orientation, v 4.2


Copyright  1998-1999 Rational Software, all rights reserved 21
Class Diagram for the Sales Example

Sale

seller buyer item sold shipping mechanism


Salesperson Customer Product Vehicle

Corporate Individual Truck Train

OOAD Using the UML - Introduction to Object Orientation, v 4.2


Copyright  1998-1999 Rational Software, all rights reserved 22
Effect of Requirements Change
Suppose you need a
new type of shipping
vehicle ... Sale

seller buyer item sold shipping mechanism


Salesperson Customer Product Vehicle

Corporate Individual Truck Train Airplane

Change involves adding a new subclass


OOAD Using the UML - Introduction to Object Orientation, v 4.2
Copyright  1998-1999 Rational Software, all rights reserved 23

S-ar putea să vă placă și