aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 29237
    
139

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: 29237
    
139

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: 117
    
    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: 117
    
    1
But couldn't it be the violation of layer structure. Service Locator falls in Business layer but DAO comes in Integration layer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Service Locator pattern
 
Similar Threads
To Bill
workflow implemetation with stateful session bean
BusinessDelegate and ServiceLocator pattern
mock test on design patterns : ATTENTION
Best Practice For Accessing EJB's