This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Service Locator pattern  RSS feed

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 39129
723
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think? List out some and we can provide feedback.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 39129
723
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 122
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the use case for you to think about the DAO is injected with Service Locator instance?
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is possible, for example, cache manager in DAO class.
 
kumar naresh
Ranch Hand
Posts: 122
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But couldn't it be the violation of layer structure. Service Locator falls in Business layer but DAO comes in Integration layer.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!