OOPM- Class Diagrams

advertisement
OOPM – Sem III ( Comp/IT )
UML Class Diagrams
UML :
UML stands for Unified Modeling Language.
It is used for creating Models for Object Oriented Programming Software.
It has two parts: Meta Model and Notations.
Class Diagrams :
In planning stage of OOP Software one need to describe behavior of different
Objects. Since Objects are created from classes. Thus, one has to describe all the
classes involved and their relationships using some diagrams. These diagrams are
called as Class Diagrams.
Thus, class diagrams in UML is a ‘Static’ structure diagram that describes the
structure of a system, by showing system’s classes, their properties (Data
Members) ,their Operations (Methods) and relationship between Objects.
Class Icon : Class is shown by a rectangle with three
compartments : Topmost compartment mentions class
name, Second mentions the class Attributes and third
the Operations.
Visibility of class Members :
Visibility Mode ( Access Mode ) of member is mentioned with different symbols.
Visibility
Public
Private
Protected
Symbol
+
#
These symbols are written before name a member in class diagram.
E.g. assume a class Rectangle with private data members for Length and Breadth.
Also, assume the public methods as setDimensions() and getArea().
Rectangle
- Ln : Integer
- Br : Integer
+ setDimensions(Integer , Integer )
+ area() : Integer
1
OOPM – Sem III ( Comp/IT )
Class Relationships:
1. Composition :
E.g. Class A consists of object (instance) of B.
An arrow with diamond at the tail indicated Composition.
Diamond is on the class A which is composed of instance of B.
A
B
2. Inheritance :
E.g. B is a subclass ( i.e. derived class) of B.
Here an arrow with triangle at the arrowhead is drawn towards super (base) class.
Following example shows Rectangle and Triangle are sub-classes an Abstract class
Shape. In a class diagram, abstract class and its abstract methods are written in
Italic font.
Different class relationships are shown with following kind of arrows and lines.
2
OOPM – Sem III ( Comp/IT )
3. Aggregation / Association :
This is a weaker relationship. A class may contain one or more instances of other
class. E.g. ‘Window’ class may contain multiple instances of ‘Shape’ class.
The relation is shown by white diamond on ‘Whole’ class and arrow on ‘Part’ class.
In the above figure * sign indicates Window can consists of multiple Shape
instances.
4. Dependency :
This is weakest relation. The two classes are Not implemented as Member of other
class, but one class object may be a Method parameter of the other class.
Interfaces :
Interfaces have similar syntax and symbol as classes. Interfaces consist of abstract
method declarations (pure virtual functions). i.e. no executable instructions.
In UML diagram we mention <<type>> at the top as shown in following figure.
<<type>>
Shape
SetDimensions()
Area()
Lollipop notation is used to denote Interface.
e.g. Rectangle implements Shape will be shown as follows.
Shape
Rectangle
3
OOPM – Sem III ( Comp/IT )
Multiplicity :
Multiplicity in UML allows to specify number of elements in a collection of
elements. It’s also called as Cardinality.
It’s is specified by an interval of non-negative numbers called as Lower Bound and
Upper bound. Multiplicity is specified as :
Range = LowerBound .. UpperBound | *
If there are unlimited of numbers then UpperBound can be denoted by * symbol.
Some typical examples of multiplicity:
Multiplicity
Option
Cardinality
0..0
0
Collection must be empty
0..1
No instances or one instance
1..1
1
Exactly one instance
0..*
*
Zero or more instances
1..*
5..5
m..n
At least one instance
5
Exactly 5 instances
At least m but no more than n instances
4
Download