11-Class Diagram
11-Class Diagram
Objectives
• Describe the static view of the system and show how to
capture it in a model.
• Demonstrate how to read and interpret a class diagram.
• Model an association and aggregation and show how to
model it in a class diagram.
• Model generalization on a class diagram.
@Nguyễn Thị Thu Trang, [email protected] 3
Content
1. Class diagrams
2. Association
3. Aggregation and Composition
4. Generalization
@NGUYỄN Thị Thu Trang, [email protected] 4
Professor
Professor Torpie
What Is an Attribute?
• An attribute is a named property of a class that describes
the range of values that instances of the property may
hold.
• A class may have any number of attributes or no
attributes at all.
Student
- name
- address
Attributes
- studentID
- dateOfBirth
@NGUYỄN Thị Thu Trang, [email protected] 7
What Is an Operation?
• A service that can be requested from an object to effect
behavior. An operation has a signature, which may restrict
the actual parameters that are possible.
• A class may have any number of operations or none at all.
Student
+ get tuition()
+ add schedule()
Operations + get schedule()
+ delete schedule()
+ has prerequisites()
@NGUYỄN Thị Thu Trang, [email protected] 9
Member Visibility
• Visibility is used to enforce encapsulation
• May be public, protected, or private
Private
operations
Public Protected
operations operations
@NGUYỄN Thị Thu Trang, [email protected] 10
ClassName
- privateAttribute
+ publicAttribute
# protectedAttribute
- privateOperation ()
+ publicOperation ()
# protecteOperation ()
@Nguyễn Thị Thu Trang, [email protected] 11
Scope
• Determines number of instances of the
attribute/operation
• Instance: one instance for each class instance
• Classifier: one instance for all class instances
• Classifier scope is denoted by underlining the
attribute/operation name
Class1
- classifierScopeAttr
- instanceScopeAttr
+ classifierScopeOp ()
+ instanceScopeOp ()
@Nguyễn Thị Thu Trang, [email protected] 12
LoginForm RegistrationController
RegisterForCoursesForm
Schedule
CloseRegistrationForm CloseRegistrationController
Professor
Student
Course CourseOffering
CourseCatalogSystem
BillingSystem
@Nguyễn Thị Thu Trang, [email protected] 15
University
Artifacts
@Nguyễn Thị Thu Trang, [email protected] 16
Registration
CloseRegistrationForm CloseRegistrationController
RegisterForCoursesForm RegistrationController
@NGUYỄN Thị Thu Trang, [email protected] 17
Student
Artifacts Professor
University
Artifacts Course Schedule
Student CourseOffering
@Nguyễn Thị Thu Trang, [email protected] 18
Class Relationships
• Association
• Aggregation
• Composition
• Generalization
• Realization
@Nguyễn Thị Thu Trang, [email protected] 19
Content
1. Class diagrams
2. Association
3. Aggregation and Composition
4. Generalization
@Nguyễn Thị Thu Trang, [email protected] 20
What Is an Association?
• The semantic relationship between two or more classifiers
that specifies connections among their instances.
• A structural relationship specifying that objects of one thing
are connected to objects of another thing.
: Student
8: any conflicts?( )
3: save( )
4: submit( )
6: has pre-requisites(CourseOffering)
: CourseOffering
: Student
5: is selected?( )
10: mark as enrolled in( )
: PrimaryScheduleOfferingInfo
@Nguyễn Thị Thu Trang, [email protected] 22
Role
• Role
• Useful technique for specifying the context of a class
and its objects
• Optional
• Role name
• String placed near the end of the association next to the
class to which it applies
• Indicates the role played by the class in terms of the
association.
• Part of the association and not part of the classes
@Nguyễn Thị Thu Trang, [email protected] 23
What Is Multiplicity?
• Multiplicity is the number of instances one class relates
to ONE instance of another class.
• For each association, there are two multiplicity
decisions to make, one for each end of the association.
• For each instance of Professor, many Course Offerings may be
taught.
• For each instance of Course Offering, there may be either one
or zero Professor as the instructor.
instructor
Professor CourseOffering
0..1 0..*
@Nguyễn Thị Thu Trang, [email protected] 24
Multiplicity Indicators
Unspecified
Exactly One 1
Zero or More *
Example: Multiplicity
1
RegisterForCoursesForm RegistrationController
1
0..1
0..1
1 0..*
Student Schedule CourseOffering
0..* 0..4
@Nguyễn Thị Thu Trang, [email protected] 26
Many-to-many association
@Nguyễn Thị Thu Trang, [email protected] 27
Java
implementation for
Association
//InsuranceCompany.java file
public class InsuranceCompany
{
// Many multiplicity can be implemented using Collection
private List<InsuranceContract> contracts;
/* Methods */
}
// InsuranceContract.java file Field Reference
public class InsuranceContract
{
private InsuranceCompany refers_to;
InsuranceContract(){ }
/* Methods */
public void setCompany(InsuranceCompany comp) { refers_to = comp; }
}
@Nguyễn Thị Thu Trang, [email protected] 28
Content
1. Class diagrams
2. Association
3. Aggregation and Composition
4. Generalization
@Nguyễn Thị Thu Trang, [email protected] 29
What Is an Aggregation?
• A special form of association that models a whole-part
relationship between the aggregate (the whole) and its
parts.
• An aggregation is an “is a part-of” relationship.
• Multiplicity is represented like other associations.
Whole 1 Part
0..1
@Nguyễn Thị Thu Trang, [email protected] 30
What is Composition?
• A special form of aggregation with strong
ownership and coincident lifetimes of the part with
the aggregate
• Also called composition aggregate
• The whole “owns” the part and is responsible for
the creation and destruction of the part.
• The part is removed when the whole is removed.
• The part may be removed (by the whole) before the
whole is removed.
Whole Part
@Nguyễn Thị Thu Trang, [email protected] 31
Aggregation Example
Composition Example
• A compound aggregate is shown as attributes in a class
@Nguyễn Thị Thu Trang, [email protected] 35
Car(Engine engine) {
this.engine = engine;
}
Content
1. Class diagrams
2. Association
3. Aggregation and Composition
4. Generalization
@Nguyễn Thị Thu Trang, [email protected] 37
+ withdraw()
+ createStatement()
Generalization
Relationship
Subclasses
Savings Checking
(children)
Descendents
@Nguyễn Thị Thu Trang, [email protected] 39
FlyingThing Animal
Multiple Inheritance
Lion Tiger
+ communicate () + communicate ()
WindowWithScrollbar
1
WindowWithScrollbar Scrollbar
1
Interfaces and Realizes Relationships
<<interface>>
Serializable Serializable
Schedule
Schedule
Icon presentation
Normal presentation
@Nguyễn Thị Thu Trang, [email protected] 44
Exercise
Document a class diagram using the following information:
• A class diagram containing the following classes: Personal
Planner Profile, Personal Planner Controller, Customer
Profile, and Buyer Record and other necessary ones.
• Associations drawn using the following information:
• Each Personal Planner Profile object can be associated with up to
one Personal Planner Controller object.
• Each Personal Planner Controller object must be related to one
Personal Planner Profile.
• A Personal Planner Controller object can be associated with up to
one Buyer Record and Customer Profile object.
• An instance of the Buyer Record class can be related to zero or one
Personal Planner Controller.
• Zero or one Personal Planner Controller objects are associated with
each Customer Profile instance
• A customer can become a VIP one.