2. Object Model
Veennkkaattt SSuubbrraamaanniiiaam UML-1
Benefits Of OO Development
• Models System using Objects
• Small Semantic gap between reality & model
• Understanding the system is easier
• Modifications are localized
Veennkkaattt SSuubbrraamaanniiiaam 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 Veennkkaattt SSuubbrraamaanniiiaam 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 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“ Veennkkaattt SSuubbrraamaanniiiaam UML-4 Encapsulation
• Information hiding • Interface - Implementation • Behavior & Data —Encapsulation is the process of compartmentalizingthe elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation“
Veennkkaattt SSuubbrraamaanniiiaam 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 dylan kim charlie charlie.fly() stimulus Veennkkaattt SSuubbrraamaanniiiaam UML-6 What is a Class? • Group of Objects with similar œproperties (attributes) œbehavior œrelationships to other objects œsemantics • Blueprints of Objects
Parrot Example Girl Boy jullie kim dylan charlie Veennkkaattt SSuubbrraamaanniiiaam 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 of another
Veennkkaattt SSuubbrraamaanniiiaam 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 know specific class of receiver
Veennkkaattt SSuubbrraamaanniiiaam UML-9
Object Model
• Captures static structure of system
• Objects, relationships, attributes &
operations
• Most important
• Intuitive graphic representation
• Valuable for communication & documentation
Veennkkaattt SSuubbrraamaanniiiaam UML-10 Objects
• Decomposing problem into objects œ depends on judgement & nature of problem
• No one correct representation
• Objects have identity
Veennkkaattt SSuubbrraamaanniiiaam UML-11
Class • Often appear as nounsin 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
Veennkkaattt SSuubbrraamaanniiiaam UML-12 Class Diagrams
• Provide formal graphic notation for
modeling
• Concise, easy to understand, practical
• Describes many possible instances
Veennkkaattt SSuubbrraamaanniiiaam 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
Veennkkaattt SSuubbrraamaanniiiaam UML-14 Notation for Classes & Objects (UML)
Girl kelly b renda : Girl
Boy d ylan Classes Objects
Veennkkaattt SSuubbrraamaanniiiaam 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“
Veennkkaattt SSuubbrraamaanniiiaam UML-16 Attributes...
• Attribute is a pure data value - not an object • Internal identifiers must not be shown as attributes • Show only important attributes
Veennkkaattt SSuubbrraamaanniiiaam 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
Veennkkaattt SSuubbrraamaanniiiaam UML-18 Attributes Notation
className Girl -attrPrivate #attrProtected -name +attrPublic Underline in revised $attrStatic notation +dob /attrDerived /age: int -attrWType: int -attrWInitVal: double=0.0
Veennkkaattt SSuubbrraamaanniiiaam 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
Veennkkaattt SSuubbrraamaanniiiaam UML-20 Operations Notation
className
+publicMethod() Girl #protectedMethod() -privateMethod() +sing() $staticMethod() +methodWParam(int) +play() +methodWRetVal(): int
Veennkkaattt SSuubbrraamaanniiiaam UML-21
Example with Attributes & Operations
StopWatch
-seconds +start() +stop() +reset() +getSeconds(): double
Veennkkaattt SSuubbrraamaanniiiaam 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
Veennkkaattt SSuubbrraamaanniiiaam 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 Veennkkaattt SSuubbrraamaanniiiaam 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
Veennkkaattt SSuubbrraamaanniiiaam UML-25
Associations Notations
Exactly One * Many 0..1 Directed Association Optional * 1..* One Or More 0..* Zero Or More
0..* 1..* Company employees Person employs worksFor
Veennkkaattt SSuubbrraamaanniiiaam 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 Veennkkaattt SSuubbrraamaanniiiaam UML-27
Association Class Notation
Person * * Company
Employment salary:double
Veennkkaattt SSuubbrraamaanniiiaam UML-28 Role Names • Name given to either end of an association • Helps to navigate from one object to related objects wife husband Woman 0..1 married 0..1 Man
• Helps clarify when two classes have several associations between them wife husband 0..1 married 0..1 Woman Man * 1 daughter children father Veennkkaattt SSuubbrraamaanniiiaam UML-29
Qualifiers • Distinguishes among set of associated objects • Models associative arrays, dictionaries • Qualifiers may be wrongly modeled as attribute of associated class
Student GradeList * * code Unqualified
1 GradeList code * Student Qualified Veennkkaattt SSuubbrraamaanniiiaam 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
Veennkkaattt SSuubbrraamaanniiiaam 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
Veennkkaattt SSuubbrraamaanniiiaam UML-32 Aggregation/ Composition Notation Aggregation
Has By Value
Has By Reference
Engine Vehicle
+drive()
VIN Veennkkaattt SSuubbrraamaanniiiaam UML-33
Aggregation / Composition Example
Person brain Composition
Aggregation
Car
Veennkkaattt SSuubbrraamaanniiiaam 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
Veennkkaattt SSuubbrraamaanniiiaam UML-35
Inheritance Notation Generalization
Superclass Vehicle
+drive() Subclass1 Subclass2
Car Truck
-sunRoof Veennkkaattt SSuubbrraamaanniiiaam UML-36 Inheritance Example
Child Animal
Girl Boy Dog Cat
Veennkkaattt SSuubbrraamaanniiiaam 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 Veennkkaattt SSuubbrraamaanniiiaam UML-38 Package Notation
componentName VehicleComponent
Veennkkaattt SSuubbrraamaanniiiaam Accessories EngineCompoUnMenLt-39
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 Veennkkaattt SSuubbrraamaanniiiaam 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 Veennkkaattt SSuubbrraamaanniiiaam 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 Veennkkaattt SSuubbrraamaanniiiaam UML-42 Example : Fixed, Variable, Recursive Aggregation
* * Layout ElectronicComponent
2 NAND AND FlipFlop
Veennkkaattt SSuubbrraamaanniiiaam UML-43
Operations & Aggregation
• Operation or Triggering : œ automatic application of an operation to network of objects when applied to some starting object
• ShallowCopy
• DeepCopy
Veennkkaattt SSuubbrraamaanniiiaam 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
Veennkkaattt SSuubbrraamaanniiiaam UML-45
Abstract Classes ...
Example: Shape, Employee, Animal
Whether a class in Abstract or not depends on œ judgement œ application on hand
Veennkkaattt SSuubbrraamaanniiiaam 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)
Veennkkaattt SSuubbrraamaanniiiaam 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
Veennkkaattt SSuubbrraamaanniiiaam UML-48 Metadata & Metaclass
Metadata : Data that is description of other data
Metaclass : Class descriptor objects that describe other classes
Veennkkaattt SSuubbrraamaanniiiaam 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 Students {E E Discipline}
Veennkkaattt SSuubbrraamaanniiiaam UML-50 Multiple Inheritance
Allows a class to have more than one Superclass • Join class
TerrestrialBeing AquaticBeing
Amphibian Veennkkaattt SSuubbrraamaanniiiaam UML-51
Multiple Inheritance Poses Problems
Person
gender discipline
Male Female ComputerScientist CivilEngineer
FemaleComputerScientist
• Ambiguous function collisions • Class proliferation Veennkkaattt SSuubbrraamaanniiiaam UML-52 Workarounds for MI
• Delegation using aggregation of roles
• Single inherit important class & delegate
rest
• Nested Generalization
Veennkkaattt SSuubbrraamaanniiiaam UML-53
Delegation using Aggregation of Roles
Person gender discipline *
PersonsGender PersonsDiscipline
Male Female ComputerScientist CivilEngineer • Inheritance of operations across aggregation not automatic • Join class must handle that operations and delegate to appropriate component
Veennkkaattt SSuubbrraamaanniiiaam UML-54 Single Inherit Important Class & Delegate Rest
Person discipline *
gender PersonsDiscipline
Male Female ComputerScientist CivilEngineer • Preserves identity and inheritance across at least one generalization
Veennkkaattt SSuubbrraamaanniiiaam UML-55
Nested Generalization
Person
gender
Male Female
MaleComputerScientist MaleCivilEngineer FemaleComputerScientist FemaleCivilEngineer
• Violates the OO spirit • Not practical for large number of combinations Veennkkaattt SSuubbrraamaanniiiaam UML-56