Vous êtes sur la page 1sur 61

Object Oriented Programming

Object Web
Bapayya Choudhary Maganti
2-2-647/182/J, C. E. Colony, New Nallakunta, Hyderabad
- 500 013.
Phone: +91-(0)40-27406781, E-mail:
Course Contents
Object Oriented Background
Object Oriented Programming Concepts
Analysis
 Use Case Modeling
Design
 Object Modeling
 CRC
 Class Diagrams
 Dynamic Modeling
 Sequence Diagram
 Collaboration Diagram
 State Diagrams
 Activity Diagrams
 Component Diagram
 Deployment Diagram
Case Study – Drawing Editor Problem
Background

Object Web
Bapayya Choudhary Maganti
2-2-647/182/J, C. E. Colony, New Nallakunta, Hyderabad
- 500 013.
Phone: +91-(0)40-27406781, E-mail:
What Is Object Orientation?
Term ‘Object-Orientation’ means.
 The organization of software as a collection of discrete
objects that incorporate both data structure and behavior.
A new technology based on objects and classes.
Use of objects and classes in analysis.
Everything here is object and process is sending a
message to objects.
A language is object oriented if supports the following
four proprieties:
 Abstraction.
 Polymorphism.
 Encapsulation.
 Inheritance.
Object-based Vs Object-oriented
Object-based programming usually refers to
objects without inheritance and hence without
polymorphism.
These languages support abstract data types
and not classes, which provide inheritance and
polymorphism.
Object-oriented programming refers to the
objects with inheritance and polymorphism.
What Is the Background?
Arrived from Process.
Talk about the Behavior Centric not Data Centric.
Simula 67 the first Object Oriented Language, 1967.
 Objects, Classes, Inheritance, Dynamic Typing.
Smalltalk first Pure Object Oriented Language, 1969.
 Classes, Objects, Inheritance.
 Dynamic Typing.
 First Graphics Environment.
 First Use of Mouse in Programming Environment.
 Introduced first Multi Threaded Environment.
 Form Parc of Xerox Corporation.
 For more info look at parc.xerox.com.
OO Programming Languages
Statically-typed:
 Memory allocation at compile time
 No Runtime Class, Objects
 Type Defined.
 Example: C++
Dynamically-typed:
 Memory Allocation at Run-time
 Runtime Classes, Object
 Runtime Inheritance
 No Data types
 Example: Smalltalk
Both:
 Example: Java
Statically-Typed:
C++

Classic-Ada

Dragoon

Emerald/Jade

Object Pascal

Trellis/Owl
Dynamically-Typed:
Actors Language

C+@

Flavors

Python

Self

Smalltalk
Both:
Actor Eiffel

Ada95 Java

BETA Modula-3

C++ (With RTTI) Objective-C

Cecil Sather

CLOS B
Object-Oriented Operating Systems
Object-Oriented Operating Systems provide
resources through objects, sometimes all the
way down to to the machine.
These are almost always distributed systems
(DOS or DPOS), allowing objects to be passed
freely between machines.
Object-Oriented Operating Systems
Some of the Object-Oriented Operating
Systems:
Apertos.
Chorus Micro-kernel.
Choices.
GEOS.
Mach.
NachOS.
Ouverture Project.
Peace.
Spring.
Object-Oriented Databases
Object-oriented databases are databases that
support objects and classes.
They are different from the more traditional
relational databases because they allow
structured sub objects, each object has its own
identity, or object-id and because of support for
methods and inheritance.
Object-oriented Databases
It is also possible to provide relational
operations on an object-oriented database.
OODBs allow all the benefits of object-
orientation, as well as the ability to have a
strong equivalence with object-oriented
programs, an equivalence that would be lost if
an alternative were chosen, as with a purely
relational database.
List of Object-oriented Databases:
 Research Systems  Commercial Systems
 AVANCE  ArtBASE, EasyDB
 CLOSQL  GemStone
 ConceptBase*  ITASCA, Matisse
 COOL/COCOON  NeoAccess
 Encore*  OBST+
 Exodus*  O2, Objectivity
 Machiavelli  ObjectStore
 MOOD4-PC*  Ontos
 OBST/STONE*  Odapter/OpenODB program (HP)
 Ode*  Unisys Universal Repository
 Oggetto  Versant
 Orion  VisualWorks
 OOFILE
 OTGen
 Phyla, POET
 PLOB
 Statice
 VODAK  UniSQL
Object-oriented Methodologies
List of Methodologies:
 Bernard  IBM
 BON  Jacobson
 Booch  Martin/Odell
 Coad/Yourdon  Ooram
 Colbert  Room
 de Champeaux  Rumbaugh
 Embley  Shlaer and Mellor
 EVB  Open
 Rebecca Wirf’s  UML
 FUSION  Wasserman
 HOOD  Wirfs-Brock
UML
Collection of concepts from various
methodologies.
From:
BOOCH.
RUMBAUGH.
JACOBSON.
Started in 1995.
Adapted by OMG.
Current Version 1.3.
Can be adapted for any application.
UML, Stepping ahead
Booch OMT OOSE Others

Unified Method 0.8 UML 0.9, 0.91


Start 1/95, OOPSLA 10/95 6/96, 9/96

UML 1.1 UML 1.0


OMG Revision 9/97
OMG Submission 1/97
Adoption 11/97

UML Partners Expertise


Others
UML 1.3
RTF report 4/99 UML 1.?
Expected Shortly

OMG Feedback OMG/RTF Revisions


Object Oriented Programming
Concepts - Fundamentals

Object Web
Bapayya Choudhary Maganti
2-2-647/185, C. E. Colony, New Nallakunta, Hyderabad - 500 013.
Phone: +9140-7422414, E-mail: info@objweb.com, Web: www.objweb.com
What is an Object?
The entity which represents something
Objects is having a concrete physical form as
well as intelligence, because we not only
perceive them but also make them do
something.
Physical Object
An object may be invisible to the human eye
but having some basic form of intelligence.
Conceptual Object
SBI/K
Object have State, Behavior, Identity and
Knowledge about Other Objects
State is attributes
Behavior is methods
Identity is uniqness
Knowledge is message sent between to objects.

State x=50, y=50


Behaviour isFirstQuadrantPoint ( )
Knowledge
Identity Point Object at memory AF00
Identity
Each Object is different form other.
All Objects Have Identity.
Identity is that property of an object that
distinguishes it from all other objects. (Booch).
An object is a reality and it is an entity which
exists in time and space. We are able to
identify it as an object because of its
IDENTITY. This is true for all objects in the real
world.
We see a lion as a lion and not a horse
because of its identity.
, Identity
If we look at any two similar object, the
possible outer look may be same but there may
be lot of difference between them.
My be two pens are appearing same, but the
are different.
Similarly, two identical twins are nothing but
two distinct objects.
Objects Know Each Other
Objects can't behave independently
Like humans objects need to know about other
objects in the system.
All objects are intelligent
We give them levels of intelligence we would
normally associate with humans.
Objects are not in isolation
We share the world around us with other objects.
Objects Communicate With Each Other.
Objects live in a dynamic world where they
exchange information and communicate with a
number of objects getting certain things done.
When a object receives the messages and if
the object cannot understand the information
then it searches for the object which can
understand this and send this message to the
other object.
There are two types of communications
between objects.
The direct communication.
The indirect communication.
Behavior
By communication an operation was achieved.
The operation placed a certain
RESPONSIBILITY.
A property is gives uniqueness to Object.
The term property denotes attributes.
Both property and attribute may be used
interchangeably.
Properties are usually static, which means that
they do not change over a period of time.
Behavior
Attributes have values.
An attribute is a data value held by the objects
in a grouping of similar objects.
It is important to note here that ATTRIBUTE
SHOULD BE A PURE DATA VALUE NOT AN
OBJECT. (Not valid for pure object oriented
programming languages.)
Unlike objects, data values have no identity.
State
Preceding each of these state - change is a stimulus
(Booch) or event (Rumbaugh) or action (Booch).
Therefore, the current state of any object depends on:
 Preceding State.
 Object's inherent properties (or attributes).
 Stimulus received by the object. Which may cause the
properties to undergo changes
Current Status =
Inherent Properties +
Preceding State +
Stimulus received +
Argument Passed with Stimulus.
, State
The State of an object may be defined as:
 The state of an object at any given instance is determined
by the combination of its precedent status, its internal
properties, the current values of these properties and the
stimulus it receives.
 The fact that every object has a state implies that every
object takes up some amount of space (either in the
physical world or in computer memory).
Now we can define Behavior of an object as:
 Behavior is how an object acts or reacts in terms of its state
changes and message passing.
Colloquially the word object is also used to indicate a
group of similar objects.
Instance Object -> Instance.
Class Object -> Class.
Important Aspects
An object with the same name may occur in
completely different domains.
Ex: Customer.
An object is characterized by what it is used for
and by the information that is attached to it.
i.e., attributes.
Use of an object is captured by its behavior.
i.e., by the operation that it can offer to other object
in its environment.
Client Server Nature of Objects
An object uses another object for performing
an operation by sending a stimulus.
Each stimulus has an unique name that clearly
determines the operation to be performed.
The requirements placed on the server object may
have several responsibilities depending upon its
use.

Client Object Server Object


, Client Server Nature of Objects
 When a client objects sends a stimuli to a server object, the
only information that is available to the client object is the
type of operation that can be performed by the server
object.
 The client knows only the name of the operation to be
performed by the server which is sent as the stimuli along
with various types of information each operation needs in
order to perform the task.
 The names and types of information are called the
operations 'signature'.
 Remember an object can perform different operations
depending upon usage, and, therefore will have different
'signatures'.
 The combination of all such signatures of an object is called
the objects protocol.
Object Description
An object can be described in two different, but
related ways.
The external description, which shows how
others can use the object and the responsibilities
the objects has towards others. This description
contains only the objects protocol (how it can be
used). This is also sometimes called the objects
specification.
The internal description specifies the objects
various attributes and how each operation is
realized. This description is called the objects
implementation.
According to Booch
Without this conception framework, you may
be programming in a language such as
Smalltalk, object Pascal, C++, CLOS, Eiffel,
Ada, but your design is going to smell like a
FORTRAN, Pascal or C application.
You will have missed out on or otherwise
abused the expressive power of the object-
oriented language you are using for
implementation.
More importantly, you are not likely to have
mastered the complexity of the problem at
hand.
Related Concepts:
Important concepts & Related Topics:
Abstraction.
Encapsulation.
Generalization.
Concurrence.
Persistence.
Polymorphism.
Composition.
Factorization.
Delegation.
Refinement.
Multi-layer Architecture.
Abstraction

View of the Object


Object Web, Bapayya Choudhary Maganti
Abstraction
One of the two ways humans cope with
complex problems to spot
Similarities between objects, processes, situations.
Break down large problems into manageable level.
A viewer abstracts objects in relation to the problem
perspective.
Let us consider an example of how 'human beings' may
be viewed in different domains.
Domain Viewer Abstraction
Biology Biologist Species
Politics Politician Vote Bank
Industries Manager Resource
Food habits Man-eater Food
, Abstraction
Abstraction is a phenomenon
By which a viewer is able to identify unique
features or characteristics of an object which
separate the object from all other objects in a given
problem domain.
There are four metrics to ensure the design of
class hierarchy and relations are:
Coupling
Cohesion
Sufficiency
Completeness
Coupling
Coupling is a measure of the strength of
association between two classes.
A strong coupling tends to complicate the
system in the sense that it will be difficult to
modify or change it easily.
This complexity can be reduced by designing
systems with weakest possible coupling
between classes.
Private
Public
Cohesion
Cohesion measures the extent of connectivity
among the elements of a single object or class.
The most desirable form of cohesion is the
FUNCTIONAL COHESION.
i.e. We check if elements comprising an abstraction
are truly representative of the behaviors.
You can't abstract an animal and a vehicle if
they don't serve the same semantic purpose.
Both animal and vehicle may be abstracted in the
class “LoadCarrier” but cannot be abstracted
together if we are looking at a race horse and a
passenger car.
Sufficiency
This means that the class captures in its
abstraction all the expected behavior of the
class for meaningful interaction.
For example
if we are designing a class 'UpdateItem', it makes
sense to include an operation that will add an item
or remove an item or modify an item.
Though this may sound very routine, it has been
noticed that many times we neglect or miss out to
include all the operations.
Completeness
This has two forms
Extension of sufficiency
A complete class or module is one whose interface is
general enough to be commonly usable to any client.
In other words the interface should be such that it covers
all aspects of the abstraction.
Extension of generalization
The class should have implemented all the expected
abstract or virtual functions of the super class.
The class hierarchy is considered to complete when all
the sub classes are defined and the sub-classes are
defined as per the minimum expectations of the super
class.
The Process of Abstraction
Booch says
 Classes and objects should be at the right level of
abstraction;
 Neither too high nor too low.
The process of abstraction, is incremental and
iterative.
 We study the problem domain
 Identify objects which may seem disparate in the beginning
 Identify their relationship and group them in different classes
based on their common characteristics.
The identification of abstractions involves
 Knowledge of the problem domain
 Discovery
 Invention.
Encapsulation

Hide for right Abstraction & Coupling


Object Web, Bapayya Choudhary Maganti
Encapsulation
Encapsulation actually means to capsulate.
A capsule means something held in a covering.
What is inside can not be seen.
Enclose something or Encapsulate something
inside the object.

Public
Private
Protected
... Encapsulation
Abstraction takes care of what an object can
do i.e., the various operations it can perform
Encapsulation takes care of how it does it i.e.,
the attributes and the implementation details.
All objects have two parts
The abstracted visible part - INTERFACE
The encapsulated hidden - IMPLEMENTATION
What Is Achieved by Encapsulation?
Encapsulation is the process of hiding the
representation of an abstraction that make up
the structure of a class of objects and their
behavior.
If the implementation is modified we continue
to use the object without any modifications to
the interface.
According to Booch.
Intelligent encapsulation localizes design decisions
that are likely to change.
Refactoring
In Refactoring is the process of changing the
object without changing the Interface.
Interface is public method defined in Class
Note:
If an Object need implementation to be modified
without changing the interface it is terms ad
Refactoring.
This may not be possible always.
Refactoring minimizes the changes on other object.
This process ensures that the classes have right
level of coupling and will have minimum/no impact
on the collaborating classes.
Achieving Encapsulation
Smalltalk.
 By default all attributes are private and methods are public.
 It is only documentation process to achieve private
methods.
Java.
 Variables and methods can be declared as private public
and protected.
 Can have package level access modifiers and can mark
classes as private to package.
 Can define inner classes, which have class scope.
C++.
 Variables and methods can be declared as private, public
and protected.
 Can have private, protected, public inheritance.
Why Public, Protected and Private
Attributes of class
Make sure that all attributes are declared as private
and give protected or public access modifiers (get
and set methods)
Methods of class
Make sure that all methods are declared as
protected (never private) and only those required
as public.
Different levels of Encapsulation
Collaboration level
Inheritance level
Package level
Level of Encapsulation
Collaboration level
Between two classes there exists public and private
methods where the collaborating class can access
only the public behavior of other class but not the
private behavior.
Inheritance level
Sub class is extension to the existing behavior of
the super class and it should be able to access all
the behavior of the super class.
Package level
Classes can be defined as public or private to
package and this should minimize the coupling
between the packages.
Inheritance

Specialization & Generalization


Object Web, Bapayya Choudhary Maganti
Generalization and Inheritance.
Generalization and Specialization are
important aspects of Object Orientation which
give rise to the concepts of HIERARCHY and
INHERITANCE.
Each individual object in a class is called an
INSTANCE of its class.
FourWheeler
The terms 'instance' and 'object' are
interchangeable (for C++)

Car Van
... Generalization
An instance of a subclass is simultaneously an
instance of all its ancestor classes.
Each subclass not only inherits all the features
of its superior class, but adds its own specific
attributes and operations as well. It may
augment or restrict the existing structure and
behavior of a super-class.
Classes are related to one another via
inheritance relationships.
... Generalization
The most specialized class in a hierarchy is
known as the leaf class.
The most generalized class is known as the
base class.
The leaf classes are also known as concrete
classes.
Only the leaf class (or concrete class) will have
instances.
The base class and other intermediary classes
usually do not have any instances.
... Generalization
Abstract classes are not expected to have
instances of their own.
Abstract classes are written with the
expectation that its subclasses will add to its
structure the behavior which implements
methods.
Single inheritance means each subclass has
exactly one super-class.
Multiple inheritance means each such class
has more than one super-class.
... Generalization
The terms Generalization, Inheritance and
Specialization refer to the same underlying
idea, and are many a time used
interchangeably.
Generalization is used while modeling and
implementation. It facilitates modeling by
structuring classes and capturing what is
similar and what is different amongst classes.
Identify the behavior of different classes
Magnitude

Point Number Character

Integer Float

ShortInteger LongInteger
Solution
Magnitude Abstract Methods: +, -, <, ==
Concrete Method: >, <=, >=, !=

Abstract: *, / Concrete Methods: +, -, <, ==


Point Number Character

Concrete: +, -, *, /, <, ==

Integer Float

Concrete: +, -, *, /, <, ==

ShortInteger LongInteger
factorial ( ), B
Problem
Build the hierarchy/classes for the following.
Identify all classes in a coordinate system.
Identify the major responsibilities of the classes.
For a drawing editor problem define the elements.
 Rectangle.
Has start point, stop point, the diagnoll opposite corners
 Line.
Has start point, stop point.
 Circle.
Has center and radius.
 Ellipse.
Has center, major radius, minor radius.
Solution
Shape Shape Point
Line
Rectangle
Ellipse Line Rectangle Ellipse
Point

Vous aimerez peut-être aussi