I want to create a media management tool, for which I try to draw an appropriate UML class diagram. The tool shall hold a collection of different media types (movies, music, etc.) which inherit from a basic medium class. Each medium has different attributes in addition (e.g. interpreter, director,...). I plan to fetch information about media online to complement missing attributes automatically.

Currently I plan to hand this functionality to an additional singleton class template OnlineSynchronisation, to differ in the kind of medium, to fetch from the proper online source the proper attributes.

enter image description here

Does this scheme make sense or is there a better approach? If there would only be one kind of medium, I would create OnlineSynchronisation only with static methods, since there is no need for individual instances. But the method updateMedium needs to be adapted to different sources (e.g. get attributes from imdb.com, if the medium is a movie).

EDIT The strategy pattern suggested in the comments by @CandiedOrange looks promising. In combination with the answer and comments by @Bart I will drop my intention of using the singleton pattern

  • 1
    What is your question? – Batavia Nov 26 '17 at 9:39
  • Forgot the last sentence due to question migration – Jannek S. Nov 26 '17 at 9:46
  • 3
    In most cases, the best approach to the singleton pattern is "don't use it." – Sjoerd Nov 26 '17 at 18:14
  • @Sjoerd, Only "In most cases"? I think you are being overly generous to the singleton pattern ;) – David Arno Nov 27 '17 at 13:45

No, your scheme does not make sense.

First of all, you present no argument why there can ever only be one OnlineSynchronization instance. That is what it means to make a class a Singleton. It is a serious error in the logic of the program if there are ever multiple instances of a Singleton.

Secondly, the arrows in your design don't match with the attributes and operations in your classes. According to the arrows, OnlineSynchronization holds and manages the Medium instances, but according to the attributes, this is the other way around (which is also inconsistent with the use of a Singleton).

The scheme that would make sense to me is close to what your attributes suggest:

  • The (abstract) Medium class holds (owns; composition) an OnlineSynchronization instance.
  • Singleton pattern is not used.
  • There is a hierarchy of SynchronizationSource classes for dealing with the APIs that the various sources provide.
  • Thanks for your input. I'm not used to UML yet, therefore the misdirection. About the singleton: For every subtype of medium is only one single online ressource responsible; should it not be avoided to have multiple instances for the same subtypes, which do not differ regarding their attributes? – Jannek S. Nov 26 '17 at 19:51
  • 3
    @JannekS. Your UML looks much more like the strategy pattern. It will be composed of one medium at a time but there may be many mediums constructed waiting to be used by it. Which might be fine. It's just not what the singleton pattern is about. We still don't understand your needs so we can't even say if you should use it. – candied_orange Nov 26 '17 at 20:25
  • @JannekS., there might currently be only one online provider of resource attributes, but are you willing to say that there never can be a competitor of IMDB that you also want to retrieve movie attributes from? Because that is the signal you are giving by using a Singleton. – Bart van Ingen Schenau Nov 27 '17 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.