I want to create a class User
that will manipulate data on the user
table in my database. But I don't know if I should make it a Singleton or just bundle a bunch of static methods.
class UserSingleton {
private static instance;
private UserSingleton() {
// stablish connection and prepare statements
}
public static getInstance() {
if (instance == null) {
instance = new UserSingleton();
}
return instance;
}
public void create() {};
public void delete() {};
// and so on...
}
class UserStatic {
public static initialize() {
// Stablish connection and prepare statements.
// These properties would be static.
}
public static void create() {};
public static void delete() {};
// and so on...
}
I really don't know what would be the best approach here or the pros and cons of each of them. While the singleton class seems more elegant and cool, the static class API would be easier to use since I don't have to instantiate an object. Also it resembles me of mongoose, where such methods are static, e.g: Model.create()
, Model.findById()
, and so on...
What do you think? Or I should do it in a completely different manner? Some context about the app:
- It's a small/medium desktop app made with JavaFX
- The database is SQLite
- Tests aren't a priority (some people may point out that Singleton is bad because it makes testing harder)
Aucun commentaire:
Enregistrer un commentaire