diff --git a/src/lab7_state/ex2/User.java b/src/lab7_state/ex2/User.java index a850f29..1119765 100644 --- a/src/lab7_state/ex2/User.java +++ b/src/lab7_state/ex2/User.java @@ -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; diff --git a/src/learn/simple_responsibility_chain/ConcreteHandler1.java b/src/learn/simple_responsibility_chain/ConcreteHandler1.java new file mode 100644 index 0000000..155de43 --- /dev/null +++ b/src/learn/simple_responsibility_chain/ConcreteHandler1.java @@ -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 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); + } + } + } +} diff --git a/src/learn/simple_responsibility_chain/ConcreteHandler2.java b/src/learn/simple_responsibility_chain/ConcreteHandler2.java new file mode 100644 index 0000000..f869700 --- /dev/null +++ b/src/learn/simple_responsibility_chain/ConcreteHandler2.java @@ -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 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); + } + } + } +} diff --git a/src/learn/simple_responsibility_chain/ConcreteHandler3.java b/src/learn/simple_responsibility_chain/ConcreteHandler3.java new file mode 100644 index 0000000..0f27835 --- /dev/null +++ b/src/learn/simple_responsibility_chain/ConcreteHandler3.java @@ -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 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"); + } + } +} diff --git a/src/learn/simple_responsibility_chain/Handler.java b/src/learn/simple_responsibility_chain/Handler.java new file mode 100644 index 0000000..8ff18af --- /dev/null +++ b/src/learn/simple_responsibility_chain/Handler.java @@ -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); +} diff --git a/src/learn/simple_responsibility_chain/Main.java b/src/learn/simple_responsibility_chain/Main.java new file mode 100644 index 0000000..5d961ad --- /dev/null +++ b/src/learn/simple_responsibility_chain/Main.java @@ -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); + } +} diff --git a/src/learn/simple_responsibility_chain/Request.java b/src/learn/simple_responsibility_chain/Request.java new file mode 100644 index 0000000..09bcda6 --- /dev/null +++ b/src/learn/simple_responsibility_chain/Request.java @@ -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; + } +}