Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Service Locator Pattern

Service Locator Pattern

The Service Locator Pattern is used when we want to locate various services using JNDI lookups. Considering that looking up JNDI for a service is very expensive, the Service Locator Pattern makes full use of caching technology. When a service is requested for the first time, the service locator looks up the service in JNDI and caches the service object. When the same service is requested again, the service locator looks it up in its cache, which greatly improves the performance of the application. The following are the entities of this design pattern.


Summary

Intent

Used to provide a centralized service access point in an application for obtaining various services or resources.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

  1. Decoupled service access: Separates the service access logic from the business logic that uses the services.
  2. Centralized management: The service access points are managed centrally for easy maintenance and extension.
  3. Flexibility: Easy to add, modify, or replace services.

Disadvantages

Usage recommendation

Notes

Main roles included

  1. Service Locator Interface:

  2. Defines the method for obtaining services.
  3. - Defines the method for obtaining services.
    1. Service Locator Implementation:

    2. Implements the service locator interface and encapsulates the service lookup and access logic.
    3. - Implements the service locator interface and encapsulates the service lookup and access logic.
      1. Service Interface:

      2. Defines the abstract representation of a service.
      3. - Defines the abstract representation of a service.
        1. Concrete Service:

        2. Implements the service interface and provides specific service functions.
        3. - Implements the service interface and provides specific service functions.
          1. Client:

          2. Use the service locator to access the required services.
          3. - Use the service locator to access the required services.

            The Service Locator Pattern helps simplify service access logic and improve the flexibility and maintainability of an application by providing a centralized service access point.


            <

            Create a class that implements

            We will create ServiceLocator、InitialContext、Cache、Service As the various objects representing entities.Service1 and Service2 Represents the entity service.

            ServiceLocatorPatternDemo The class here acts as a client and will use ServiceLocator to demonstrate the Service Locator design pattern.

            Step 1

            Create the service interface Service.

            Service.java

            public

            interface

            Service

            {

            public

            String

            getName

            (

            )

            ;

            public

            void

            execute

            (

            )

            ;

            }

            Step 2

            Create the entity service.

            Service1.java

            public

            class

            Service1

            implements

            Service

            {

            public

            void

            execute

            (

            )

            {

            System

            .

            out

            .

            println

            (

            "

            Executing Service1

            "

            )

            ;

            }

            @

            Override

            public

            String

            getName

            (

            )

            {

            return

            "

            Service1

            "

            ;

            }

            }

            Service2.java

            public

            class

            Service2

            implements

            Service

            {

            public

            void

            execute

            (

            )

            {

            System

            .

            out

            .

            println

            (

            "

            Executing Service2

            "

            )

            ;

            }

            @

            Override

            public

            String

            getName

            (

            )

            {

            return

            "

            Service2

            "

            ;

            }

            }

            Step 3

            Create an InitialContext for the JNDI lookup.

            InitialContext.java

            public

            class

            InitialContext

            {

            public

            Object

            lookup

            (

            String

            jndiName

            )

            {

            if

            (

            jndiName

            .

            equalsIgnoreCase

            (

            "

            SERVICE1

            "

            )

            )

            {

            System

            .

            out

            .

            println

            (

            "

            Looking up and creating a new Service1 object

            "

            )

            ;

            return

            new

            Service1

            (

            )

            ;

            }

            else

            if

            (

            jndiName

            .

            equalsIgnoreCase

            (

            "

            SERVICE2

            "

            )

            )

            {

            System

            .

            out

            .

            println

            (

            "

            Looking up and creating a new Service2 object

            "

            )

            ;

            return

            new

            Service2

            (

            )

            ;

            }

            return

            null

            ;

            }

            }

            Step 4

            Create the cache Cache.

            Cache.java

            import

            java

            .

            util

            .

            ArrayList

            ;

            import

            java

            .

            util

            .

            List

            ;

            public

            class

            Cache

            {

            private

            List

            <
            
            Service
            
            services

            ;

            public

            Cache

            (

            )

            {

            services

            =

            new

            ArrayList

            <
            
            Service
            
            (
            
            )

            ;

            }

            public

            Service

            getService

            (

            String

            serviceName

            )

            {

            for

            (

            Service

            service

            :

            services

            )

            {

            if

            (

            service

            .

            getName

            (

            )

            .

            equalsIgnoreCase

            (

            serviceName

            )

            )

            {

            System

            .

            out

            .

            println

            (

            "

            Returning cached

            "

            serviceName

            "

            object

            "

            )

            ;

            return

            service

            ;

            }

            }

            return

            null

            ;

            }

            public

            void

            addService

            (

            Service

            newService

            )

            {

            boolean

            exists

            =

            false

            ;

            for

            (

            Service

            service

            :

            services

            )

            {

            if

            (

            service

            .

            getName

            (

            )

            .

            equalsIgnoreCase

            (

            newService

            .

            getName

            (

            )

            )

            )

            {

            exists

            =

            true

            ;

            }

            }

            if

            (

            !

            exists

            )

            {

            services

            .

            add

            (

            newService

            )

            ;

            }

            }

            }

            Step 5

            Create the service locator.

            ServiceLocator.java

            public

            class

            ServiceLocator

            {

            private

            static

            Cache

            cache

            ;

            static

            {

            cache

            =

            new

            Cache

            (

            )

            ;

            }

            public

            static

            Service

            getService

            (

            String

            jndiName

            )

            {

            Service

            service

            =

            cache

            .

            getService

            (

            jndiName

            )

            ;

            if

            (

            service

            !=

            null

            )

            {

            return

            service

            ;

            }

            InitialContext

            context

            =

            new

            InitialContext

            (

            )

            ;

            Service

            service1

            =

            (

            Service

            )

            context

            .

            lookup

            (

            jndiName

            )

            ;

            cache

            .

            addService

            (

            service1

            )

            ;

            return

            service1

            ;

            }

            }

            Step 6

            Use ServiceLocator to demonstrate the Service Locator design pattern.

            ServiceLocatorPatternDemo.java

            public

            class

            ServiceLocatorPatternDemo

            {

            public

            static

            void

            main

            (

            String

            [

            ]

            args

            )

            {

            Service

            service

            =

            ServiceLocator

            .

            getService

            (

            "

            Service1

            "

            )

            ;

            service

            .

            execute

            (

            )

            ;

            service

            =

            ServiceLocator

            .

            getService

            (

            "

            Service2

            "

            )

            ;

            service

            .

            execute

            (

            )

            ;

            service

            =

            ServiceLocator

            .

            getService

            (

            "

            Service1

            "

            )

            ;

            service

            .

            execute

            (

            )

            ;

            service

            =

            ServiceLocator

            .

            getService

            (

            "

            Service2

            "

            )

            ;

            service

            .

            execute

            (

            )

            ;

            }

            }

            Step 7

            Run the program, output result:

            
            
            Looking up and creating a new Service1 object
            
            Executing Service1
            
            Looking up and creating a new Service2 object
            
            Executing Service2
            
            Returning cached  Service1 object
            
            Executing Service1
            
            Returning cached  Service2 object
            
            Executing Service2