Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

Class Diagrams

lecture 30-34, chitkara university


Intro to Class Diagrams

 A class defines the methods and variables in an object, which


is a specific entity in a program or the unit of code
representing that entity
 In an object oriented application, classes have attributes
(member variables), operations (member functions) and
relationships with other classes

lecture 30-34, chitkara university


UML Class Diagrams: The Analysis Model

 Class diagram in the Unified Modeling Language (UML) is


a type of static structure diagram that describes the structure
of a system by showing the system's classes, their attributes,
and the relationships between the classes.
UML modeling elements in class diagrams
1. Classes and their structure and behavior
2. Association, aggregation, and inheritance relationships
3. Multiplicity and navigation indicators
4. Role names

lecture 30-34, chitkara university


The Class Icon

Class NAME

Attribute

operation()

lecture 30-34, chitkara university


Three compartments

 Top most compartment contains the name of the class


 Middle compartment contains a list of attributes (member
variables)
 The bottom compartment contains a list of operations
(member functions).

lecture 30-34, chitkara university


Example:- Classes (with attributes and
operations)

lecture 30-34, chitkara university


Example 2 Class

Class name

Data members

Instance methods

Class method Return types


Arguments
Data members, arguments and methods are specified by
visibility name : type

lecture 30-34, chitkara university


Attribute/ Operations

 An attribute is a property of a class. It describes a range


of values that the property may hold in objects of the class.

 An operation is something that a class can do, or that you (or


another class) can do to a class. Like an attribute name, an
operation’s name is written in lowercase letter

lecture 30-34, chitkara university


Instances

tariff_1974:TarifSchedule
zone2price = {
{‘1’, .20},
{‘2’, .40},
{‘3’, .60}}

 An instance represents a phenomenon.


 The name of an instance is underlined and can contain the
class of the instance.
 The attributes are represented with their values.
Class Visibility
Visibility - Visibility applies to attributes or
operations and specifies the extent to which
other classes can use a given class’s attributes or
operations. Three levels of visibility are possible
(last symbols are used in UML classes to
indicate different levels of visibility)

•public level - usability extends to other


classes ( + )

•protected level - usability is open only to


classes that inherit from original class
( #)

•private level - only the original class can


use the attribute or operation ( - )
lecture 30-34, chitkara university
Class Multiplicity
 A multiplicity in a class specifies the
number of instances (objects) of that
class that can exist simultaneously

 Example:- Only one Library object is


allowed in the system (referred to as a
singleton object).

 Default multiplicity is 0 or more (if


not specified)

lecture 30-34, chitkara university


Objects: Instances of Classes

lecture 30-34, chitkara university


Figures (a)-(d) show four possible
representations of objects
 Top –
1. objectName:ClassName – underlined
2. :ClassName – underlined

 Bottom –
Attribute names and values.

lecture 30-34, chitkara university


Class Relationships

Classes can related to each other through different relationships

 Association (delegation)
 Generalization (inheritance)
 Realization (interfaces)
 Dependency

lecture 30-34, chitkara university


Relationships

lecture 30-34, chitkara university


Association
 Association describes a link, a link being a connection
among objects between classes.

 Association is shown by a solid line between classes.

lecture 30-34, chitkara university


Association - Example
 A Person works for a Company

lecture 30-34, chitkara university


Association - Properties

 Name:-Name of the association(“works for” Is the name of the


relationship)
 Role :- The specific role of the association(Person plays the role employee
and the Company plays the role employer.)
 Multiplicity:-Indicates the number of objects that are connected(Many
employees to one company.)
 Type:-Plain association, aggregation, composition(This shows a plain
association)

lecture 30-34, chitkara university


Association - Multiplicity

 Multiplicity on association specify properties of the number


of links that can exist between instances (objects) of the
associated classes.

 That is, it indicates how many objects of one class relate to


one object of another class. It is indicated by a single number
or a range of numbers.

 We can add multiplicity on either end of class relationship by


simply indicating it next to the class where the relationship
enters.
lecture 30-34, chitkara university
Example:-Association - Multiplicity

 A Student can take up to five Courses.


 Student has to be enrolled in at least one course.
 Up to 300 students can enroll in a course
 A class should have at least 10 students

lecture 30-34, chitkara university


Example 2 Association - Multiplicity
 A teacher teaches 1 to 3 courses (subjects)
 Each course is taught by only one teacher
 A student can take between 1 to 5 courses
 A course can have 10 to 300 students

lecture 30-34, chitkara university


Multiplicity Indicators

Exactly one 1

Zero or more (unlimited) * (0..*)

One or more 1..*

Zero or one (optional association) 0..1

Specified range 2..4

Multiple, disjoint ranges 2, 4..6, 8

lecture 30-34, chitkara university


Aggregation
 A special form of association that models a whole-part
relationship between an aggregate (the whole) and its parts.

lecture 30-34, chitkara university


Composition
 A strong form of aggregation
› The whole is the sole owner of its part.
 The part object may belong to only one whole
› Multiplicity on the whole side must be zero or one.
› The life time of the part is dependent upon the whole.
 The composite must manage the creation and destruction of its
parts.

1 Circle
Circle Point

3..* Point
Polygon

Intro | Classes | Relations | Generalization


lecture 30-34,| chitkara
Objects | Guidelines
university
Composition vs. Aggregation
Aggregation Composition

Part can be shared by several Part is always a part of a


wholes single whole
0..4 * *
category document Window Frame

Parts can live independently (i.e., Parts exist only as part of the
whole cardinality can be 0..*) whole. When the wall is
destroyed, they are
destroyed
Whole is not solely responsible Whole is responsible and
for the object should create/destroy the
objects
Generalization
 Indicates that objects of the specialized class (subclass)
are substitutable for objects of the generalized class
(super-class).
› “is kind of” relationship.

An abstract Shape Super


{abstract} is a tagged class {abstract} Class
value that indicates that the
class is abstract.
The name of an abstract Generalization
relationship
class should be italicized
Sub
Circle Class

lecture 30-34, chitkara university


Generalization
 A sub-class inherits from its super-class
› Attributes
› Operations
› Relationships
 A sub-class may
› Add attributes and operations
› Add relationships
› Refine (override) inherited operations
 A generalization relationship may not be used to model
interface implementation.

lecture 30-34, chitkara university


Dependency

 Notated by a dotted line


 The most general relation between classes
 Indicates that an object affects another object
Accounting
SecurityControl
System

<<creates>> <<modifies>>
AccountingSystem creates
a Receipt object
Reciept Order

lecture 30-34, chitkara university


Constraint Rules and Notes
 Constrains are simple properties of associations, classes
and many other things in UML
 Constraints are semantic restrictions noted as Boolean
expressions.
› UML offers many pre-defined constraints.

Customer 1 * may be
Order
{ total < $50 } canceled
id: long { value > 0 }

Constraint Note

lecture 30-34, chitkara university


Stereotypes, Profiles, and Tags

 A stereotype represents a refinement of an existing modeling concept and is defined within a


UML profile informally, a collection of related stereotypes, tags, and constraints to
specialize the use of the UML for a specific domain or platform, such as a UML profile for
project management or for data modeling.
 The stereotype declares a set of tags, using the attribute syntax. When an element (such as
the Square class) is marked with a stereotype, all the tags apply to the element, and can be
assigned values.

lecture 30-34, chitkara university


Method
 In the UML, the specific implementation or algorithm of
an operation for a class. Informally, the software
procedure that can be executed in response to a message.

lecture 30-34, chitkara university


Keywords

UML keyword is a textual adornment to categorize a model


element. For example, the keyword to categorize that a
classifier box is an interface is «interface».

lecture 30-34, chitkara university


Multiplicity

Customer
1 Simple
Class Aggregation

Abstract Rental Invoice


Class

Rental Item 1..*


1 0..1

Composition Simple
Generalization Association

Checkout Screen
DVD Movie VHS Movie Video Game

lecture 30-34, chitkara university


UML Class Diagrams: Detailed Design
Digital Sound Recorder Case Study
Define the detailed design of each subsystem/component

lecture 30-34, chitkara university

You might also like