From: Kai Moritz Date: Wed, 28 Dec 2022 17:09:06 +0000 (+0100) Subject: refactor: Introduced `ChatroomFactory` X-Git-Tag: TEST~60 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=d2aef4ab1b82ccb0883bad80ce428fcab6e6c293;p=demos%2Fkafka%2Fchat refactor: Introduced `ChatroomFactory` - `ChatBackendController` only has to know of the `ChatroomFactory`. - `ChatBackendController` does not have to know the `PersistenceStrategy`. --- 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); + } +}