Transcript 2 . Object Model UML-1 Venkat Subramaniam
2
. Object Model
Venkat Subramaniam
UML-1
Benefits Of OO Development
• Models System using Objects • Small Semantic gap between reality & model • Understanding the system is easier • Modifications are localized
Venkat Subramaniam
UML-2
Object-Oriented Paradigm
Collection of Discrete Objects Data & Behavior OO Paradigm • Abstraction • Encapsulation • Hierarchy – Inheritance hierarchy (“is-a”) – Part of hierarchy (“has a”) • Polymorphism
Venkat Subramaniam
UML-3
Abstraction
“A simplified description ... of a system that emphasizes some of the system’s details ... while suppressing others” “An abstraction denotes the essential characteristics perspective of an object that distinguish it from all other kind of objects and thus provide crisply defined conceptual boundaries, relative to the of the viewer”
Venkat Subramaniam
UML-4
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 ”
Venkat Subramaniam
UML-5
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 state Examples: jullie
Venkat Subramaniam
dylan kim charlie charlie.fly() stimulus UML-6
What is a Class?
• Group of Objects with similar –properties (attributes) –behavior –relationships to other objects –semantics • Blueprints of Objects
Parrot Example Venkat Subramaniam Girl
jullie kim
Boy
dylan charlie UML-7
Hierarchy
“Hierarchy is a ranking of abstractions” Inheritance : expresses “
is-a
” or “
Kind-of
” relationship • Extensibility & Reusability Part-of: expresses that object is an aggregate another of
Venkat Subramaniam
UML-8
Polymorphism
Hiding alternative procedures behind a Common Interface Send a Message to an object Polymorphism guarantees that the correct/proper implementation is invoked .
Sender
does not
receiver know specific class of
Venkat Subramaniam
UML-9
Object Model
• Captures static structure of system • Objects, relationships, attributes & operations • Most important • Intuitive graphic representation • Valuable for communication & documentation
Venkat Subramaniam
UML-10
Objects
• Decomposing problem into objects – depends on judgement & nature of problem • No one correct representation • Objects have identity
Venkat Subramaniam
UML-11
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
Venkat Subramaniam
UML-12
Class Diagrams
• Provide formal graphic notation for modeling • Concise, easy to understand, practical • Describes many possible instances
Venkat Subramaniam
UML-13
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
Venkat Subramaniam
UML-14
Notation for Classes & Objects (UML)
Girl Boy Classes
Venkat Subramaniam
Objects UML-15
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”
Venkat Subramaniam
UML-16
Attributes...
• Attribute is a pure data value - not an object • Internal identifiers must not be shown as attributes • Show only important attributes
Venkat Subramaniam
UML-17
Derived Attributes
Base 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 operation Example : Area of a circle, age of a person
Venkat Subramaniam
UML-18
Attributes Notation
clas s Nam e -attrPrivate #attrProtected +attrPublic $attrStatic /attrDerived
Underline in revised notation
-attrWType: int -attrWInitVal: double=0.0
Girl -name +dob /age: int
Venkat Subramaniam
UML-19
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
Venkat Subramaniam
UML-20
Operations Notation
className +publicMethod() #protectedMethod() -privateMethod() $staticMethod() +methodWParam(int) +methodWRetVal(): int Girl +sing() +play()
Venkat Subramaniam
UML-21
Venkat Subramaniam
Example with Attributes & Operations
StopWatch -seconds +start() +stop() +reset() +getSeconds(): double UML-22
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 John Association: Woman is-wife-of Man Associations and Links appear as Verbs
Venkat Subramaniam
UML-23
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
Venkat Subramaniam
UML-24
Ternary & High Order Associations
Binary Association : Relates two classes Woman is-wife-of Man Ternary Association : Relates three classes Nancy is-daughter-of Susan and Robert n-ary Association : Relates n classes Higher Order Associations – complicated to draw, implement and think – try to avoid if possible
Venkat Subramaniam
UML-25
Associations Notations
Exactly One * M any 0..1
Optional 1..* One Or M ore 0..* Ze ro Or M ore
Directed Association
*
Company 0..* employs employees 1..* worksFor Person
Venkat Subramaniam
UML-26
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
Venkat Subramaniam
UML-27
Venkat Subramaniam
Association Class Notation
Person * * Company Employment salary:double UML-28
association
Role Names
• Name given to either end of an • Helps to navigate from one object to related objects Woman wife 0..1
married husband 0..1
Man • Helps clarify when two classes have several associations between them Woman
Venkat Subramaniam
wife 0..1
* daughter married children husband 0..1
1 father Man UML-29
objects
class
Qualifiers
• Distinguishes among set of associated • Models associative arrays, dictionaries • Qualifiers may be wrongly
modeled as attribute of associated
GradeList * * Student code Unqualified GradeList code * Qualified 1 Student
Venkat Subramaniam
UML-30
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
Venkat Subramaniam
UML-31
Aggregation & Composition...
Composition: • Part belongs to only one whole (by Value) • Part lives and dies with the whole • Whole cannot replace the part
Venkat Subramaniam
UML-32
Aggregation/ Composition Notation
A ggregation
Has By V alue Has By Re fe r e nce
Vehicle
+drive()
Engine
Venkat Subramaniam
VIN UML-33
Aggregation / Composition Example
Person Aggregation
Venkat Subramaniam
Car Composition brain UML-34
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
Venkat Subramaniam
UML-35
Inheritance Notation
Generalization
Superclass
Vehicle
+drive()
Subclass1 Subclass2
Car
Venkat Subramaniam
-sunRoof Truck UML-36
Inheritance Example
Child Animal Girl
Venkat Subramaniam
Boy Dog Cat UML-37
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
Venkat Subramaniam
UML-38
Package Notation
componentName VehicleComponent
Venkat Subramaniam
Accessories
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
Venkat Subramaniam
UML-40
Aggregation Vs. Inheritance
• Aggregation represents part-of relationship • Inheritance represents kind-of relationship • Aggregation refers to object relationships • Inheritance refers to class relationships
Venkat Subramaniam
UML-41
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
Venkat Subramaniam
UML-42
Layout
Example : Fixed, Variable, Recursive Aggregation
* ElectronicComponent * NAND
Venkat Subramaniam
2 AND FlipFlop UML-43
Operations & Aggregation
• Operation or Triggering : – automatic application of an operation to network of objects when applied to some starting object • ShallowCopy • DeepCopy
Venkat Subramaniam
UML-44
Abstract Classes
Representing an Abstraction that is Abstract.
• Abstract classes represent – concepts – not real objects • ABCs used only to create other “Concrete” classes
Venkat Subramaniam
UML-45
Abstract Classes ...
Example: Shape, Employee, Animal Whether a class in Abstract or not depends on – judgement – application on hand
Venkat Subramaniam
UML-46
Inheritance : Extension & Restriction
Extension : – Subclass adds new features – Subclass inherits all properties & operations of ancestor Restriction : – Subclass constrains ancestor attributes – Subclass may not inherit all properties & operations of its ancestor – Often leads to trouble (Liskov Substitutability Principle)
Venkat Subramaniam
UML-47
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
Venkat Subramaniam
UML-48
Metadata & Metaclass
Metadata : Data that is description of other data Metaclass : Class descriptor objects that describe other classes
Venkat Subramaniam
UML-49
Constraints
Restricts values that entities can assume Entity : objects, classes, attributes, associations Better to capture constraints in structure rather than using constraints constructs ElectronicsClass
Venkat Subramaniam
* {E E Discipline} Students UML-50
Multiple Inheritance
Allows a class to have more than one Superclass • Join class TerrestrialBeing AquaticBeing Amphibian
Venkat Subramaniam
UML-51
Multiple Inheritance Poses Problems
gender Person discipline ComputerScientist CivilEngineer Male Female FemaleComputerScientist • Ambiguous function collisions • Class proliferation
Venkat Subramaniam
UML-52
Workarounds for MI
• Delegation using aggregation of roles • Single inherit important class & delegate rest • Nested Generalization
Venkat Subramaniam
UML-53
Delegation using Aggregation of Roles
Person gender discipline * PersonsDiscipline PersonsGender Male Female ComputerScientist • Inheritance of operations across aggregation not automatic CivilEngineer • Join class must handle that operations and delegate to appropriate component
Venkat Subramaniam
UML-54
Single Inherit Important Class & Delegate Rest
Person gender discipline * PersonsDiscipline Male Female ComputerScientist CivilEngineer • Preserves identity and inheritance across at least one generalization
Venkat Subramaniam
UML-55
Nested Generalization
Person gender Male Female MaleComputerScientist MaleCivilEngineer FemaleComputerScientist FemaleCivilEngineer • Violates the OO spirit • Not practical for large number of combinations
Venkat Subramaniam
UML-56