Search code examples
design-patternssingletonadaptersoftware-designclass-diagram

Singleton and Adapter patterns together


I'm designing a simple game application in Java for my OOP exam. The critical point seems to be the database. I've modeled a DBConnector class, which is supposed to manage all interactions with db. These are:

  1. provide a list of every song available (this game is kind of like OSU)
  2. download/upload of a new song
  3. provide a ranking of every game played

I've thought the Singleton pattern could be the case for DBConnector, since the entire app needs just one instance of this class. But doing so heavily violates the Single Responsibility Principle (SRP). In fact, this class could be modified for more than one reason, one for each function previously described [ 1), 2), 3) ].

I was wondering whether applying an adapter interface (thus using the Adapter pattern) between the Singleton class and other classes that use it to retrieve data could improve things. The SRP violation would remain, but at least those classes wouldn't be affected by any changes in the DBConnector code.


Solution

  • An Adapter is typically used to fill the gap between your code and someone else's. When you control both interfaces, you probably don't need an Adapter.

    That being said, you may be overestimating the constraints of the SRP here. Your DB layer will change when the data changes. That's only one reason for change. A single data change may affect multiple methods, but it's still only one reason.

    So to answer the question, no, I don't think an Adapter would add value in this case. A Singleton DBConnector is sufficient for an OOP exam.