UML generalization and implementation

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?

+5
source share
2 answers

In your opinion, which of the two descriptions is better?

In my opinion, option 3 shown in the figure below is better. Timer will be reused (conceptually final ) by a universal class, the instance is only configured by singleton shells and a dependency dependency is associated

enter image description here

VP allows you to write implementation code for three operations. During code generation ... Is it correct to write code implementation for interface operations ...?

I don’t know how to properly configure the Visual Paradigm code generator to create what you want, but although the C++ interface is just a class like any other , there is no special keyword for this concept, the UML interface is only for describing contract without an associated implementation. Some languages, such as C # or Java, have special interface keyword for this purpose, without a code rule hardcoded in the compiler.

Thus, although Visual Paradigm may possibly generate the code you want at the end, in terms of UML , modeling the interface with the code is incorrect


Make your choice. If you only need code that The does something, then go and hack it anyway, which works (as @ gilead-silvanas suggests). If you want to practice UML for use in your future projects in different languages, then do not.

There is also a time when the source generated code will move away from the source drawings and the code generator, and you will edit it manually, even if you use a tool, such as the Quantum Leaps Model for embedded systems .

I would review (regularly) if struggling with a design tool would exceed the benefits of code generation

+3
source

This is probably just a shorthand for VP to make things faster. As you said, when generating the code, it still clears these codes from the interface and places them in the implementing classes. I do not see anything wrong with this, because the generated code matters, which is correct in your case.

+2
source

All Articles