UML generalization and realization

11,293

Solution 1

In your opinion which of the two descriptions is better?

In my opinion option 3 sketched in the picture below is better. Timer would be reusable (conceptually final) universal class, it's instance only configured by the singleton wrappers and linked by usage dependency relationship

enter image description here

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

I don't know how to correctly configure Visual Paradigm's code generator to produce what you want, but although C++ interface is just a class like any other and there's no special keyword for this concept, UML interface is meant to be only description of a contract with no coupled implementation. Some languages like C# or Java have special interface keyword for this purpose, with no-code rule hardcoded in the compiler.

Thus, although Visual Paradigm can perhaps generate the code which you want at the end, from UML perspective, modeling an interface with code is wrong


Make your choice. If you want just code that does The 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 using different languages, then don't.

Also there's a time when the initial generated code will depart from the initial design drawings and from the code generator and you'll edit it by hand, even if you'd use a tool like Quantum Leaps Modeler for embedded systems.

I'd reconsider (regularly) if fighting with the design tool over-weights the code generation benefits

Solution 2

Probably this is just a shorthand for VP to make things quicker. As you said, upon code generation, it clears those codes anyway from the interface and put it in the realizing classes. I don't see anything wrong with that because what matters is the generated code, which in your case is correct.

Share:
11,293
Umberto D.
Author by

Umberto D.

Updated on July 26, 2022

Comments

  • Umberto D.
    Umberto D. almost 2 years

    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?