200803-java-devops / http-server

0 stars 1 forks source link

Lambdas (5 points) #5

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/App.java#L10-L13

What is happening here? Explain the syntax with regards to the use of the arrow symbol. What might this look like if written without the arrow?

dougliu20 commented 4 years ago

Lambdas are functions that are introduced in java 1.8. You can execute something without creating the method. The status will be set to "200 OK" and the Body will be set to "Server OK" when taking in request and response. You can do it the harder way by:

server.addController("/health", (request, response) { void service(Request request, Response response) { response.setStatus("200 OK"); response.setBody("Server OK".getBytes()); } }

(Not 100% sure but please comment if there are issues)

ItsAlxl commented 4 years ago

The -> indicates a lambda function, used here to define the void service(Request request, Response response) method from the Controller interface. Lambda functions help simplify the syntax when using interfaces that only have one method. Without using a lambda function, this could be achieved with an anonymous class.

server.addController("/health", new Controller() {
     void service(Request request, Response response) {
          response.setStatus("200 OK"); 
          response.setBody("Server OK".getBytes());
     }
});

You could also create a named class with the service function defined as above, though this would probably be excessive because it would not see any use outside of this one spot in the code.

haoknowah commented 4 years ago

Lambdas are short functions that can be made without creating methods for them, which in this case would be a void method sets the request and response objects from the Controller. This could be done with:

server.addController("/health", new Controller() {
void service(Request request, Response response){
response.setStatus("200 OK");
response.setBody("Server OK".getBytes());
}
});
hibby96 commented 4 years ago

The lambda syntax is as follows: 'parameters -> expression'

The lambda is taking in parameters to then use in an expression here. This is all being done inside the addController method. The lambda is technically creating an anonymous class that implements the service method from the Controller interface, using (request, response) as the parameters just as they are defined in the interface. The body of this said newly created anonymous class and method is the following code block containing the setStatus and setBody methods. After all of this, we are left with a value that goes in place of the second parameter of the addController(String path, Controller controller) method.

Thus, as previously mentioned, without the lambda, we would need to create another implementation of Controller:

public class Anothercontroller implements Controller {
void service(Request request, Response response) {
     response.setStatus("200 OK"); 
     response.setBody("Server OK".getBytes()); 
}
}