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

Appendix A Small UML Guide

Structural Modeling

This section introduces the UML notations used to model structures, i.e. the building blocks of a software system, their interfaces, and their relationships (dependencies). Among the most important structural diagrams in UML are the component diagram and the class diagram.

Component

The UML element component represents a modular part of a system that is usually on a high abstraction level, for example at the level of software architecture. A component serves as a kind of “capsule” or “envelope” for a set of smaller components or classes that together fulfill certain functionality.

COMPONENT

A component represents a modular part of a system that encapsulates its contents and whose manifestation is replaceable within its environment.

The notation (syntax) of a component is a rectangular symbol with the component’s name, as depicted in Figure A-1. Above the component’s name (Billing), the keyword «component» appears within French quotation marks, which are also called guillemets. The icon in the upper-right corner is optional.

Figure A-1.  An example of the notation of a billing component

Due to the fact that a component encapsulates its content, its services are defined in terms of so-called provided and required interfaces. Only these interfaces are available to the environment for the use of a component. That means that a component is a substitutable unit that can be replaced at design time or at runtime by another component that has compatible interfaces and offers equivalent functionality.

452

Appendix A SMALLmall UML GUIDEuide

Class andObject

Among various other applications, class diagrams are typically used to depict structures of an object-oriented software design. Class diagrams are at a lower level of abstraction than the previously discussed component diagrams. The central element in class diagrams is the class.

CLASS

A class describes a set of objects that share the same specifications of features, constraints, and semantics.

The notation (symbol) for a class is a simple rectangle with the name of the class, as depicted in Figure A-2.

Figure A-2.  A class named Customer

An instance of a class is commonly referred to as an object. Therefore, classes can be considered as blueprints for similar objects. If such objects are presented in a UML diagram, they are called instance specifications.2 The notation of an instance specification is very similar to that of a class, with the difference that its name is underlined. Figure A-3 depicts three instance specifications (“peter”, “mary”, and “sheila”) that were created by instantiating the same class.

2A linguistic subtlety: The background for this very special term “instance specification” is that the graphical representation of an instance (object) on a UML diagram is not the real object at all, it is just a specification of it in a model. The real object can be found in the memory of the running software system.

453

Appendix A Small UML Guide

Figure A-3.  Three instance specifications created from the Customer class

Usually, classes have both structural and behavioral features. These are called attributes and operations.3 Attributes are usually shown in the second compartment of the class and operations in the third, as depicted in Figure A-4.

Figure A-4.  The Customer class with attributes and operations

3In C++, the attributes of a class are sometimes called “members,” and the operations are sometimes called “methods” or “member functions,” whereby the last term is properly speaking not quite correct, because they are normally not true pure functions.

454

Appendix A Small UML Guide

The type of an attribute is noted separated by a colon after the attribute’s name. The same applies to the type of the return value of an operation. Operations can have parameters that are specified within parentheses. If an operation has more than one parameter, the parameters are noted as a comma-separated list. Static attributes or operations are underlined.

Classes have a mechanism to regulate the access to their attributes and operations. In UML they are called visibilities. The visibility kind is placed in front of the attribute’s or operation’s name and may be one of the characters described in Table A-1.

Table A-1.  Visibilities

Character Visibility Kind

+public: This attribute or operation is visible to all elements that can access the class.

#protected: This attribute or operation is not only visible inside the class itself, but also visible to elements that are derived from the class that owns it (see the section entitled “Generalization”).

~package: This attribute or operation is visible to elements that are in the same package as its owning class. This kind of visibility cannot be realized in a language like C++ and is therefore not used in this book.

-private: This attribute or operation is only visible inside the class, nowhere else.

A C++ class definition corresponding to the UML class shown in Figure A-4 may look like Listing A-1.

Listing A-1.  The Customer Class Implemented in C++

#include <string>

#include <string_view> #include "Address.h"

#include "UniqueIdentifierFactory.h"

class Customer { public:

Customer() = delete;

Customer(const UniqueIdentifierFactory& idProvider, std::string_view name, const bool isRegular);

virtual ~Customer() = default;

455

Appendix A Small UML Guide

void setShippingAddress(const Address& address); void setBillingAddress(const Address& address); Address getShippingAddress() const;

Address getBillingAddress() const; bool isRegular() const;

GUID getIdentifier() const;

std::string getIdentifierAsString() const;

private:

void requestUniqueIdentifier(const UniqueIdentifierFactory& identifierFactory);

GUID identifier; std::string name; Address billingAddress; Address shippingAddress; bool isRegular;

};

Note  Due to the fact that a UML model is an abstraction in most cases (exception: when their purpose is to generate source code), diagrams often do not depict all existing properties of a described element, i.e., diagrams usually have a lower level of detail.

If a class is abstract, that is, it cannot be instantiated due to an incomplete specification, its name is typically shown in italicized letters, as depicted in Figure A-5. Abstract classes serve as base classes in inheritance hierarchies (see the section entitled “Generalization”).

Figure A-5.  An abstract class called Shape

456