2

I have some related interfaces and classes that I want to represent in UML (sorry about the relationships, I don't know how to do it properly with StarUML):

enter image description here

The idea of an interface ISMS implementing IMessage and IStorable, instead of having directly the SMS class implementing itself both interfaces, aims to make the project more modular, maintainable, and easier to test.

Is this a good approach for the design? If so, is this a good way of representing them in an UML Class Diagram or is there a better way to represent an interface and its relationship with other interfaces/classes in UML?

ehtio
  • 179
  • 3
  • 11
  • 1
    the relation to indicate a class implements an interface is a realization, using dotted line, you used a generalization. ` ISMS implementing IMessage and IStorable` this is not an implentation, same for IEmail – bruno Oct 07 '20 at 07:39

2 Answers2

2

Is this a good approach for the design?

I think yes, also because MMS if you add it can also implement ISMS too (may renaming that interface).

For IEmail it is less clear except that simplify Email and other classes working with interfaces to have one interface rather than two

I am pretty sure Christophe will say much more about that :-)


is this a good way of representing them in an UML Class Diagram or is there a better way to represent an interface and its relationship with other interfaces/classes in UML?

the relation to indicate a class implements an interface is a realization (drawn with dotted line), you used a generalization, so also adding MMS :

enter image description here

... ISMS implementing IMessage and IStorable

warning this is not an implementation because ISMS is an interface, same for IEmail, this is why between interfaces the inheritance is supported by a generalization rather than a realization.

bruno
  • 32,421
  • 7
  • 25
  • 37
  • 2
    Very clear! Difficult to add much after such an answer! Maybe you could add an extra sentence about the generalization between the interfaces: between class and interface it's cristal clear, but personally I always hesitate between the interfaces; is it generalization (as java syntax suggests) or is it a realization (since after all it's only about the requirement to implement by transitivity). – Christophe Oct 07 '20 at 08:52
  • 2
    Thanks for your answer. I think my problem was that I couldn't use realization using StarUML; for some reason, it wouldn't let me so I had to use generalization to put this example together. Thanks for pointing that out because that means there is something clearly wrong about it. I will revise it and see where I went wrong while doing the UML diagram. Regarding the approach, thank you again for letting me know. I feel a bit better knowing that I am slowly understanding all this a bit better. – ehtio Oct 07 '20 at 09:11
  • 1
    @Christophe between interfaces it is a generalization, the norm says `Realization is a specialized Abstraction relationship between two sets of model Elements, one representing a specification (the supplier) and the other represents an implementation of the latter (the client)`, the implementation must be done (directly) by the 'client', and in fact an interface does not know it is implemented or not, so that cannot affect the way it generalizes an other interface – bruno Oct 07 '20 at 10:13
  • 2
    @CarlosMira Maybe the time to change from StarUml to Bouml ;-) – Christophe Oct 07 '20 at 10:24
  • 1
    I guess so. I am using StarUML because it is the software we are using in our practices. But yes, I will look into Bouml. Thanks! @bruno, that is a great explanation. Thank you very much for that. I really understand that much better now. I wish I could choose yours as an accepted answer too. I feel that your comments are very, very helpful. – ehtio Oct 07 '20 at 11:57
2

I have a couple of remarks on top of Bruno's already very clear answer.

Your design

The decomposition of the interfaces into IStorable and IMessage seems at first sight to be a sound application of interface segregation principle.

Combining the two interfaces into a reusable ISMS interface instead of directly implementing them in a concrete SMS class will in this regard keep your code more maintainable, since it will easily allow to replace the SMS implementation with an alternative one (which makes sense if you consider that SMS functionality can be platform specific).

The question is however if SMS and email could not be used interchangeably. But only you can answer this question: If your design requires to keep those communication channels distinct (and maybe your real code adds some differences between the two interfaces), it's fine. But if not, it would make sense to allow such interchangeability and replace ISMS and IEmail with a single more general INotification.

Your UML representation

First of all, I'd like to reinforce Bruno's remark about the difference between generalization (plain line) and realization (dotted line).

Maybe I'm old school, but instead of using the circle for the interface, I'd advise for the more conventional interface as with a class box with the keyword «interface» above the name of the interface. Especially if you have properties and operations.

The circle is in my view better suitable for the lollipop notation of an interface. This is very practical when you have no much to say about the interface itself, but want to show what interfaces a class implements (lollipop) or is dependent on (socket). The interfaces details are then defined in another more detailed diagram. You can theoretically merge the two notations in the same diagram, but personally I find it less readable and would not advise it.

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • 1
    Thank you again @Christophe. Regarding SMS and Email, they won't be interchangeable. I just wanted to keep the example simple. And about the UML representation, as bruno said, I will change the link between classes and interfaces to show their relationship properly. I have Simon Bernett OO Systems Analysis and Design Using UML book, but I wasn't so sure about whether to use the lollipop notation or the class box. I think I prefer the class box too. It is more readable when there properties and operations. – ehtio Oct 07 '20 at 09:01