From 0eab1a667e8863acfe318f520d9f0bca7fba2e2b Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 28 Dec 2022 18:09:06 +0100 Subject: [PATCH] refactor: Introduced `ChatroomFactory` - `ChatBackendController` only has to know of the `ChatroomFactory`. - `ChatBackendController` does not have to know the `PersistenceStrategy`. --- .../backend/api/ChatBackendController.java | 6 ++--- .../chat/backend/domain/ChatroomFactory.java | 9 ++++++++ .../persistence/InMemoryChatroomFactory.java | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/main/java/de/juplo/kafka/chat/backend/domain/ChatroomFactory.java create mode 100644 src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomFactory.java diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java index 17d2b1f5..b8024e63 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java @@ -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 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 index 00000000..02ce3d27 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatroomFactory.java @@ -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 index 00000000..4f33b110 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/InMemoryChatroomFactory.java @@ -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); + } +} -- 2.20.1