image
Service Locator
Start course
Difficulty
Intermediate
Duration
4h 56m
Students
118
Ratings
4/5
Description

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.

Prerequisites

Basic knowledge of Java programming.

 

Transcript

Hello, dear friends. In this video, we will start to examine business tier patterns starting with the service locator pattern. Let's begin. The service locator pattern is a relatively old pattern that was very popular with the Java EE. Martin Fowler described it in 2004 on his blog. The goal of this pattern is to improve the modularity of your application by removing the dependency between the client and the implementation of an interface. Basically, the purpose of the service locator pattern is to return the service instances on demand. This is useful for decoupling service consumers from concrete classes. The service locator pattern is used to decouple the service consumers and the concrete classes like DAO implementations. The pattern looks for adequate service, saves it in cache storage to reduce the number of requests, and therefore, the strain on the server and provides the application with their instances.

The service locator design pattern is used when we want to locate various services using JNDI lookup. Considering a high cost of looking up JNDI for a service, service locator pattern makes use of caching technique. Spring Framework internally uses service locator pattern to manage dependency injection. These days, service locator isn't of much use anymore except in very small proprietary projects. As you can see in the UML of the service locator pattern, there are five participants. The Client object is a service consumer. It's responsible for invoking the request from the ServiceLocator. The service locator abstracts the API lookup services, vendor dependencies, lookup complexities, and provides a simple interface to clients. This reduces the client's complexity. In addition, the same client or other clients can reuse the service locator. Cache is an object for storing service references to reuse them later.

The InitialContext object is the start point in the lookup and creation process. Service providers provide the context object, which varies depending on the type of business object provided by the service locator's lookup and creation service. The service component represents the original services or their implementation. The ServiceFactory object represents an object that provides lifecycle management for the BusinessService objects. The ServiceFactory object for enterprise beans is EJB home object. Now, I want to create a project using this pattern. Let's begin. Create a new project and name it ServiceLocator. In describing the previous patterns, we used the employees example. I'd like to use it again in this project. First, we need a Service interface. In the interface, we need two methods; getServiceName, execute. Now, we need a couple of concrete classes.

We'll implement this interface. First, start with EmployeeService, implement Service, override methods, add for customer, override methods. According to the pattern, when looking up for these services, we should cache them to reduce server strain, so we have to define cache. We need service list. In constructor, we will use...  I also want to add two methods. Now add getService method. Add service method. Done. We also need a class to look for and instantiate our services. Create InitialContext. Define a new method name to lookup. Done. And finally, we can define a locator class to expose to the client that uses the initial context class to look for services and the cache class to cache them for further use. Add static cache. We also need to getService method. Now, our structure is ready. Let's use. First, define a service with EmployeeService. Execute. Now, change with Customer. Execute. Done. And look, we can see employees operations and customer operations.

Great. I think that covers the service locator pattern. In the following video, we will examine the business delegate pattern. See you in the following video.

 

About the Author
Students
3948
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics