Pergunta

I have an object which is instantiated during compilation according to the build configuration. As far as the surrounding software considered, the object exposes the same interface. I would like to model the fact that the instantiation decision is taken during compilation (i.e. static polymorphism), as opposed to the usual dynamic polymorphism.

Is there a way to depict a static polymorphism in UML class diagram?

Here is more or less what I need:

enter image description here

Obviously, only one of the above type definitions will be instantiated at compilation.

Foi útil?

Solução 4

Combining from @ChiragDesai and @user2004268 answers and linked question (Static polymorphism definition and implementation):

  1. Polymorphism type is an implementation detail and as such it doesn't have active role in design diagrams.
  2. Implementation details can be present in UML diagram but have a complementary and informal role. Stereotypes and notes can be used to clarify the intentions.

Outras dicas

I think the UML representation will be same for static and dynamic polymorphism. UML is about how classes interact at runtime -- I don't believe there's a UML format for describing templates, but I could be wrong.

I would use stereotypes to solve the problem. So you can mark dynamic and static

I think your diagram is fine. What you are describing seems to best described as a sequence diagram describing your compilation process. (Kind of like how you'd draw a factory sequence diagram I guess)

As you correctly point out, interactions at runtime occur with some unknown concrete thing behind the interface, so you never really have to bother about concrete classes in those sequence or interaction diagrams. It's completely irrelevant there.

If there is a good deal of this happening then a deployment diagram might also be a good idea to help show what concrete implementations occur under what circumstances.

You'd want to document classes that implement your interfaces of course, and that's just a normal class diagram exactly as you've drawn it.

Use an empty class utility with stereotype Singleton with a generic boolean parameter named e.g. #ifdef(YOUR_FLAG), whose true specialization has the instance as a static member with public or implementation visibility.

EDITED (in reply to a comment)

Draw this in your UML tool:

Pseudo-C++-code:

class Foo; 

template <
   Boolean #ifdef(WHATEVER)
> struct Bar {};

template <> 
struct Bar<true> {
  public: 
    static Foo the_foo;
};

and add utility and Singleton stereotypes (but DON'T try to generate code from that ;) )

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top