added chain of responsibility example

This commit is contained in:
Louis Heredero 2024-11-04 09:01:38 +01:00
parent fd99c16e89
commit d46443f6d5
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
7 changed files with 106 additions and 6 deletions

View File

@ -3,13 +3,13 @@ package lab7_state.ex2;
import lab7_state.ex2.states.*;
public class User {
private String name;
private final String name;
private UserState currentState;
private UserState unregisteredState;
private UserState registeredState;
private UserState inMeetingState;
private UserState waitingToSpeakState;
private UserState speakingState;
private final UserState unregisteredState;
private final UserState registeredState;
private final UserState inMeetingState;
private final UserState waitingToSpeakState;
private final UserState speakingState;
public User(String name) {
this.name = name;

View File

@ -0,0 +1,19 @@
package learn.simple_responsibility_chain;
import java.util.Arrays;
import java.util.List;
public class ConcreteHandler1 extends Handler {
private final List<String> myAnimals = Arrays.asList("cat", "dog", "horse");
@Override
public void handleRequest(Request request) {
if (myAnimals.contains(request.getValue())) {
System.out.println("Request for " + request.getValue() + " handled by " + this);
} else {
if (successor != null) {
successor.handleRequest(request);
}
}
}
}

View File

@ -0,0 +1,19 @@
package learn.simple_responsibility_chain;
import java.util.Arrays;
import java.util.List;
public class ConcreteHandler2 extends Handler {
private final List<String> myColors = Arrays.asList("red", "yellow", "blue");
@Override
public void handleRequest(Request request) {
if (myColors.contains(request.getValue())) {
System.out.println("Request for " + request.getValue() + " handled by " + this);
} else {
if (successor != null) {
successor.handleRequest(request);
}
}
}
}

View File

@ -0,0 +1,17 @@
package learn.simple_responsibility_chain;
import java.util.Arrays;
import java.util.List;
public class ConcreteHandler3 extends Handler {
private final List<String> myNumbers = Arrays.asList("1", "2", "3");
@Override
public void handleRequest(Request request) {
if (myNumbers.contains(request.getValue())) {
System.out.println("Request for " + request.getValue() + " handled by " + this);
} else {
throw new RuntimeException("Request could not be handled");
}
}
}

View File

@ -0,0 +1,11 @@
package learn.simple_responsibility_chain;
public abstract class Handler {
protected Handler successor;
public void setSuccessor(Handler successor) {
this.successor = successor;
}
public abstract void handleRequest(Request request);
}

View File

@ -0,0 +1,21 @@
package learn.simple_responsibility_chain;
public class Main {
public static void main(String[] args) {
Handler firstHandler = new ConcreteHandler1();
Handler secondHandler = new ConcreteHandler2();
Handler thirdHandler = new ConcreteHandler3();
firstHandler.setSuccessor(secondHandler);
secondHandler.setSuccessor(thirdHandler);
Request request1 = new Request("dog");
firstHandler.handleRequest(request1);
Request request2 = new Request("3");
firstHandler.handleRequest(request2);
Request request3 = new Request("house");
firstHandler.handleRequest(request3);
}
}

View File

@ -0,0 +1,13 @@
package learn.simple_responsibility_chain;
public class Request {
private String value;
public Request(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}