200803-java-devops / http-server

0 stars 1 forks source link

Interfaces (3 points) #6

Open MehrabRahman opened 4 years ago

MehrabRahman commented 4 years ago

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?

dougliu20 commented 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.

acasey456 commented 4 years ago

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.

hibby96 commented 4 years ago

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.

afkpuezo commented 4 years ago

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.

haoknowah commented 4 years ago

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.

AaronDownward commented 4 years ago

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.

Malokingi commented 4 years ago

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.

  1. Classes which use them:

    • Abstract Classes: Can use only One Class (including Abstract Classes)
    • Interfaces: Can use more than One Interface
  2. Variable Flexibility:

    • Abstract Classes: May freely have whatever Access Modifiers
    • Interfaces: Implicitly (in other words, by default) have public static final Access Modifiers
  3. Method Flexibility:

    • Abstract Classes: May freely have whatever Access Modifiers
    • Interfaces:
      • Before J8: must be public and abstract (Only Method signatures, no functionality specified)
      • As of J8: Java 8 added the option to make default methods
  4. Conventionally, when to use:

    • Abstract Classes: You want to develop a specific hierarchy of functionality where state and behavior are tracked
    • Interfaces: You want a class to be free to implement without hierarchical constraints

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.