Yes, it's singleton, not the best of patterns, but it works here.
My preference now is to instantiate a new service for every request, similar
to how many modern web frameworks also instantiate a new controller class
for each request. Object creation is fairly cheap and it makes concurrent
programming simpler in that there cannot be any shared data (other than
statics which are not recommended for anything other than constants anyway).
To summarize simply:
HTTP Request > New Controller > New Service > New DAO*
*I don't always use the DAO layer.
A simple dependency injection pattern uses two simple constructors:
// used for most code
public void MyController() {
this.service = new MyService();
}
// allows easy mocking for unit testing
public void MyController(MyService service) {
this.service = service;
}
Of course, Spring does something like this for you.
This works very well for unit testing and simplifies concurrent programming
(or at least makes it harder to make a mistake).
Cheers,
Clinton
-----Original Message-----
From: Dmitry Neverov [mailto:[EMAIL PROTECTED]
Sent: October-23-07 3:21 AM
To: [email protected]
Subject: question on com.ibatis.jpetstore.service.*
Hi all!
Can anyone explain why should we use static method getInstance()? Why
not just use static methods for accesing DB? Is it some kind of design
pattern?
--
Best regards,
Dmitri Neverov