| 
									
										
										
										
											2015-08-13 23:54:40 +02:00
										 |  |  | --- | 
					
						
							|  |  |  | layout: pattern | 
					
						
							|  |  |  | title: Service Locator | 
					
						
							|  |  |  | folder: service-locator | 
					
						
							| 
									
										
										
										
											2015-08-15 18:03:05 +02:00
										 |  |  | permalink: /patterns/service-locator/ | 
					
						
							| 
									
										
										
										
											2019-12-13 21:09:28 +02:00
										 |  |  | categories: Architectural | 
					
						
							| 
									
										
										
										
											2015-12-28 15:52:44 +02:00
										 |  |  | tags: | 
					
						
							| 
									
										
										
										
											2019-12-13 21:09:28 +02:00
										 |  |  |  - Game programming | 
					
						
							| 
									
										
										
										
											2015-12-28 16:07:43 +02:00
										 |  |  |  - Performance | 
					
						
							| 
									
										
										
										
											2015-08-13 23:54:40 +02:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 21:14:30 +01:00
										 |  |  | ## Intent
 | 
					
						
							|  |  |  | Encapsulate the processes involved in obtaining a service with a | 
					
						
							| 
									
										
										
										
											2015-08-13 23:54:40 +02:00
										 |  |  | strong abstraction layer. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-07 20:01:13 +02:00
										 |  |  | ## Class diagram
 | 
					
						
							| 
									
										
										
										
											2015-09-24 12:23:02 +05:30
										 |  |  |  | 
					
						
							| 
									
										
										
										
											2015-08-13 23:54:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 21:14:30 +01:00
										 |  |  | ## Applicability
 | 
					
						
							|  |  |  | The service locator pattern is applicable whenever we want | 
					
						
							| 
									
										
										
										
											2015-08-13 23:54:40 +02:00
										 |  |  | to locate/fetch various services using JNDI which, typically, is a redundant | 
					
						
							|  |  |  | and expensive lookup. The service Locator pattern addresses this expensive | 
					
						
							|  |  |  | lookup by making use of caching techniques ie. for the very first time a | 
					
						
							|  |  |  | particular service is requested, the service Locator looks up in JNDI, fetched | 
					
						
							|  |  |  | the relevant service and then finally caches this service object. Now, further | 
					
						
							|  |  |  | lookups of the same service via Service Locator is done in its cache which | 
					
						
							|  |  |  | improves the performance of application to great extent. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 21:14:30 +01:00
										 |  |  | ## Typical Use Case
 | 
					
						
							| 
									
										
										
										
											2015-08-13 23:54:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 21:09:28 +02:00
										 |  |  | * When network hits are expensive and time consuming | 
					
						
							|  |  |  | * Lookups of services are done quite frequently | 
					
						
							|  |  |  | * Large number of services are being used | 
					
						
							| 
									
										
										
										
											2015-09-24 12:23:02 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 21:59:36 +02:00
										 |  |  | ## Consequences
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * Violates Interface Segregation Principle (ISP) by providing pattern consumers with an access  | 
					
						
							|  |  |  | to a number of services that they don't potentially need. | 
					
						
							|  |  |  | * Creates hidden dependencies that can break the clients at runtime. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-03 21:14:30 +01:00
										 |  |  | ## Credits
 | 
					
						
							| 
									
										
										
										
											2015-09-24 12:23:02 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 13:31:07 +03:00
										 |  |  | * [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31) |