Example

The sample code implementation is available at  09-module-patterns/02-services. The pattern.two.service module exports the PublicInterface. It doesn't contain any implementation classes of its own:

    module pattern.two.service { 
      exports pattern.two.external; 
    } 

Two implementation modules pattern.two.implA and pattern.two.implB both contain implementations of the service that are declared using provides in the module declaration. Both implementation modules require the service module pattern.two.service to access the interface. Here's how the module definition for one of the implementation modules looks like:

    module pattern.two.implA { 
      requires pattern.two.service; 
      provides pattern.two.external.PublicInterface with 
pattern.two.implA.ImplA; }

The consumer module also depends on the service interface module, and not on the implementation modules. The uses clause in the module definition indicates that the module will need to look up the service:

    module consumer { 
      requires pattern.two.service; 
      uses pattern.two.external.PublicInterface; 
    } 

Benefits:

  • Provides an additional layer of abstraction between the service consumer and provider logic.
  • Completely loose coupling between consumer and implementation modules. There's no hard-wired requires dependency on the implementation modules.
  • Flexibility in the implementation options available. Modules can be dropped in the module path at runtime, and as long as they implement and provide the service interface, they can be plugged in to the application.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.61.218