Is singleton approach right for accessing/maintain

2019-07-23 07:28发布

So , I am working on an app which requires a local db connection and uses some web services to send and receive data back-forth as well.

  • Whenever I need and database operation then I create an object of my DbConnection class (this is the name of class I am using to get my database) and perform operations on the same.

  • Similarly for connecting with internet I use DefaultHttpClient and create a static object of the same, and whenever need to get connection and call a webservice I create HttpResponse object and get response data.

1) Am I using the right approach or pattern ?

2) Also one more thing focusing on point number 2 does static and singleton work the same way ?

4条回答
祖国的老花朵
2楼-- · 2019-07-23 07:52

Wow. There is a lot more to your question than you think, but to give you the short, short answer, most projects I've seen (which don't use ORM, something you may want to look into) will have a class which wraps around the DB object that calls all of the appropriate queries, handles escaping &c. It will implement a public interface so that it can be served up by a factory class to something which is expecting a matching interface.

As to Singleton/static, well, that really depends on your need. In the pattern I mentioned above, Singleton would be far superior: it means each of the objects accessing the database will be able to store a local reference to the db object. It also means that you can encapsulate the process of fetching the Singleton in a parent class. Use of a static class will not afford you that option.

查看更多
够拽才男人
3楼-- · 2019-07-23 07:55

No singletons are always bad. Avoid them unless you are forced to use them (because some dumb framework gives you no other option)

  • they make no sense, what does a global variable mean? What if you have multiple classloaders? What if you run the same program twice?
  • they cause hidden temporal coupling. singleton A must be initialized before singleton B, but not before singleton C or singleton D. most people don't even know how to control the initialize time a singleton in Java. (basically you have to touch a static field to ensure it's initialized, but don't touch it too soon or you'll break the order!)
  • they cause security vulnerabilities if you're in a secure environment, such as securitymanager, or a secure language like E.
  • it's hard to make them work in multi-threaded code. hard as in, there are only a few thousand people in the world that know how to do this "correctly" in a language like Java, which still requires intermodular analysis to get right unless you simply encode something mimicking the actor model, which defeats the purpose of java concurrency
  • the coauthor of the java language specification (gilad bracha) even says singletons are horrible and made a new language (newspeak) without them

Either you pass some object around, or you face the above problems plus more I probably forgot to mention. Why such mundane issues still exist in 2013 is beyond me.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-07-23 07:55

Answer to point 1 - Yes singleton is the right approach. Ideally you must establish only single connection to database. Sinleton will help you achieve this.

Answer to point 2 - No static does not necessarily owrk the same way as singleton. If we make multiple calls to create object in singleton pattern, it returns the same object. But static objects can be created multiple times if create object is called several times.

But you can assure that static objects are created only once, which will then be same as singleton pattern.

查看更多
Explosion°爆炸
5楼-- · 2019-07-23 07:56

Am I using the right approach or pattern ?

I don't think so. If you use a singleton (or a static) for the connection then you make it difficult if your code needs to be used / reused in a context where there may need to be more than one connection; e.g. if you make your application multi-threaded, or turn it into a library.

Singletons also have problems with respect to testability ...

Generally speaking, anything you can do with Singletons (or statics) you can also do with a context parameter of some kind. Dependency Injection (DI) frameworks provide a neat way to create the shared objects and "wire" them into the other objects that need them ... without using singletons or statics. Alternatively, you can do it by hand, in a variety of ways.

Does static and singleton work the same way ?

Singletons are a design pattern. Statics are Java language feature that can be used to implement the Singleton pattern. So they are not the same thing (or even the same kind of thing), but they are related.

查看更多
登录 后发表回答