refactor: Introduced `ChatroomFactory`
authorKai Moritz <kai@juplo.de>
Wed, 28 Dec 2022 17:09:06 +0000 (18:09 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 18:35:59 +0000 (19:35 +0100)
- `ChatBackendController` only has to know of the `ChatroomFactory`.
- `ChatBackendController` does not have to know the `PersistenceStrategy`.

src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java
src/main/java/de/juplo/kafka/chat/backend/domain/ChatroomFactory.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomFactory.java [new file with mode: 0644]

index 17d2b1f..b8024e6 100644 (file)
@@ -1,7 +1,7 @@
 package de.juplo.kafka.chat.backend.api;
 
 import de.juplo.kafka.chat.backend.domain.Chatroom;
-import de.juplo.kafka.chat.backend.domain.PersistenceStrategy;
+import de.juplo.kafka.chat.backend.domain.ChatroomFactory;
 import lombok.RequiredArgsConstructor;
 import org.springframework.http.MediaType;
 import org.springframework.web.bind.annotation.*;
@@ -21,14 +21,14 @@ import java.util.UUID;
 public class ChatBackendController
 {
   private final Map<UUID, Chatroom> chatrooms = new HashMap<>();
-  private final PersistenceStrategy persistenceStrategy;
+  private final ChatroomFactory factory;
   private final Clock clock;
 
 
   @PostMapping("create")
   public Chatroom create(@RequestBody String name)
   {
-    Chatroom chatroom = new Chatroom(UUID.randomUUID(), name, persistenceStrategy);
+    Chatroom chatroom = factory.createChatroom(UUID.randomUUID(), name);
     chatrooms.put(chatroom.getId(), chatroom);
     return chatroom;
   }
diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatroomFactory.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatroomFactory.java
new file mode 100644 (file)
index 0000000..02ce3d2
--- /dev/null
@@ -0,0 +1,9 @@
+package de.juplo.kafka.chat.backend.domain;
+
+import java.util.UUID;
+
+
+public interface ChatroomFactory
+{
+  Chatroom createChatroom(UUID id, String name);
+}
diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomFactory.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomFactory.java
new file mode 100644 (file)
index 0000000..4f33b11
--- /dev/null
@@ -0,0 +1,23 @@
+package de.juplo.kafka.chat.backend.persistence;
+
+import de.juplo.kafka.chat.backend.domain.Chatroom;
+import de.juplo.kafka.chat.backend.domain.ChatroomFactory;
+import de.juplo.kafka.chat.backend.domain.PersistenceStrategy;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Component;
+
+import java.util.UUID;
+
+@Component
+@RequiredArgsConstructor
+public class InMemoryChatroomFactory implements ChatroomFactory
+{
+  private final PersistenceStrategy persistenceStrategy;
+
+
+  @Override
+  public Chatroom createChatroom(UUID id, String name)
+  {
+    return new Chatroom(id, name, persistenceStrategy);
+  }
+}