java - Is singleton approach right for accessing/maintaining database and internet connection -


so , working on app requires local db connection , uses web services send , receive data back-forth well.

  • whenever need , database operation create object of dbconnection class (this name of class using database) , perform operations on same.

  • similarly connecting internet use defaulthttpclient , create static object of same, , whenever need connection , call webservice create httpresponse object , response data.

1) using right approach or pattern ?

2) 1 more thing focusing on point number 2 static , singleton work same way ?

am using right approach or pattern ?

i don't think so. if use singleton (or static) the connection make difficult if code needs used / reused in context there may need more 1 connection; e.g. if make application multi-threaded, or turn library.

singletons have problems respect testability ...

generally speaking, can singletons (or statics) can context parameter of kind. dependency injection (di) frameworks provide neat way create shared objects , "wire" them other objects need them ... without using singletons or statics. alternatively, can hand, in variety of ways.

does static , singleton work same way ?

singletons design pattern. statics java language feature can used implement singleton pattern. not same thing (or same kind of thing), related.


Comments

Popular posts from this blog

Receive udp packets in android gstreamer -

php - Extract Text from HTTP referer -

java - Callback from new thread to class from which thread was initiated -