Creating an interface for DataServiceImpl

The first thing we can do is to create an interface for DataServiceImpl. Instead of using the direct class, we can use the newly created interface of DataServiceImpl in BusinessServiceImpl.

The following code shows you how to create an interface:

    public interface DataService { 
List<Data> retrieveData(User user);
}

Let's update the code in BusinessServiceImpl to use the interface:

    DataService dataService = new DataServiceImpl();

We are now using the DataService interface, but BusinessServiceImpl is still tightly coupled as it is creating an instance of DataServiceImpl:

    DataService dataService = new DataServiceImpl();

How do we solve that? How about moving the logic to create DataServiceImpl elsewhere and making it available to BusinessServiceImpl? Let's look at that next.

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

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