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 astatic
object of the same, and whenever need to get connection and call a webservice I createHttpResponse
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 ?
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.
No singletons are always bad. Avoid them unless you are forced to use them (because some dumb framework gives you no other option)
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.
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.
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.
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.