Understanding services

Let's begin our journey of understanding services with a concept that you should be very familiar with as a Java developer--polymorphism. It starts with one interface and (possibly multiple) implementations of that interface. Although interfaces are not strictly necessary for services, they are still a good place to start. Let's say you define a service interface called MyServiceInterface that looks like this:

    package service.api; 
    public interface MyServiceInterface { 
      public void runService(); 
    } 

Now you can have multiple modules containing classes that implement this interface. Since all those modules need access to this interface, let's throw this interface into a module of its own, called service.api, and expose the package that the interface MyServiceInterface is in. Then each implementation module can require the service.api module and implement MyServiceInterface.

Consider there are three implementations of MyServiceInterface in three corresponding modules. Since they need the interface to implement it in the first place, all three implementation modules read the service.api module to get access to MyServiceInterface. Imagine each module does just that, and that each contains a class that implements MyServiceInterface:

Now, the consumer module needs to call one of these implementations to actually run the service. The goal here is not to have the consumer module directly read the various implementation modules, since that's tight coupling. What we want is for the consumer module to read just the interface module service.api and deal with the interface type only, but still somehow manage to get access to instances of that interface's implementations. Remember, we do not want the consumer to require the individual implementation modules (the Xs in the following diagram):

..................Content has been hidden....................

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