This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes Service Locator pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "Service Locator pattern" Watch "Service Locator pattern" New topic
Author

Service Locator pattern

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
In the JSF, I want to use service locator pattern, then I have two choices, one is use singleton + synchronizedMap, another choice is to inject a service locator instance in the DAO class . these two choices , what is pros and cons ?

thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

What do you think? List out some and we can provide feedback.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Singleton approach can cache data, one object across system, easy to use. the bad part is the testing. but singleton is good enough for Service Locator pattern.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

I agree. Good analysis.

There are some techniques to make a Singleton more unit testable. First you give it in interface. Then other classes can pass in a mock instead of the real Singleton. You can also change the static variable of the Singleton to another implementation of the interface, but that is hacky since it is global.
kumar naresh
Ranch Hand

Joined: Mar 12, 2011
Posts: 118
    
    1
What is the use case for you to think about the DAO is injected with Service Locator instance?


OCMJEA 5 |
http://sceacertification.blogspot.in/2011_12_01_archive.html
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
it is possible, for example, cache manager in DAO class.
kumar naresh
Ranch Hand

Joined: Mar 12, 2011
Posts: 118
    
    1
But couldn't it be the violation of layer structure. Service Locator falls in Business layer but DAO comes in Integration layer.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Service Locator pattern
 
Similar Threads
Best Practice For Accessing EJB's
To Bill
BusinessDelegate and ServiceLocator pattern
mock test on design patterns : ATTENTION
workflow implemetation with stateful session bean