From 80a568d33ab4c2b6658b61eff1261a4cd8bba6cb Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 14 Jan 2023 16:01:55 +0100 Subject: [PATCH] refactor: Removed `ChatHomeFactory` - `ChatHomeFactory` was only used during initialization. - By moving the instanciation of `chatHomes` into the implementation- specific configuration, the necessity of the factory vanished. --- .../backend/ChatBackendConfiguration.java | 22 ---------------- .../chat/backend/domain/ChatHomeFactory.java | 6 ----- .../inmemory/InMemoryChatHomeFactory.java | 19 -------------- .../InMemoryServicesConfiguration.java | 25 ++++++++++++++----- .../api/ChatBackendControllerTest.java | 4 +-- 5 files changed, 21 insertions(+), 55 deletions(-) delete mode 100644 src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeFactory.java delete mode 100644 src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeFactory.java diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java index 6e74bb01..f49e385b 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java @@ -1,9 +1,5 @@ package de.juplo.kafka.chat.backend; -import de.juplo.kafka.chat.backend.domain.ChatHome; -import de.juplo.kafka.chat.backend.domain.ChatHomeFactory; -import de.juplo.kafka.chat.backend.domain.ChatHomeService; -import de.juplo.kafka.chat.backend.persistence.StorageStrategy; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -15,24 +11,6 @@ import java.time.Clock; @EnableConfigurationProperties(ChatBackendProperties.class) public class ChatBackendConfiguration { - @Bean - ChatHome[] chatHomes( - ChatHomeFactory factory, - ChatBackendProperties properties, - StorageStrategy storageStrategy) - { - ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()]; - storageStrategy - .read() - .subscribe(chatRoom -> - { - int shard = chatRoom.getShard(); - if (chatHomes[shard] == null) - chatHomes[shard] = factory.createChatHome(shard); - }); - return chatHomes; - } - @Bean Clock clock() { diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeFactory.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeFactory.java deleted file mode 100644 index fb20cc7b..00000000 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeFactory.java +++ /dev/null @@ -1,6 +0,0 @@ -package de.juplo.kafka.chat.backend.domain; - -public interface ChatHomeFactory -{ - ChatHome createChatHome(int shard); -} diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeFactory.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeFactory.java deleted file mode 100644 index d2c6b635..00000000 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeFactory.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.juplo.kafka.chat.backend.persistence.inmemory; - -import de.juplo.kafka.chat.backend.domain.ChatHome; -import de.juplo.kafka.chat.backend.domain.ChatHomeFactory; -import lombok.RequiredArgsConstructor; - - -@RequiredArgsConstructor -public class InMemoryChatHomeFactory implements ChatHomeFactory -{ - private final InMemoryChatHomeService service; - - - @Override - public ChatHome createChatHome(int shard) - { - return new ChatHome(service, shard); - } -} diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java index 94dd5d8a..e40d950f 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java @@ -3,6 +3,7 @@ package de.juplo.kafka.chat.backend.persistence.inmemory; import de.juplo.kafka.chat.backend.ChatBackendProperties; import de.juplo.kafka.chat.backend.api.KafkaLikeShardingStrategy; import de.juplo.kafka.chat.backend.api.ShardingStrategy; +import de.juplo.kafka.chat.backend.domain.ChatHome; import de.juplo.kafka.chat.backend.persistence.StorageStrategy; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -19,6 +20,24 @@ import java.time.Clock; @Configuration public class InMemoryServicesConfiguration { + @Bean + ChatHome[] chatHomes( + ChatBackendProperties properties, + InMemoryChatHomeService chatHomeService, + StorageStrategy storageStrategy) + { + ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()]; + storageStrategy + .read() + .subscribe(chatRoom -> + { + int shard = chatRoom.getShard(); + if (chatHomes[shard] == null) + chatHomes[shard] = new ChatHome(chatHomeService, shard); + }); + return chatHomes; + } + @Bean InMemoryChatHomeService chatHomeService( ChatBackendProperties properties, @@ -30,12 +49,6 @@ public class InMemoryServicesConfiguration storageStrategy.read()); } - @Bean - InMemoryChatHomeFactory chatHomeFactory(InMemoryChatHomeService service) - { - return new InMemoryChatHomeFactory(service); - } - @Bean InMemoryChatRoomFactory chatRoomFactory( ShardingStrategy strategy, diff --git a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java index 12d34be1..5ebd08d1 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java @@ -262,12 +262,12 @@ public class ChatBackendControllerTest @Bean ChatHome[] chatHomes( ChatBackendProperties properties, - ChatHomeFactory factory) + InMemoryChatHomeService service) { ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()]; Arrays .stream(properties.getInmemory().getOwnedShards()) - .forEach(i -> chatHomes[i] = factory.createChatHome(i)); + .forEach(i -> chatHomes[i] = new ChatHome(service, i)); return chatHomes; } } -- 2.20.1