Open MehrabRahman opened 4 years ago
This is basically set up the structure for service that involves in request and response. The main benefit is that it used to achieve abstraction. The way the interface is written requires both request and response when service is being called in main, and helps the development of the request and response class.
The purpose of this file is to achieve abstraction. In this instance it is calling the service to start and feeding it a request and response which is necessary for the service to to be called when running the app.
So long as the FileController and any other type of Controller is implementing the Controller interface, they will be required to implement the service method that takes the parameters of Request and Response type. This can help ensure that the Controllers are built the same way and always contain this essential method and take in those parameter types.
So far, only FileController implements this interface.
Defining the Controller interface allows for some flexibility in implementing the control and service logic.
The service method is also an example of dependency injection - the Request and Response objects are passed to the Controller, rather than the Controller creating them itself. This allows for more flexibility - as different implementations of Request and Response could be passed to the same Controller.
This file is an interface which is an abstract class, and thus anything implementing this interface will have to have the service method that takes in those parameters and define it in its code. It is only in the FileController but is written in such a way that both Request and Response objects must be added into the method and created outside of the interface, allowing multiple instances of these objects to run with the same interface.
An interface serves as a template for implementing classes and ensures that those classes override included methods. In this application, it is implemented by the Class FileController. Doing this allows some details to be hidden to achieve abstraction, and multiple interfaces can be inherited if we want that class to implement more interfaces.
This file defines an Interface, called Controller
. One of the main purposes of an Interface is to guarantee the availability of methods in an implementing class. kind of like an Abstract Class. One way to understand Interfaces is to understand how they are similar to and different from Abstract Classes, which they are often confused with.
Classes which use them:
Variable Flexibility:
public static final
Access ModifiersMethod Flexibility:
public
and abstract (Only Method signatures, no functionality specified)default
methodsConventionally, when to use:
So, when some other part of the code uses this interface, since the method is abstract, it will have to specify that method's behavior at that time.
Also, since this Interface contains nothing but one abstract method, it's called a Functional Interface.
https://github.com/200803-java-devops/http-server/blob/4dbbddbe2df1fd298902532af196afabd4580dfd/src/main/java/com/github/MehrabRahman/http/Controller.java#L3-L5
What purpose does this file serve? How is it used in this application, and what benefits are there to its use? Is there anything special about the way this Interface is written that leads to something interesting happening in another part of the code base?