Class diagram

advertisement
Lection 2
Class diagram
Plan:
1. Class: name, attributes, operations.
2. Relationships between classes.
3. Interfaces.
4. Objects.
5. Templates.
6. Recommendations for the development of class diagram
Question 1.
Class diagrams are the most common diagram found in modeling object- oriented
systems. A class diagram shows a set of classes, interfaces, and collaborations and their
relationships.
You use class diagrams to model the static design view of a system. For the most
part, this involves modeling the vocabulary of the system, modeling collaborations, or
modeling schemas. Class diagrams are also the foundation for a couple of related
diagrams: component diagrams and deployment diagrams.
Class diagrams are important not only for visualizing, specifying, and documenting
structural models, but also for constructing executable systems through forward and
reverse engineering.
Class
Class (class) in UML is used to refer a set of objects which have the same structure,
behavior, and relationships with objects of other classes. Graphically, the class is depicted
in the form of a rectangle, which also can be divided by horizontal lines on the sections
(Fig. 1). In these sections there can be specified class name, attributes (variables) and
operations (methods).
1
Fig. 1.
Compulsory elements of the designation of the class are its name. In the initial
stages of development of the diagrams the individual classes can be marked with a simple
rectangle indicating only the name of the class (Fig. 1, a). Then the individual
components of diagrams describing classes are supplemented with attributes (Fig. 1, b)
and operations (Fig. 1, C).
Even if the section of the attributes and operations is empty, it is a horizontal line,
to immediately distinguish the class from the other elements of UML. Examples of
graphic image of the class in the class diagram shown in Fig. 2. In the first case, for a
class Rectangle (Fig. 2, a) are there are only its attributes - point in the coordinate plane,
which determine its location. For class «Window» (Fig. 2, b) there are the only its
operation, the section attribute is left empty. For class «Account» (Fig. 2, b) it is depicted
the fourth optional section, in which the specified exception (collision) is described (the
failure because of the overdue of credit card).
Fig. 2.
2
The name of the class
The class name must be unique within a package, which describes a collection of
the diagrams of classes (perhaps one diagram). It is recorded in the first top section of the
rectangle. The class name should start with a capital letter. It is recommended that the
names of the classes use nouns, recorded without gaps. It is necessary to remember, that
the names of the classes form a dictionary of the subject field (Glossary).
In the first section there can be references on the standard templates or abstract
classes, from which this class was formed and, respectively, from which it inherits
properties and methods. In this section there may be some information about the
developer of this class and status of development, and may also be recorded, and other
common properties of this class, relevant to other classes of diagrams.
Examples of classes’ names can be such nouns as «Employee», «Company»,
«Manager», «Client», «Seller», «Manager», «Office» and many others directly related to
a model of a subject domain and functional purpose of the projected system.
The class may not have instances or objects. In this case it is called an abstract
class.
Class attributes
In the second section of the rectangle there are recorded its attributes or properties.
Every attribute of the class corresponds to a separate line of text, which consists of
quantum of visibility of attribute, the attribute name, its frequency, type of attribute
values and its initial value:
<visibility
quantor><attribute-name>[multiplicity]:<attribute
type>=<the
original
value>{line-property}
Quantor of visibility can take one of three possible values and, accordingly, is
displayed using special characters:
• The "+" symbol indicates the attribute with the public type. An attribute with this
tupe is available or visible from any other class of the package, which defined in the
diagram.
• The "#" symbol indicates an attribute with the protected type. The attribute with
this area is not available; it is invisible for all classes except subclasses of this class.
3
• And, finally, the sign "-" denotes an attribute with the closed type (private). The
attribute is not available or invisible for all classes without exception.
Quantor of visibility can be omitted. In this case, it simply means that the visibility
attribute is not specified. This situation is different from the default agreements in
traditional programming languages, when the absence of quantum visibility is treated as
public or private.
The name of the attribute is a string of text. The name of the attribute is the only
required element in syntax notation of an attribute.
The multiplicity of the attribute describes the total number of specific attributes of
the type included in a separate class.
As an example, let us consider the following variants of setting multiplicity of
attributes.
[0..1] means that the multiplicity of an attribute can have a value of 0 or 1. While 0
means the absence of values for a given attribute.
[0..*] means that the multiplicity of the attribute may be any positive integer
greater than or equal to 0. This multiplicity can be expressed in a short form by simple
character - [*].
[1..*] means that the multiplicity of the attribute may be any positive integer
greater than or equal to 1.
[1..5] means that the multiplicity of the attribute can take any value from the
numbers: 1, 2, 3, 4, 5.
[1..3, 5, 7] means that the ratio of the attribute can take any value from the
numbers: 1, 2, 3, 5, 7.
[1..3, 7.. 10] means that the ratio of the attribute can take any value from the
numbers: 1, 2, 3, 7, 8, 9, 10.
[1..3,7..*] means that the multiplicity of the attribute can take any value from the
numbers: 1, 2, 3, as well as any positive integer value greater than or equal to 7.
If the multiplicity of the attribute is not specified, the default value is its value
equal to 1..1, i.e. in the accuracy of 1.
4
The attribute type is sometimes defined depending on the programming language
you want to use for realization of this model. In the simplest case, the attribute type
specifies a string of text.
We can look at the following examples of specifying the names of the attribute
types and classes:
• color: Соlоr - here «color» is the attribute name, “Color” is the name of the type
of the attribute.
• employee_name [1..2] : String - here employee_name is the name of the attribute.
The attribute type String points to the fact that the name can consist of one or two words
(for example, «Kirill» or «Dmitry Ivanovich»).
• visibility:Boolean. Here «visibility» is the name of the attribute, which may
indicate the presence of a visual representation of the relevant class on the monitor
screen. In this case, the Boolean type means that the possible values of this attribute is
one of the two Boolean values: true or false. The meaning of truth may correspond to the
presence of a graphical image on the monitor screen, and the value false is its absence.
Because of the multiplicity of the attribute visibility is not specified, it is set to 1 by
default.
• form:Polygon - here the name of the attribute «form» can characterize this class,
which is a geometric figure on the plane. In this case, the attribute type Polygon points to
the fact that a separate geometric shape can have the form of a triangle, rectangle, a
rhombus, pentagon and any other polygon, but not of a circle or an ellipse.
The initial value is used to specify a starting value for the attribute at the time of
the creation of a separate instance of the class.
As the examples of the initial values of the attributes there are the following
attributes:
• color:Соlоr = (255, 0, 0) in the RGB color model that corresponds to the pure red
color as the initial values for a given attribute.
• employee_name[1..2]:String = Ivan Ivanovich.
• visibility:Вооlеаn = true.
• form:Polygon = rectangle.
5
• visibility:Вооlеаn = truth.
Operation
In the third section of the rectangle there are the operations or class methods.
Operation is a service that provides each instance of a class on a particular request. Set of
operations describes the functional aspect of the behavior of the class.
<quantor of visibility><operation name>(list of parameters):<expression of type of the
return value>{line-property}
Quantor of visibility, as in the case of class attributes, can take one of three
possible values"+", "#", "-".
The name of the operation is a string of text. The name of the attribute is the only
required element syntax indicate the operation.
The parameter list is a list of comma-separated formal parameters, each of which
can be represented in the following form:
<type
of
parameter><parameter-name>:<statement-type>=<default
value
of
this
parameter>.
Here the type of parameter is one of the key words “in”, “out”, or “inout” with the
value in the default, if the type parameter is not specified.
The expression of the type of the return value is also dependent on the language
implementation. A colon and the expression of the type of the return value may be
omitted, if the operation does not return any value.
As examples of recording operations we can cause the following symbols of
separate operations:
• +create() - can mean the operation, which is public and contains no formal
parameters. This operation does not return any value after his execution.
• +draw (form: Polygon = rectangle, fill_color Color: = (0, 0, 255)) - can mean the
operation on the drawing on the screen of the monitor a rectangular area of blue color,
and if you do not specify other values as arguments to this operation.
• ask_client’s_account (number_of_account:Integer):Сиггепсу - denotes the
operator of establishing the availability of funds on the current account of the client of the
Bank. The argument of this operation is the number of the account of the client, which is
6
written in the form of a number (for example, «123456»). The result of this operation is a
number that is recorded in the currency format (for example, $1,500 .00).
• show_message():{"division by zero Error"}. This message may appear on the
screen of the monitor in the case of the attempt to divide a number by zero, which is
inadmissible.
Question 2.
Variety of relationships between classes can be indicated in class diagram.
The basic relations for the class diagrams are:
• dependency relationship
• association relationship
• generalization relationship
• realization relationship
Dependency
The relationship of dependence graphically depicted by the dotted line between the
relevant elements with the arrow on one of its ends («->» and «<-»). In class diagram this
relationship binds the different classes, the arrow points from the class-client dependency
to independent class or a class-source (Fig. 3). At this figure two classes are shown:
Class_А and Class_B. Class_B is a source of dependence, and Class_А is a client of this
dependence.
Fig. 3. Dependency relationship
As a class-client and a class-source of dependencies it can be a set of elements of
the model. For example, if Class_С depends on the peculiarities of the implementation of
Class_А and Class_B, then the relationship can be depicted as follows (Fig. 4).
7
Fig. 4.
An arrow may be marked optional standard keyword in quotation marks and
optional individual name. Examples of keywords:
• «access» is used to show that all open attributes and operations of a class-source
are available for class-clients;
• «bind» - class-client can use a template;
• «derive» - the attributes of a class-client can be computed by the attributes of a
class-source;
• «import» - open attributes and operations of a class-source become a part of a
class-client, as if they were declared directly in it.
• «refine» - indicates that the class-client serves as a specification of a class-source
for the reasons of a historical nature, when there is more information in the course of the
project.
Association
Association corresponds to the presence of a relationship between classes. This
relationship is indicated by a solid line with additional special characters, which
characterize the individual properties of an association. As an additional special
characters we can use the name of association, the names and the multiplicity of a classroles of in association. The name of association is an optional element. If it is set, it is
written with a capital letter.
The direction of the arrow indicates the order of the classes, one of which is the
first (from the side of the triangle), and the other - the second (from the side of the top of
8
the triangle). The absence of the arrow next to the name of association means that the
order of the classes is not defined.
As a simple example of binary association let us consider the relation between two
classes «Company» and «Employee» (Fig. 5). They are connected by a binary association
«Work», the name of which is indicated on the figure near the line of the Association. For
the relations it is determined the order of the classes, the first of which is a class
«Employee», and the second is a class «Company».
Fig. 5.
The ternary association and association of the higher number are called Nassociation in general case. Such association binds 3 or more classes, and one class can be
related with other more than once.
N-association is graphically indicated by a diamond, from which solid lines to
classes go. The name of N-association is written next to the diamond.
The order of the classes in such an association is not fixed.
As an example of a ternary association let us consider the relation between the
three classes: « Football team», «Year» and « Game» (Fig. 6).
Fig. 6.
We see at these examples that there is the multiplicity of separate classes. For the
previously discussed example (Fig. 5) the multiplicity of "1" for the class «Company»
means that each employee can work in only one company. The multiplicity of «1..*» for a
9
class of «Employee» means that every company can work a number of employees, the
total number of which is not known and is not restricted.
A particular case of association is excluding association (Xor-association). It points
that in each moment of time it can be used only one association of several possible
associations. In a class diagram the excluding association is represented by a dotted line
that connects two or more associations with the keyword «{xor}».
For example, a bank account can be opened for the client, as a physical person or as
a company (Fig. 7).
На диаграмме классов исключающая ассоциация изображается пунктирной
линией, соединяющей две и более ассоциации, рядом с которой записывается
строка-ограничение «{хог}».
Fig. 7.
Aggregation
Aggregation is used when one of the classes is a certain essence, which includes as
components other entities used to represent a relationship of «part-whole». Aggregation
shows what components the system is made up and how they are interconnected. This
relationship describes the partitioning of a complex system into simple parts.
Aggregation is fundamentally different from the relation of generalization. The
difference lies in the fact that part of the system does not have to inherit its properties and
behavior, as are completely independent entities. Moreover, the parts of a whole have
their own attributes and operations, which are different from the attributes and operations
of a whole class.
For example, «Truck» consists of components such as «Engine», «Cabin»,
«Body».
10
Graphically the relation of aggregation is represented by a solid line, one end of
which is a non-filled diamond. This diamond indicates on a «whole» class. The rest
classes are the «parts» (Fig. 8).
Fig. 8.
Another example: the division of the personal computer into its constituent parts:
the system unit, monitor, keyboard and mouse (Fig. 9).
Fig. 9.
Composition
The relationship of the composition is a special case of the relations of aggregation.
This relationship serves to highlight the special form of relations «part-whole», in which
the parts are inside of a whole. With the destruction of the whole all its component parts
are destroyed too.
An example of this relationship is a living cell in biology. Graphically composition
is represented by a solid line, one end of which is marked by filled diamond. This
diamond is near that class, which is a «whole» class. The rest classes are the «parts» (Fig.
10).
Fig. 10.
Another example (Fig.11).
11
Fig. 11.
Generalization
Generalization is the relationship between the more common element (parent) and
more private element (child).
In a class diagram this relation describes the hierarchical structure of classes and
the inheritance of their properties and behaviour. A class-child has all the properties and
behavior of a class-parent, and has its own properties and behavior, which are absent
from class-parent. Generalization is indicated by the solid line with a triangular arrow on
one end (Fig. 12). The arrow points to the more general class (class-parent or superclass).
Fig. 12.
For example, the class Geometric_shape is a superclass to a subclasses Rectangle,
Circle, Ellipse, etc. (Fig. 13).
Fig. 13.
To simplify the diagram all lines may be combined in one line (Fig. 14).
12
Fig. 14.
We can write some property near the arrow, which applies to all subclasses. The
text should be seen as a limitation, it is recorded in curly braces:
• {complete} - means that in generalization all classes-children are indicated and
there can’t be other children. For example, the class Bank_client is the parent for two
classes: Person and Company, and there isn’t exist any other classes-children.
• {disjoint} - means that the classes-parents cannot contain objects, which at the
same time belong to several classes-children. For the example above, this condition is
also true, because no concrete physical person can be simultaneously a particular
company.
• {incomplete} - the diagram shows not all the classes-children. An example is a
class diagram «Car» with indication of models of cars.
• {overlapping} - means that some copies of the classes-children may belong at the
same time to different classes. For example, a class «Polygon» is a class-parent to classes
«Rectangle» and «Rhombus». However, there is a class of «Square», copies of which are
at the same time the objects of the first two classes.
Fig. 15.
Question 3.
Interfaces.
13
Interfaces are the elements of use-case diagrams. However, when making a class
diagram the individual interfaces can be specified, then we use a rectangle with the
keyword «interface» (Fig.16). In this section the attributes of the rectangle is not
available, and only section of the operations should be indicated.
Fig. 16.
Question 4.
The object is a separate instance of a class that is created on the stage of the
program execution. It has its own name and specific attribute values.
For the graphic image of the object it is used as a rectangle, as for classes. The
differences is in the indication of the names of objects, which are necessarily highlighted
(Fig. 17). Object name is a string of text «object name:class name», separated by a colon
(Fig. 17 a,c). The name of the object may be absent, in this case, it is assumed that the
object is anonymous (Fig. 17, b). The name of the class may be absent. Then we specify
just the name of the object (Fig. 17, d).
When the image of a chart objects need to remember that all of the connections are
depicted by solid lines.
Fig. 17.
14
Question 5.
Template or a parametrized class is used to denote a class, which has one (or more)
of fixed formal parameter. Usually templates’ parameters are the types of the attributes of
the classes, such as integers, an array of strings, etc.
Graphically, the template is depicted as a rectangle with a small rectangle of dotted
lines in the upper right corner (Fig. 18). There is the list of formal parameters for those
classes, which can be obtained on the basis of this template.
Fig. 18. Графическое изображение шаблона на диаграмме классов
A template can not be used as a class. A template is usually a superclass, the
parameters of which are being established in his classes-children. Obviously, in this case,
there is a dependency relationship with the keyword «bind» between them. In a more
private event it can be a generalization of the inheritance of properties of the template
(Fig. 19). In this example, the class «Address» can be obtained from the template List on
the basis of its formal parameters «S, k, l» as attributes «street», «house», «apartment».
Fig. 19. Пример использования шаблона на диаграмме классов
15
Question 6.
When you create class diagrams in the UML, remember that every class diagram is
just a graphical presentation of the static design view of a system. No single class
diagram need capture everything about a system's design view. Collectively, all the class
diagrams of a system represent the system's complete static design view; individually,
each represents just one aspect.
A well-structured class diagram
 Is focused on communicating one aspect of a system's static design view.
 Contains only elements that are essential to understanding that aspect.
 Provides detail consistent with its level of abstraction, with only those
adornments that are essential to understanding.
 Is not so minimalist that it misinforms the reader about important semantics.
When you draw a class diagram,
 Give it a name that communicates its purpose.
 Lay out its elements to minimize lines that cross.
 Organize its elements spatially so that things that are semantically close are
laid out physically close.
 Use notes and color as visual cues to draw attention to important features of
your diagram.
 Try not to show too many kinds of relationships. In general, one kind of
relationship will tend to dominate each class diagram.
16
Download