Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
roth_stephan_clean_c20_sustainable_software_development_patt-1.pdf
Скачиваний:
25
Добавлен:
27.03.2023
Размер:
7.26 Mб
Скачать

Appendix A Small UML Guide

Interface

An interface defines a kind of a contract. A component or class that realizes the interface must fulfill that contract. Another component or class that uses the interface expects that it is supplied with an element that fulfills the contract.

INTERFACE

An interface is a declaration of a set of coherent public obligations.

Interfaces are always abstract, that is, they cannot be instantiated by default. The UML symbol for an interface is very similar to a class, with the keyword «interface» above its name, as depicted in Figure A-6.

Figure A-6.  The Displayable interface with two declared operations

To express, for example, that a class realizes (synonym: implements) an interface, a special relationship exists in UML. The realization relationship is a dashed arrow with a closed but not filled arrowhead. This relationship points, as depicted in Figure A-7, from the class (the realizing element) to the interface (the specification).

457

Appendix A Small UML Guide

Figure A-7.  The Shape class realizes the Displayable interface

It is, of course, allowed that a class implements multiple interfaces.

Unlike some other object-oriented languages, such as Java and C#, there is no interface keyword in C++. Interfaces are therefore usually emulated with the help of abstract classes that solely consist of pure virtual member functions, as shown in Listing A-2.

Listing A-2.    The Displayable Interface in C++

class Displayable { public:

virtual ~Displayable() = default; virtual void show() = 0;

virtual void hide() = 0;

};

To show that a class or component provides or requires interfaces, you can use the so-called ball-and-socket notation. A provided interface is depicted using a ball (a “lollipop”), whereas a required interface is depicted with a socket (a symbol that looks like a claw). Strictly speaking, this is an alternative notation, as Figure A-8 clarifies. (The association relationship that appears in this figure between Account and Owner is explained in detail in the following section.)

458

Appendix A Small UML Guide

Figure A-8.  The so-called “ball-and-socket-notation” for provided (AccountService) and required (Owner) interfaces

Association

Classes usually have static relationships to other classes. The UML association specifies such a relationship.

ASSOCIATION

An association relationship allows one instance of a classifier (e.g., a class or a component) to access another.

In its simplest form, the UML syntax for an association is a solid line between two classes, as depicted in Figure A-9.

459

Appendix A Small UML Guide

Figure A-9.  A simple association relationship between two classes

This simple association is often not sufficient to properly specify the relationship between both classes. For instance, the navigation direction across such a simple association, that is, who is able to access whom, is undefined by default. However, navigability in this case is often interpreted as bidirectional by convention, that is, Customer (whose internals have been completely hidden here) has an attribute to access ShoppingCart and vice versa. Therefore, more information can be provided to an association. Figure A-10 illustrates a few of the possibilities.

Figure A-10.  Some examples of associations between classes

\ 1.\ This example shows an association with one end navigable (depicted by an arrowhead) and the other having unspecified navigability. The semantic is: class A is able to navigate to class B. In the other direction it is unspecified, that is, class B might be able to navigate to class A.

460

Appendix A Small UML Guide

Note  It is strongly recommended to define the interpretation of the navigability of such an unspecified association end in your project. My recommendation is to consider them as non-navigable. This interpretation is also used in this book.

\2.\ This navigable association has a name (“has”). The solid triangle indicates the direction of reading. Apart from that, the semantics of this association is fully identical to Example 1.

\3.\ In this example, both association ends have labels (names) and multiplicities. The labels are typically used to specify the roles of the classes in an association.

A multiplicity specifies the allowed quantity of instances of the classes that are involved in an association. It is an inclusive interval of non-negative integers beginning with a lower bound and ending with an (possibly infinite) upper bound. In this case, any A has zero to any number of Bs, whereas any B has exactly one A. Table A-2 shows some examples of valid multiplicities.

\4.\ This is a special association called aggregation. It represents a whole-part-­relationship; that is, the one class (the part) is hierarchically subordinated to the other class (the whole). The hollow diamond is just a marker in this kind of association and identifies the whole. Otherwise, everything that applies to associations applies to an aggregation.

\5.\ This is a composite aggregation, which is a strong form of aggregation. It expresses that the whole is the owner of the parts, and thus also responsible for the parts. If an instance of the whole is deleted, all of its part instances are normally deleted with it.

Note  Note that a part can (where allowed) be removed from a composite before the whole is deleted, and thus not be deleted as part of the whole. This can be made possible by a multiplicity of 0..1 at the association end that is connected to the whole, that is, the end with the filled diamond. The only allowed multiplicities at this end are 1 or 0..1; all other multiplicities are prohibited.

461