I am new to UML, so I have some questions about generalization and implementation. I simulate the behavior of an electronic microcontroller, and I need to generate C ++ code from the UML description.
As far as I know, a class implements an interface, which means that it can provide an implementation of the interface. Between the two classes there may be a connection of generalization . In this case, the derived class inherits all members of the base class and gains access to public and protected members.
Here is my question (I am using Visual Paradigm as a modeling tool). Suppose we have a microcontroller module, namely Timer . We have a set of operations that we can perform, for example, initTimer() , startTimer() , stopTimer() , etc. In fact, these functions define a kind of API. We can have different Timer classes, say TimerA , TimerB , TimerC , which inherit (or implement?) All of these operations. Perhaps the picture will make the script clearer. [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 enter code into each function. I ask you what kind of relationship should be arrows.
1) Generalization : Timer class with a set of operations. Each operation has its own code implementation. The two derived classes TimerA and TimerB using the TimerB link that inherits the operations of the Timer class.
2) Implementation : Timer is an interface (not a class, as shown), and two implementing classes are TimerA and TimerB . The critical point is as follows. Although Timer is an interface, and its operations should not contain implementation details, VP allows you to write implementation code for three operations. During code generation, the C ++ class Timer interface is created: initTimer() , startTimer() and stopTimer() are virtual Timer members with no code (as it should be). The C ++ class TimerA , which inherits the members of the Timer class; in addition, the three Timer operations are copied among memebers TimerA with a code implementation that I wrote for interface class operations. This also happens for TimerB .
In your opinion, which of the two descriptions is better? Is it correct to write a code implementation for interface operations, even if I know that after the generation of the code they will be passed to the implementing classes?