What is the difference between all static methods and using a singleton pattern?

I create a database to store information about users of my site (I use stuts2 and, therefore, Java EE technology). For the database, I will create a DBManager. Should I apply a singleton pattern here, or rather make all its methods static?

I will use this DBManager for basic things like adding, removing and updating user profiles. Along with this I will use for all other requests, for example, to find out if a username already exists and that all users are for administrative purposes, etc.

My questions

  • What is the advantage of the singleton template?
  • Which thing is most appropriate here? All static methods or singleton pattern?
  • Please compare them both.

considers

shahensha

PS The database is larger. Here I am only talking about tables that I will use to store user information.

+7
source share
3 answers

Should I apply a singleton pattern here, or rather make all its methods static?

None of them. Just create one .

In a simple servletcontainer, you can use the ServletContextListener . At the start of Webapp, create it and place ServletContext#setAttribute() in the application area. It will be available to all servlets during the life of the webapp. For a basic startup example, you can find this article .

+8
source

I do not know the internal elements of EE, but, as a rule, the difference between a static class and Singleton is an instantiation - with a static class there is no actual instance, no element data, no object reference. So the practical difference ... not much.

I think the real benefit here is conceptual ... static methods and properties are methods that apply to the abstract concept of a class, and not to a specific instance. If you create a single-user user object here, who is this user? Why does it have a specific context for creating and updating profiles? I do not think the concept looks very good.

I am much more comfortable saying UserProfile.Update (username, password, firstName ...). You say: "Perform the task" Upgrade from the abstract concept of UserProfile on this particular dataset. "

Keep in mind that my use of the abstract word is strictly figurative, and not in the sense of the computer science of the word.

+1
source

Not many practical differences, but several philosophical differences.

I would recommend a singleton approach here - you really describe the type of data access object (DAO), albeit at a high level.

Some thoughts on why:

  • A Factory-style approach to creating the DBManager itself (i.e. DBManagerFactory) will simplify unit testing; It lends itself to injection injection models.

  • A class with a bunch of static methods is generally considered a switchblade / utility class of a class without a re-archiving topic (i.e. independence between methods). Think of a class like StringUtils - people assume that there is no general state in the class.

  • At the red level, using the β€œall static methods” approach, you create a lot of work for yourself, because you need to constantly enter static information for each method. Again, you can just have DBManagerFactory, which just has one static method to create DBManager, which eventually becomes a single.

+1
source

All Articles