2 . Object Model

56
UML-1 Venkat Subramaniam Venkat Subramaniam 2. Object Model

description

2 . Object Model. Benefits Of OO Development. Models System using Objects Small Semantic gap between reality & model Understanding the system is easier Modifications are localized. Object-Oriented Paradigm. Collection of Discrete Objects Data & Behavior OO Paradigm Abstraction - PowerPoint PPT Presentation

Transcript of 2 . Object Model

Page 1: 2 . Object Model

UML-1Venkat SubramaniamVenkat Subramaniam

2. Object Model

Page 2: 2 . Object Model

UML-2Venkat SubramaniamVenkat Subramaniam

Benefits Of OO Development• Models System using Objects

• Small Semantic gap between reality & model

• Understanding the system is easier

• Modifications are localized

Page 3: 2 . Object Model

UML-3Venkat SubramaniamVenkat Subramaniam

Object-Oriented ParadigmCollection of Discrete Objects

Data & BehaviorOO Paradigm

• Abstraction• Encapsulation• Hierarchy

– Inheritance hierarchy (“is-a”)– Part of hierarchy (“has a”)

• Polymorphism

Page 4: 2 . Object Model

UML-4Venkat SubramaniamVenkat Subramaniam

Abstraction“A simplified description ... of a system that emphasizes some of the

system’s details ... while suppressing others”

“An abstraction denotes the essential characteristics of an object that distinguish it from all other kind of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer”

Page 5: 2 . Object Model

UML-5Venkat SubramaniamVenkat Subramaniam

Encapsulation• Information hiding• Interface - Implementation• Behavior & Data“Encapsulation is the process of

compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation”

Page 6: 2 . Object Model

UML-6Venkat SubramaniamVenkat Subramaniam

What is an Object?• “Concept, abstraction, or thing

with crisp boundary & meaning for a problem”• An Object has state and behavior• Objects receive stimuli/messages & respond• Receiving a stimulus, Object may change stateExamples:

kimjullie dylan charlie charlie.fly() stimulus

Page 7: 2 . Object Model

UML-7Venkat SubramaniamVenkat Subramaniam

What is a Class?• Group of Objects with similar

–properties (attributes)–behavior–relationships to other objects–semantics

• Blueprints of Objects

dylankimjullie charlieGirl Boy

ParrotExample

Page 8: 2 . Object Model

UML-8Venkat SubramaniamVenkat Subramaniam

Hierarchy

“Hierarchy is a ranking of abstractions”

Inheritance : expresses “is-a” or “Kind-of” relationship

• Extensibility & ReusabilityPart-of:

expresses that object is an aggregate of another

Page 9: 2 . Object Model

UML-9Venkat SubramaniamVenkat Subramaniam

PolymorphismHiding alternative procedures

behind a Common Interface

Send a Message to an object - Polymorphism guarantees that the

correct/proper implementation is invoked.

Sender does not know specific class of receiver

Page 10: 2 . Object Model

UML-10Venkat SubramaniamVenkat Subramaniam

Object Model

• Captures static structure of system• Objects, relationships, attributes &

operations• Most important• Intuitive graphic representation• Valuable for communication & documentation

Page 11: 2 . Object Model

UML-11Venkat SubramaniamVenkat Subramaniam

Objects

• Decomposing problem into objects – depends on judgement & nature of problem

• No one correct representation

• Objects have identity

Page 12: 2 . Object Model

UML-12Venkat SubramaniamVenkat Subramaniam

Class• Often appear as nouns in problem descriptions• Has semantic• Interpretation of semantics

– depends on application and matter of judgement

• Each class may have zero, one or more objects• Each object knows it class

Page 13: 2 . Object Model

UML-13Venkat SubramaniamVenkat Subramaniam

Class Diagrams

• Provide formal graphic notation for modeling

• Concise, easy to understand, practical

• Describes many possible instances

Page 14: 2 . Object Model

UML-14Venkat SubramaniamVenkat Subramaniam

Object Diagrams

• Describes how set of objects relate• Useful for

– documenting test cases– Clarification of complex class diagrams

• Class Diagram corresponds to infinite set of object diagrams

Page 15: 2 . Object Model

UML-15Venkat SubramaniamVenkat Subramaniam

Notation for Classes & Objects (UML)

Boy

Girl

Classes

kelly

brenda : Girl

dylan

Objects

Page 16: 2 . Object Model

UML-16Venkat SubramaniamVenkat Subramaniam

Attributes

• Data value held by objects of a class• Objects may have same/different values for

attribute• Attribute name unique within a class• Adjectives often represent specific

enumerated attribute values : “red car”

Page 17: 2 . Object Model

UML-17Venkat SubramaniamVenkat Subramaniam

Attributes...

• Attribute is a pure data value - not an object

• Internal identifiers must not be shown as attributes

• Show only important attributes

Page 18: 2 . Object Model

UML-18Venkat SubramaniamVenkat Subramaniam

Derived AttributesBase Attribute :

– primitive, not dependent on other attributes

Derived Attribute : – computing not considered to change state of

an object– dependent on base attributes– may be stored or computed upon a query

operationExample : Area of a circle, age of a person

Page 19: 2 . Object Model

UML-19Venkat SubramaniamVenkat Subramaniam

Attributes Notation

-attrPrivate#attrProtected+attrPublic$attrStatic/attrDerived-attrWType: int-attrWInitVal: double=0.0

className

-name+dob/age: int

Girl

Underline in revisednotation

Page 20: 2 . Object Model

UML-20Venkat SubramaniamVenkat Subramaniam

Operations & Methods• Operation : Function that may be applied to or by

objects• Same Operations applying

to different classes: Polymorphic• Method is implementation of an operation for a class• Operation has a target object and may have

arguments• Same operations on different classes should have

– same signature and consistent intent• Query Operation : Does not affect the state of object• Show only important methods

Page 21: 2 . Object Model

UML-21Venkat SubramaniamVenkat Subramaniam

Operations Notation

+publicMethod()#protectedMethod()-privateMethod()$staticMethod()+methodWParam(int)+methodWRetVal(): int

className

+sing()+play()

Girl

Page 22: 2 . Object Model

UML-22Venkat SubramaniamVenkat Subramaniam

Example with Attributes & Operations

-seconds

+start()+stop()+reset()+getSeconds(): double

StopWatch

Page 23: 2 . Object Model

UML-23Venkat SubramaniamVenkat Subramaniam

Associations and Links• Link is physical or conceptual

connection between objects• Link is an instance of an Association

Example:Link : Susan is-wife-of Robert

Julie is-wife-of JohnAssociation: Woman is-wife-of Man

Associations and Links appear as Verbs

Page 24: 2 . Object Model

UML-24Venkat SubramaniamVenkat Subramaniam

Associations & Links...• Associations are bi-directional

–may be implemented as unidirectional

• Implemented usually as pointers – important not to think as pointers

• Associations may be–one-to-one–one-to-many–many-to-one–many-to-many

Page 25: 2 . Object Model

UML-25Venkat SubramaniamVenkat Subramaniam

Ternary & High Order Associations

Binary Association : Relates two classesWoman is-wife-of Man

Ternary Association : Relates three classesNancy is-daughter-of Susan and Robert

n-ary Association : Relates n classesHigher Order Associations

– complicated to draw, implement and think

– try to avoid if possible

Page 26: 2 . Object Model

UML-26Venkat SubramaniamVenkat Subramaniam

Associations Notations

Exactly One

Optional

One Or MoreZero Or More

*0..11..*0..*

Many Directed Association*

Company PersonemployeesworksForemploys

1..*0..*

Page 27: 2 . Object Model

UML-27Venkat SubramaniamVenkat Subramaniam

Link Attributes & Association Classes

• Attributes that belong to association of object rather than one object

• Link Attributes belong in Association Classes• Ex: Salary received by Employee from Company• In an one-to-one association you may try to

make it attribute of one of the objects– Leads to extensibility problems

Page 28: 2 . Object Model

UML-28Venkat SubramaniamVenkat Subramaniam

Association Class Notation

Person Company* *

Employmentsalary:double

Page 29: 2 . Object Model

UML-29Venkat SubramaniamVenkat Subramaniam

Role Names• Name given to either end of an association

• Helps to navigate from one object to related objects

• Helps clarify when two classes have several associations between them

Woman Manhusbandwife

0..1 married 0..1

Woman Manhusbandwife

married* 1daughter children father

0..1 0..1

Page 30: 2 . Object Model

UML-30Venkat SubramaniamVenkat Subramaniam

Qualifiers• Distinguishes among set of associated

objects• Models associative arrays, dictionaries• Qualifiers may be wrongly modeled as

attribute of associated class

GradeList code Student

Qualified

* 1

GradeListStudent

Unqualifiedcode

* *

Page 31: 2 . Object Model

UML-31Venkat SubramaniamVenkat Subramaniam

Aggregation & Composition

Aggregation:• Part-of or part-whole relationship (by

reference)• Example : Car has Engine and Transmission• Assembly of objects with aggregate and

component parts• Component existence may or may not depend

on aggregate

Page 32: 2 . Object Model

UML-32Venkat SubramaniamVenkat Subramaniam

Aggregation & Composition...

Composition:• Part belongs to only one whole (by Value)• Part lives and dies with the whole• Whole cannot replace the part

Page 33: 2 . Object Model

UML-33Venkat SubramaniamVenkat Subramaniam

Aggregation/Composition Notation

Aggregation

Has By Value

Has By Reference

+drive()

VehicleEngine

VIN

Page 34: 2 . Object Model

UML-34Venkat SubramaniamVenkat Subramaniam

Aggregation /Composition Example

Person brain

Car

Aggregation

Composition

Page 35: 2 . Object Model

UML-35Venkat SubramaniamVenkat Subramaniam

Inheritance• Models is-a relationship• Relationship between a class and its

refined versions• Superclass or Base class• Subclass or Derived class• Inheritance is transitive• Discriminator : The property being

abstracted by a particular inheritance• Breath Vs. Depth of inheritance

Page 36: 2 . Object Model

UML-36Venkat SubramaniamVenkat Subramaniam

+drive()

Vehicle

-sunRoof

Car Truck

Inheritance NotationGeneralization

Subclass1 Subclass2

Superclass

Page 37: 2 . Object Model

UML-37Venkat SubramaniamVenkat Subramaniam

Inheritance Example

Child Animal

Girl Boy Dog Cat

Page 38: 2 . Object Model

UML-38Venkat SubramaniamVenkat Subramaniam

Grouping Mechanism : Package

• Grouping classes together into higher-level units

• Package diagram with dependency

• Dependency between packages exists if– class in one package depends on a class in the

other– definition change of one package may change

other

Page 39: 2 . Object Model

UML-39Venkat SubramaniamVenkat Subramaniam

Package Notation

componentName VehicleComponent

EngineComponentAccessories

Page 40: 2 . Object Model

UML-40Venkat SubramaniamVenkat Subramaniam

Aggregation Vs. Association

• Special form of Association• Confusing• Aggregation represents “part-of” relationship• Some operations on whole automatically

applied to its parts• Aggregate is asymmetric : part is

subordinate to the whole• Association is symmetric : objects involved

are of equal stature

Page 41: 2 . Object Model

UML-41Venkat SubramaniamVenkat Subramaniam

Aggregation Vs. Inheritance

• Aggregation represents part-of relationship• Inheritance represents kind-of relationship• Aggregation refers to object relationships• Inheritance refers to class relationships

Page 42: 2 . Object Model

UML-42Venkat SubramaniamVenkat Subramaniam

Fixed, Variable & Recursive Aggregates

Fixed : – Fixed structure– Number & types of parts pre-defined

Variable : – Finite number of levels - Number of parts vary

Recursive : – Contains instances of the same kind of

aggregate– number of potential levels unlimited

Page 43: 2 . Object Model

UML-43Venkat SubramaniamVenkat Subramaniam

Example : Fixed, Variable, Recursive

Aggregation

Layout ElectronicComponent

NAND AND FlipFlop

*

2

*

Page 44: 2 . Object Model

UML-44Venkat SubramaniamVenkat Subramaniam

Operations & Aggregation

• Operation or Triggering :– automatic application of an operation to

network of objects when applied to some starting object

• ShallowCopy

• DeepCopy

Page 45: 2 . Object Model

UML-45Venkat SubramaniamVenkat Subramaniam

Abstract Classes

Representing an Abstraction that is Abstract.

• Abstract classes represent – concepts – not real objects

• ABCs used only to create other “Concrete” classes

Page 46: 2 . Object Model

UML-46Venkat SubramaniamVenkat Subramaniam

Abstract Classes ...

Example: Shape, Employee, Animal

Whether a class in Abstract or not depends on – judgement– application on hand

Page 47: 2 . Object Model

UML-47Venkat SubramaniamVenkat Subramaniam

Inheritance : Extension & Restriction

Extension :– Subclass adds new features– Subclass inherits all properties & operations

of ancestorRestriction :

– Subclass constrains ancestor attributes– Subclass may not inherit all properties &

operations of its ancestor– Often leads to trouble (Liskov Substitutability

Principle)

Page 48: 2 . Object Model

UML-48Venkat SubramaniamVenkat Subramaniam

Inheritance : Extension & Restriction ...

• Proper Extension:– A Subclass may override the internal

implementation of an operation– No problem as long as external protocol

remains the same

Page 49: 2 . Object Model

UML-49Venkat SubramaniamVenkat Subramaniam

Metadata & Metaclass

Metadata : Data that is description of other data

Metaclass : Class descriptor objects that describe other classes

Page 50: 2 . Object Model

UML-50Venkat SubramaniamVenkat Subramaniam

ConstraintsRestricts values that entities can assumeEntity : objects, classes, attributes,

associationsBetter to capture constraints in structure

rather than using constraints constructs

ElectronicsClass Students*

{E E Discipline}

Page 51: 2 . Object Model

UML-51Venkat SubramaniamVenkat Subramaniam

Multiple Inheritance

Allows a class to have more than one Superclass• Join class

TerrestrialBeing AquaticBeing

Amphibian

Page 52: 2 . Object Model

UML-52Venkat SubramaniamVenkat Subramaniam

Multiple Inheritance Poses Problems

• Ambiguous function collisions• Class proliferation

Person

Male Female ComputerScientist CivilEngineer

gender discipline

FemaleComputerScientist

Page 53: 2 . Object Model

UML-53Venkat SubramaniamVenkat Subramaniam

Workarounds for MI

• Delegation using aggregation of roles

• Single inherit important class & delegate

rest

• Nested Generalization

Page 54: 2 . Object Model

UML-54Venkat SubramaniamVenkat Subramaniam

Delegation using Aggregation of Roles

• Inheritance of operations across aggregation not automatic

• Join class must handle that operations and delegate to appropriate component

Person

Male Female ComputerScientist CivilEngineer

gender discipline

PersonsGender PersonsDiscipline

*

Page 55: 2 . Object Model

UML-55Venkat SubramaniamVenkat Subramaniam

Single Inherit Important Class &

Delegate Rest

• Preserves identity and inheritance across at least one generalization

Person

Male Female ComputerScientist CivilEngineer

gender

discipline

PersonsDiscipline

*

Page 56: 2 . Object Model

UML-56Venkat SubramaniamVenkat Subramaniam

Nested Generalization

• Violates the OO spirit• Not practical for large number of combinations

Person

Male Female

gender

MaleComputerScientist MaleCivilEngineer FemaleComputerScientist FemaleCivilEngineer