class diagram
TRANSCRIPT
1
Session
Class Diagram
Checklist
Introduction to Class diagram
Explain Class diagram notations
Construct Class diagram for Case Study
2
Class Diagram
Class Diagram is the most important & most used diagram in Unified Modeling Language. They
are used to communicate the design of the system in Object Oriented Programming Model. It
represents the type of objects in a system and various static relationships that exist among
them.
I am going to take up a problem statement, describe the elements of a class diagram and how
to construct one.
The Problem Statement
Our client is a grocery store who needs a system to use in his billing counters. Our client’s
employees will be using our system. Billing counter operator enters the product code of an item
to the system. System should generate order details for each item. Our system should also
consider price reduction if there are any special offers applicable for an item. Operator then
enters the quantity to calculate its price. Operator has to repeat these steps for each item. Our
system provides a bill receipt once the cash is paid.
Some additional requirement to be considered
1. Order details include Name of the product, weight and special offers
2. Mode of payment is only cash. Credit Cards are not accepted
3. Price reduction is applicable for products with offer
4. Grocery shops registered customers can add loyalty points for their purchase. They can
redeem their points for a particular purchase.
5. Operator can update grocery shops registered customers information in our system.
Figure 14 shows the class diagram of our order processing system. Note: This diagram is neither
a complete representation of a system nor a design of a working prototype. Our main goal is to
understand class diagram elements.
4
Elements of Class Diagram
Class Compartment
Classes are represented with boxes. Classes have three primary compartments
Figure 2: Different views of class
1. Name of the class
a. First compartment represents the name of the class. It is located at the center of
the compartment. Stereotypes (<<>>) may be used to limit the role of the class
and is placed at the top of the class name. Some common examples of
stereotypes are <<interface>> to represent Java interfaces, <<Broker>> to
represent Broker design pattern. There can be user defined stereotypes also.
b. Compartments are optional, you can skip a compartment or simply represent
class name alone as shown in the figure above.
c. Abstract class should be in Italics
d. Static class should be underlined
2. Attributes Compartment
a. This compartment holds the variables that are used by a class.
b. Syntax for representing an attribute is as follows;
5
c. Example for this is:
i. The visibility (optional) marker indicates whether an
attribute is public (+), private (-) or protected (#).
ii. The name of the variable used in the class.
iii. The type of the variable – int, float, char or string etc.
iv. Refer multiplicity (optional) element heading.
v. The default (optional) value is the value which the variable holds
when the object is created.
vi. The {property - string} (optional) allows you to indicate
additional property for an attribute.
3. Operation Compartment
a. This compartment holds the functions that are used by a class for performing
functionalities.
b. Syntax for representing operation attribute is as follows;
c. Example for this is :
Association
Association tells us how object of one class interact with objects of other type class. Figure 16
shows association between two classes Person and Car. Association is represented by a solid
line between two classes [Person and Car]. Name of the association is represented on the
association line [Owns]. There is no restriction on placing the association name. It can be placed
anywhere between the line. Directional arrow [] is optional.
visibility name: type multiplicity = default {property - string}
+ name: String [1] = “Untitled” {readOnly}
visibility operationName(argname: data type{constraint},… ):
return data type{constraint}
+ isAvailable( productCode: Integer{unique} ): Boolean
6
Figure 3: Association
Multiplicity
Association defines the rules for how many objects may participate in a relationship through
multiplicity. A multiplicity value must be assigned to each participating class in an association.
Multiplicity values should be placed at the end of an association line.
It is represented by a range [Minimum … Maximum]. The following are the optional ways to
represent multiplicity.
Values separated by two periods (..) means a range 2..6. 1..1 is equivalent to 1
(*) represents no lower bound and upper bound
1..* means no upper limit
Association Roles
Association role tells how an object participates in an association. They are different from
association names. Association names do not generate code but roles generate code.
Association roles are placed at the end of the association next to the type of object that plays
the role.
Figure 4: Multiplicity
7
Figure 5: Association roles
Association Class
An Association class is used to encapsulate information about an association. Association class
is attached to the association line with dashed lines as shown in the figure below. Here the
association class encapsulates special offer information about a product.
Figure 6: Association Class
Aggregation & Composition
Aggregation and composition are special type of association which tells us about relationship
between objects in Object Oriented Programming. These two concepts are subjected to
confusion, so I am going to explain them in parallel
8
Table 1: Aggregation & Composition
Aggregation Composition
An Association is known as Aggregation when one object USES other object
An Association is known as Composition when one object OWNS other object
In Aggregation participating objects can be assembled or configured to create a complex object.
In Composition line span of one object depends on other object. One class has the control to create and delete an object
Aggregation is represented by diamond at the end of the association that is attached to aggregate class
Figure 7: Aggregation Notation
Composition is represented is filled diamond and is also attached to the aggregate class
Figure 8: Composition Notation
Example: Indian Cricket team requires 12 players for a match. These Players can be moved to IPL matches.
Figure 9: Aggregation example
Example: Human class is constructed with different part classes namely Body, Mind and Heart. When human class is destroyed, Body, Mind and Heart object cease to exist.
Figure 10: Composition Example
9
Generalization
Inheritance is realized using generalization in Unified Modeling Language. There are two types
of represent generalization. It can be represented by the following two ways.
Figure 11: Generalization Notation
Enumeration
Notation for enumeration consists of two compartments. First compartment represents name
of the enumerator with stereotype <<Enumeration>> at the center of the compartment. The
second compartment holds the values.
Figure 12: Enumeration
How to construct a class diagram
Step 1. Identify the classes, name and define them, this helps you get a better picture
about “why they are part of a model”. Determine their type [Interface | Abstract |
Static]. Assign them stereotypes if required. If you are defining your own
10
stereotype remember them to name accordingly. Use the attribute and operator
syntax to name the attributes and operators for a class
Figure 13: Construct class - step 1
Step 2. Find the relationship between the classes. Name the association between classes,
multiplicity is must for association so don’t forget it. Add constraints if required.
Figure 14: Construct class - step 2
Step 3. Evaluate each association for possibility of aggregation or composition
Figure 15: Construct class - step 3
Step 4. Evaluate the classes for any possibility towards generalization