2. Week 13
Topics to be Discussed:
What are class Diagrams?
What is a class?
CRC(Class, Responsibility, Collaborator)
UML Class Diagram Notation
How to draw Class Diagrams
Relationships between classes
Purpose of Class Diagrams
Benefits of Class Diagrams
Simple Examples
4. Design Phase
Design : specifying the structure of how a
software system will be written and function,
without writing the complete implementation.
A transition from "what" the system must do,
to "how" the system will do it
1. What classes will we need to implement a
system that meets our requirements?
2. What fields and methods will each class
have?
3. How will the classes interact with each
other?
5. Customer
Account
Opens account
Knows name
Knows address
Account
Manager
Knows interest rate
Knows balance
Handles deposits
Reports fraud to
manager
CRC (Class responsibility collaborator)
A Class-Responsibility-Collaboration
(CRC) card is a structured tool used in
object-oriented software design.
It captures essential information about a
class, including its name, responsibilities,
and collaborations with other classes.
CRC cards facilitate collaborative
discussions among team members and help
in refining the design of software systems.
6. UML Class Diagrams
What is a UML class diagram?
UML class diagram: a picture of
The classes in an OO system,
Their fields and methods,
Connections between the classes
that interact or inherit from each
other,
Algorithmic details; how a
particular behavior is implemented.
7. Class Diagrams
Class diagrams are used when developing an object-oriented system
model to show the classes in a system and the associations between
these classes.
An object class can be thought of as a general definition of one kind of
system object.
An association is a link between classes that indicates that there is
some relationship between these classes.
When you are developing models during the early stages of the
software engineering process, objects represent something in the real
world, such as a patient, a prescription, doctor, etc.
8. UML Notation for Classes
ClassName
The top compartment shows the class
name.
field1
…
fieldn
The middle compartment contains the
declarations of the fields of the class.
method1
…
methodm
The bottom compartment contains the
declarations of the methods
9. How to Draw Class Diagrams
Drawing class diagrams involves visualizing the structure of a system, including classes, their attributes,
methods, and relationships.
Step 1: Identify Classes:
1. Start by identifying the classes in the system.
2. A class represents a blueprint for objects and should encapsulate related attributes and methods.
Step 2: List Attributes and Methods:
1. For each class, list its attributes (properties, fields) and methods (functions, operations).
2. Include information such as data types and visibility (public, private, protected).
Step 3: Identify Relationships:
1. Determine the relationships between classes.
2. Common relationships include associations, aggregations, compositions, inheritance, and
dependencies.
3. Understand the nature and multiplicity of these relationships.
Step 4: Create Class Boxes:
1. Draw a rectangle (class box) for each class identified.
2. Place the class name in the top compartment of the box.
3. Divide the box into compartments for attributes and methods.
10. Step 5: Add Attributes and Methods:
1. Inside each class box, list the attributes and methods in their respective compartments.
2. Use visibility notations (+ for public, – for private, # for protected, ~ for package/default).
Step 6: Draw Relationships:
1. Draw lines to represent relationships between classes.
2. Use arrows to indicate the direction of associations or dependencies.
3. Different line types or notations may be used for various relationships.
Step 7: Label Relationships:
1. Label the relationships with multiplicity and role names if needed.
2. Multiplicity indicates the number of instances involved in the relationship, and role names clarify the
role of each class in the relationship.
Step 8: Review and Refine:
1. Review the class diagram to ensure it accurately represents the system’s structure and relationships.
2. Refine the diagram as needed based on feedback and requirements.
How to Draw Class Diagrams
11. Class attributes
attributes (fields, instance variables)
visibility name : type [count] = default_value
1. visibility: + public
# protected
- private
~ package (default)
/ derived
2. underline static attributes
3. Derived attribute: not stored, but can
be computed from other attribute values
4. attribute example:
- balance : double = 0.00
12. Class Operations / Methods
operations / methods
visibility name (parameters) : return_type
1. visibility: + public
# protected
- private
~ package (default)
2. underline static methods
3. parameter types listed as (name: type)
4. omit return_type on constructors and
when return type is void
5. method example:
+ distance(p1: Point, p2: Point): double
13. Comments
represented as a folded note, attached to the appropriate class / method /
etc. by a dashed line
15. Relationships between classes
Generalization :
An inheritance relationship,
1. Inheritance between classes
2. Interface implementation
Association :
A usage relationship
1. Dependency
2. Aggregation
3. Composition
16. Generalization Relationships
Generalization (inheritance) relationships
1. hierarchies drawn top-down with arrows
pointing upward to parent
2. line/arrow styles differ, based on
whether parent is a(n):
1. class: solid line, black arrow
2. abstract class: solid line, white
arrow
3. interface: dashed line, white arrow
17. Associational Relationships
Associational (usage) relationships
1. Multiplicity : (how many are used)
* ⇒ 0, 1, or more
1 ⇒ 1 exactly
2..4 ⇒ between 2 and 4, inclusive
3..* ⇒ 3 or more
2. Name : (what relationship the objects have)
3. Navigability : (direction)
18. one-to-one
each student must carry exactly one ID card
one-to-many
one rectangle list can contain many rectangles
Multiplicity of Associations
19. Association Types
1. aggregation: "is part of"
symbolized by a clear white
diamond.
2. composition: "is entirely made of"
stronger version of aggregation
the parts live and die with the whole.
symbolized by a black diamond.
3. dependency: "uses temporarily"
symbolized by dotted line
often is an implementation
detail, not an intrinsic part of
that object's state
1
1
Car
aggregation
Engine
Lottery
Ticket
Random
dependency
Page
Book
composition
*
1
20. Purpose of Class Diagrams
The main purpose of using class diagrams is:
1. This is the only UML that can
appropriately depict various aspects of
the OOPs concept.
2. Proper design and analysis of
applications can be faster and efficient.
3. It is the base for deployment and
component diagram.
4. It incorporates forward and reverse
engineering.
21. Benefits of Class Diagrams
1. Clarity and Communication
2. Standardization
3. Facilitates Object-Oriented Design
4. Documentation and Maintenance
5. Support for Code Generation
25. DVD Movie VHS Movie Video Game
Rental Item
Rental Invoice
1..*
1
Customer
Checkout Screen
0..1
1
Simple
Association
Class
Abstract
Class
Simple
Aggregation
Generalization
Composition
Multiplicity
Class Diagram
Example: 03: Video Store
27. Structural UML diagram provide a visual language for representing the static structure of a
system, offering clarity and facilitating communication through class diagrams, object
diagrams, package diagrams, and more.
These diagrams offer insights into the relationships, dependencies, and organization of
elements within a software system.
UML Class Diagrams are a powerful tool in software engineering, offering significant
advantages in clarity, standardization, and support for object-oriented design.
However, it is essential to be aware of their limitations and use them in conjunction with other
modeling techniques to achieve a comprehensive understanding of the system.
Summary