Why do we have dependencies?

Enterprise applications have a wide range of functionality and features. Some parts of the application need to talk to the database; some parts of the application are used to show information to the user (user interface); and some parts of the application have business logic, while other parts of the application might be talking to other applications via web services.

One of the important design objectives is the Single Responsibility Principle (SRP). We would want each class and component in our application to have a well-defined specific responsibility. We would not want the same class to be responsible for multiple features.

Let's consider an example—if you have Class A, which talks to a database and does some UI logic, it's not well designed. Class A has multiple responsibilities and it would not satisfy the SRP.

SRP is also applicable to application layers. To promote clear separation, all well-designed applications have multiple layers. Every layer has a well-defined responsibility:

Here are some of the specific responsibilities of individual layers:

  • UI layer: This is responsible for the View logic—how to show the data to the end user
  • Business layer: This is responsible for Business logic
  • Data layer: This is responsible for interacting with the database
  • Integration layer: This is responsible for talking to other applications using web services or another connection mechanism

Consider the classes in the Business layer. They would need to get data from the database. How do they do that?

They would talk to classes in the Data layer. So, classes in the Business layer are dependent on classes in the Data layer. Classes in the Data layer are dependencies of classes in the Business layer.

In the preceding example, the DataServiceImpl class gets data related to the user from the database. The BusinessServiceImpl class is a typical business service, talking to the data service, DataServiceImpl, for data and adding business logic on top of it (in this example, the business logic is very simple—calculate the sum of data returned by the data service).

Enterprise applications are complex. To make applications easy to maintain, we adhere to SRP. When you implement SRP well, you would have a number of small classes with a number of dependencies.

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

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