I am pretty new to UML, so I have some questions about Generalization and Realization. I am modeling the behavior of an electronic microcontroller and I need to generate C++ code from the UML description.
As far as I know, a class realizes an interface, that means it may provide the implementation of an interface. A generalization relationship may exist between two classes. In that case the derived class inherits all the members of the base class and gains access to public and protected members.
Here is my question (I am using Visual Paradigm as modeling tool).
Let us assume we have a module of a microcontroller, namely the Timer
. We have a set of operations we can perform, say initTimer()
, startTimer()
, stopTimer()
and so on. Actually these functions define a kind of API. We may have different classes of Timer
, say TimerA
, TimerB
, TimerC
that inherit (or implement?) all of the cited operations. The picture will make the scenario clearer, probably. [C] means classifier.
+----------------------------------+
| <<SW>> |
| <<Singleton>> |
+--------------| TimerA |
| +----------------------------------+
| | -instance : TimerA* = null [C] |
| | -instanceFlag : bool = false [C] |
| | -moduleAddress const = 0x0010 |
| +----------------------------------+
| | -TimerA() |
V | +getInstance() : TimerA* [C] |
+---------------+ +----------------------------------+
| <<SW>> |
| Timer |
+---------------+
| +initTimer() |
| +startTimer() |<-----------------------+
| +stopTimer() | |
+---------------+ +----------------------------------+
| <<SW>> |
| <<Singleton>> |
| TimerB |
+----------------------------------+
| -instance : TimerB* = null [C] |
| -instanceFlag : bool = false [C] |
| -moduleAddress const = 0x0020 |
+----------------------------------+
| -TimerB() |
| +getInstance() : TimerB* [C] |
+----------------------------------+
Visual Paradigm allows the user to put code iniside each function. I ask you which kind of relationship the arrows should be.
1) Generalization: Timer
class with a set of operations. Each operation has its code implementation. Two derived classes TimerA
and TimerB
with generalization link inheriting the operations of class Timer
.
2) Realization: Timer
is an interface (not a class as shown) and two realizing classes TimerA
and TimerB
. The critical point is the following. Although Timer is an interface and its operations should not contain implementaion details, VP allows to write implementation code for the three operations. During code generation, an interface C++ class Timer
is created: initTimer()
, startTimer()
and stopTimer()
are virtual members of Timer
with no code (as it should be). A C++ class TimerA
is generated and this inherits class Timer
members; in addition the three operations of Timer
are copied among the memebers of TimerA
with the code implementation that I wrote for the operations of the interface class. This happens for TimerB
as well.
In your opinion which of the two descriptions is better? Is it correct to write code implementation for the operations of an interface even if I know that, after code generation, will be transferred towards the realizing classes?