added chain of responsibility example
This commit is contained in:
parent
fd99c16e89
commit
d46443f6d5
@ -3,13 +3,13 @@ package lab7_state.ex2;
|
|||||||
import lab7_state.ex2.states.*;
|
import lab7_state.ex2.states.*;
|
||||||
|
|
||||||
public class User {
|
public class User {
|
||||||
private String name;
|
private final String name;
|
||||||
private UserState currentState;
|
private UserState currentState;
|
||||||
private UserState unregisteredState;
|
private final UserState unregisteredState;
|
||||||
private UserState registeredState;
|
private final UserState registeredState;
|
||||||
private UserState inMeetingState;
|
private final UserState inMeetingState;
|
||||||
private UserState waitingToSpeakState;
|
private final UserState waitingToSpeakState;
|
||||||
private UserState speakingState;
|
private final UserState speakingState;
|
||||||
|
|
||||||
public User(String name) {
|
public User(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
19
src/learn/simple_responsibility_chain/ConcreteHandler1.java
Normal file
19
src/learn/simple_responsibility_chain/ConcreteHandler1.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
src/learn/simple_responsibility_chain/ConcreteHandler2.java
Normal file
19
src/learn/simple_responsibility_chain/ConcreteHandler2.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
src/learn/simple_responsibility_chain/ConcreteHandler3.java
Normal file
17
src/learn/simple_responsibility_chain/ConcreteHandler3.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
src/learn/simple_responsibility_chain/Handler.java
Normal file
11
src/learn/simple_responsibility_chain/Handler.java
Normal 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);
|
||||||
|
}
|
21
src/learn/simple_responsibility_chain/Main.java
Normal file
21
src/learn/simple_responsibility_chain/Main.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
13
src/learn/simple_responsibility_chain/Request.java
Normal file
13
src/learn/simple_responsibility_chain/Request.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user