Computer Engineering Department
Computer Engineering Department
Object Oriented Software Modeling and Design
CE 350Abdel-Karim Al-Tamimi, Ph.D.
[email protected]://faculty.yu.edu.jo/
altamimi
Al-Tamimi 2011 © 1
Overview
• Introduction UML
Al-Tamimi 2011 © 2
UML Definition and Usage
• UML stands for : Unified Modeling Language
• UML helps in facilitating:– Specification– Visualization– Architecture Design– Construction– Simulation and Testing– Documentation
Al-Tamimi 2011 © 3
UML Basics
• A model helps getting an abstract idea of the objects the software product consists of and their interactions– Employ information hiding in the model
• Model’s point of view is important to determine the final shape of the diagram and the necessary information to be included –Use-case diagram vs. Class diagram
Al-Tamimi 2011 © 4
Using Models/Diagrams
• It is easier to change models than code
• Allow non-technical team members/ clients to understand the structure of the system and be active in decision making
• Client can check if the system’s design is confined to the agreed-on requirements
Al-Tamimi 2011 © 5
Types of Diagrams
• There are several types of diagrams :–Use-Case Diagrams– Activity Diagrams– Class Diagrams– Interaction Diagrams– State Diagrams
Al-Tamimi 2011 © 6
Types of Diagrams
Al-Tamimi 2011 © 7
UML: Fundamental Level
• The first level examines fundamental UML knowledge, including basic notions on class diagrams, activity diagrams, interaction diagrams, and use case diagrams, as well as various elements such as standard stereotypes and primitive types
• These basics include not only the elements visible to UML users, but also abstract UML concepts, such as Element—the base class for all UML classes in the metamodel
Al-Tamimi 2011 © 8
Use Case Diagram: Introduction
• It lists the capabilities of the system (including their users)
• Documents the macro requirements of the system
Al-Tamimi 2011 © 9
Borrow Book
Borrow Book
Student
Activity Diagram
• In UML an activity diagram is a version of flowchart
• Activity diagrams are used to analyze processes, or even process re-engineering
• Very helpful in understanding the overall process without indulging in technical issues
• Explains the steps (actions) the user takes to achieve a certain milestone/milestones
• There are a Start point and an End point
Al-Tamimi 2011 © 10
Activity Diagram: Example
Al-Tamimi 2011 © 11
Leave HouseLeave House
Take ClassTake Class Wander AroundWander Around
Return to House
Return to House
Take another Class
Done for today ?
Arrive to university
End of day
Class Diagram
• Show the classes in the system and the relationships between these classes
• There are many possible class diagrams, and a single class can be shown in more than one class diagram
• Show the classes and their relationships in the best way to understand the system
• Does not explain the classes interactions, just a static view of the system
Al-Tamimi 2011 © 12
Class Diagrams: Example
Al-Tamimi 2011 © 13
StudentStudentBookBook
ReferenceReferenceMagazineMagazine
Interaction Diagrams
• Interaction diagram are categorized into:– Sequence diagrams• Models a single flow, the classes are on top
– Collaboration diagrams• Messages and classes are organized in the spatial
space, no single flow
• These diagram represent the same information using different perspectives
• Shows objects and messages between them
Al-Tamimi 2011 © 14
Interaction Diagrams – Sequence Diagram
Al-Tamimi 2011 © 15
StudentStudent BookBook LibraryLibrary
Walk to school
Access Library
Browse Book
Borrow book
Read book
Interaction Diagrams – Collaboration Diagram
Al-Tamimi 2011 © 16
BookBook
StudentStudent
LibraryLibrary
1-Walk to school5-Read book
3-Browse book4-Borrow book
2-Access library
State Diagram
• Shows the changing state of a single object as it passes through the system
Al-Tamimi 2011 © 17
WalkingWalkingLeave
the house
StudyingStudyingArrive
to School
/ take classDone
RestingRestingHave a break
Yes
UML Notations
Al-Tamimi 2011 © 18
UML Notation of Objects and Classes
Al-Tamimi 2011 © 19
ClassClass
ObjectObject
- indicates private attributes/methods+ indicates public attributes/methods# indicates protected attributes/methods~ indicates package visible attributes/methods
Notice the naming conventionBankAccount
BankAccountBankAccount
- name- balance- name- balance+ debit (int amount): void+ credit (int amount): void
+ debit (int amount): void+ credit (int amount): void
Object1 : BankAccountObject1 : BankAccount
name = John Smithbalance = 1,000name = John Smithbalance = 1,000
Object2 : BankAccountObject2 : BankAccount
name = Robert Jonesbalance = -200name = Robert Jonesbalance = -200
Visibility / Access ModifiersReview
• Public : visible to all elements that can access the contents of the namespace that owns it
• Private: only visible inside the namespace that owns it
• Protected: visible to elements that have a generalization relationship to the namespace that owns it
• Package: Any element marked as having package visibility is visible to all elements within the nearest enclosing package
Al-Tamimi 2011 © 20
UML Class Example
Al-Tamimi 2011 © 21
StudentStudent
-StudentName:String-StudentID:Integer-DOB:DateTime-GPA:Double=0.0
-StudentName:String-StudentID:Integer-DOB:DateTime-GPA:Double=0.0
+ getGPA():Double+ setName(Name:String)- CalculateGPA():Double
+ getGPA():Double+ setName(Name:String)- CalculateGPA():Double
Relationships between Classes
• There are essentially four important types of relationships between classes: –Generalization/specialization
• Inheritance– Aggregation (“part-of”)– Association
• Role and the connection between the classes– Realization
• one model element (the client) realizes the behavior that the other model element (the supplier) specifies
Al-Tamimi 2011 © 22
Generalization / Specialization Relationship
Al-Tamimi 2011 © 23
BankAccount attributes and methods are not specified
again
BankAccount attributes and methods are not specified
again
New attribute “interest” is shown in the
subclass
New attribute “interest” is shown in the
subclass
Generalization
Generalization
Specialization
Specialization
Generalization / Specialization Relationship• An abstract class is used to specify the
required behaviors (operations) of a class, without having to provide their actual implementations
• An operation without the implementation (body) is called an abstract operation
• A class with one or more abstract operations is an abstract class
• An abstract class cannot be instantiated because it does not have the required implementation of the abstract operations
Al-Tamimi 2011 © 24
Association Relationship
• Object-oriented systems are made up of objects of many classes. Associations represent relationships among classes
• An association is represented by a line drawn between the associated classes involved with an optional role name attached to either end
• The role name is used to specify the role of an associated class in the association
• If an association connects two objects instead of classes, it is called a link
• A link is an instance of an association.
Al-Tamimi 2011 © 25
Association Relationship
Al-Tamimi 2011 © 26
Name of Link
Multiplicity
Role
Name of Association
Multiplicity
Al-Tamimi 2011 © 27
UML Multiplicity Meaning
1 Exactly 1 (default)
2 Exactly 2
1..3 From 1 to 3 inclusive
3, 5 Either 3 or 5
1..* At least [1], and at most [unlimited]
* Unlimited (including 0)
0..1 Either 0 or 1
Multiplicity: Example
Al-Tamimi 2011 © 28
StudentStudent BookBook
borrows
0..1 0..5
ProfessorProfessorborro
ws
*
0..1
Qualification Relationship
• Qualification serves as names or keys that are part of the association and are used to select/qualify objects across the association at the other end
• In UML, a qualifier is used to model this association semantic, that is an association attribute whose value determines a unique object or a subset of objects at the other end of an association
• The qualifier goes at the opposite end of the association from the class of which it’s an attribute
Al-Tamimi 2011 © 29
Qualification Relationship
Al-Tamimi 2011 © 30
Account Number a single person
Account number acted as a filter
Qualification Relationship: Example
Al-Tamimi 2011 © 31
DriverDriver vehiclevehicledrives
But if he can only drive 4 wheels vehicles:
DriverDriver vehiclevehicledrives
4 wheels
4 wheels
What if he can only drive his own car:
DriverDriver vehiclevehicledrives
License plate
License plate
Reflexive Associations
• Relates one object of a class to another object of the same class
• In other words, a class can be associated with itself
• Reflexive associations are two types– directional – and bi-directional
Al-Tamimi 2011 © 32
Reflexive Association: Example
Al-Tamimi 2011 © 33
Reflexive Association: Example
Al-Tamimi 2011 © 34
Directional Bi-directional
Association Classes
Al-Tamimi 2011 © 35
Sometimes, it is necessary to describe an association by including some attributes which do not naturally belong to the objects involved in the association
Enrollment is an association class
Association Classes: Example
Al-Tamimi 2011 © 36
Can a person have multiple positions at the same company ?
N-ary Association
Al-Tamimi 2011 © 37
N-ary Association
Al-Tamimi 2011 © 38
3 or a Ternary association
N-ary Association
Al-Tamimi 2011 © 39
decomposed
Aggregation Relationship• Aggregation is a stronger form of association • It represents the has-a or part-of relationship• In UML, a link is placed between the “whole”
and the “parts” classes with a diamond head attached to the “whole” class to indicate that this association is an aggregation
• Multiplicity can be specified at the end of the association for each of the “part-of” classes to indicate the quantity of the constituent parts
• Typically, aggregations are not named, and the keywords used to identify aggregations are “consists of”, “contains” or “is part of”
Al-Tamimi 2011 © 40
Aggregation : Example
Al-Tamimi 2011 © 41
Aggregation: Example
Al-Tamimi 2011 © 42
CompanyCompany
warehousewarehouse SalesmanSalesman StoreStore
0..*
1..*
0..*
0..* 1..* 0..*
Composition Relationship
• A stronger form of aggregation is called composition, which implies exclusive ownership of the “part-of” classes by the “whole” class, i.e. a composite object has exclusive ownership of the parts objects
• This means that parts may be created after a composite is created, but such parts will be explicitly removed before the destruction of the composite
• It is represented using a filled diamond head
Al-Tamimi 2011 © 43
Composition: Example
Al-Tamimi 2011 © 44
Composition: Example
Al-Tamimi 2011 © 45
ReportReport
TitleTitle ColumnColumn FigureFigure
1
1
1
1..* 1..* 0..*
General Notes
• Generalization indicates "is-a” relationship
• Composition indicates “part-of” relationship
• Aggregation indicates “has-a” relationship
Al-Tamimi 2011 © 46
CarCar EngineEngine PersonPerson AddressAddress
BookBook Reference
Reference
Difference Between Aggregation and Composition
Relationships• When attempting to represent real-world
whole-part relationships, e.g., an engine is part of a car, the composition relationship is most appropriate– A department belongs to only one university
• When representing a software or database relationship, e.g., ConvertToPDF component is a part of a software library Printing, an aggregation relationship is best
• Aggregation - Without whole part can exist. Composition - Without whole part can't exit.
Al-Tamimi 2011 © 47
Dependency Relationship
• Dependency is a weaker form of relationship which indicates that one class depends on another because it uses it at some point of time
• Dependency exists if a class is a parameter variable or local variable of a method of another class– The class uses the other’s class object(s) in its
runtime– In use-case diagrams is used to indicate that a
change to the supplier might require a change to the client
Al-Tamimi 2011 © 48
Dependency Relationship: Example
Al-Tamimi 2011 © 49
ServerServer DatabaseConnectionManager
DatabaseConnectionManager<<use>>
Dependency Relationships in UML (1 of 2)
Type of Dependency
Keyword Description
Abstraction «abstraction», «derive», «refine», or «trace»
Relates two model elements, or sets of model elements, that represent the same concept at different levels of abstraction, or from different viewpoints
Binding «bind» Connects template arguments to template parameters to create model elements from templates
Al-Tamimi 2011 © 50
Dependency Relationships in UML (2 of 2)
Type of Dependency
Keyword Description
Realization «realize» Indicates that the client model element is an implementation of the supplier model element, and the supplier model element is the specification
Substitution «bind» Indicates that the client model element takes the place of the supplier; the client model element must conform to the contract or interface that the supplier model element establishes
Usage «use», «call», «create», «instantiate», or «send»
Indicates that one model element requires another model element for its full implementation or operationAl-Tamimi 2011 © 51
Realization Relationship
• In this relationship, one element realizes the behavior that the other element specifies
• Usually associated with class and component diagrams
• Usually represents the relationship between an interface (abstract class) and a sub-class that implements the interface
Al-Tamimi 2011 © 52
Realization Relationship: Example
Al-Tamimi 2011 © 53
Payable<<interface>
>
Payable<<interface>
>
EmployeeEmployeeInvoiceInvoice
SalaryEmployee
SalaryEmployee
Constraints and Notes
• Constraints are an extension of the semantics of a UML element, allowing one to add new rules or modify existing ones. Sometimes it is helpful to present an idea about restrictions on attributes and associations for which there is no specific notation
• Constraints are represented by a label in curly brackets ({constraintName} or {expression}) that are attached to the constrained element
Al-Tamimi 2011 © 54
Constraints and Notes: Example
Al-Tamimi 2011 © 55
Constraints and Notes: Example
Al-Tamimi 2011 © 56
Resources
• Matt Weisfeld, The Object Oriented Thought Process, Sams Publishing, ISBN:0-672-32611-6
• M. Jesse, “UML 2.0 for dummies”, ISBN:0764526146
• P. Kimmel, “UML Demystified”, McGraw Hill, ISBN: 0-07-148671-2
• http://www.thefullwiki.org/Unified_Modeling_Language
57Al-Tamimi 2011 ©
Resources
• Object oriented technology, By Tsang, Lau & Leung 2005, Mcgraw-Hill
• M. Chonoles and J. Schardt, UML 2 for dummies, ISBN:0764526146
• http://publib.boulder.ibm.com/infocenter/rsmhelp/v7r0m0/index.jsp?topic=/com.ibm.xtools.modeler.doc/topics/cdepend.html
Al-Tamimi 2011 © 58