Skip to content

refactor: use abstract factory pattern for card strategy instantiation - #56

Merged
Dejmenek merged 5 commits into
mainfrom
51-refactor-card-strategy-instantiation-to-use-abstract-factory-pattern
Oct 27, 2025
Merged

Dejmenek merged 5 commits into
mainfrom
51-refactor-card-strategy-instantiation-to-use-abstract-factory-pattern

Conversation

@Dejmenek

Copy link
Copy Markdown
Owner

Summary

This pull request introduces a CardStrategyFactory to instantiate different card strategies in CardsService and StacksService.

Changes

  • CardStrategyFactory.cs: New class created to encapsulate card strategy creation logic.
  • ICardStrategyFactory.cs: New interface defined for obtaining card strategies.
  • CardsService.cs: Refactored to use ICardStrategyFactory for strategy instantiation.
  • StacksService.cs: Updated to utilize ICardStrategyFactory for managing card strategies.
  • CardsServiceTests.cs and StacksServiceTests.cs: Tests modified to use the new factory for obtaining card strategies

Updated `CardStrategyFactory` to implement the `ICardStrategyFactory` interface, providing methods to retrieve card strategies for individual cards and stacks. The constructor now accepts `ICardsRepository` and `IUserInteractionService` dependencies. Added `GetCardStrategyForStack` method for stack-based strategies. Created or updated `ICardStrategyFactory` interface to define method signatures, ensuring a consistent contract for obtaining card strategies.
Updated `CardsService` and `StacksService` to use a new `ICardStrategyFactory` dependency for dynamic card strategy creation. This refactor simplifies the strategy selection logic by utilizing factory methods, enhancing code maintainability. The `StacksService` constructor no longer requires an `ICardsRepository`, indicating a shift in responsibility for card strategy management.
Updated `Program.cs` to include a new using directive for `Flashcards.Services.CardStrategies` to improve organization and access to card strategy implementations. Added a new service registration for `ICardStrategyFactory`, allowing for dynamic creation of card strategies during runtime.
This commit introduces an `ICardStrategyFactory` dependency in both `CardsServiceTests.cs` and `StacksServiceTests.cs`. The factory is utilized to retrieve specific card strategies based on the card type, enhancing the flexibility of handling various card types such as Flashcards, Multiple Choice, and Cloze.

Test methods in `CardsServiceTests.cs` and `StacksServiceTests.cs` have been updated to leverage the new factory, ensuring the correct strategy is returned during testing. Additionally, return values of mocked methods have been adjusted to align with the new strategy implementations, improving the accuracy of the tests.
@Dejmenek Dejmenek linked an issue Oct 27, 2025 that may be closed by this pull request
@Dejmenek
Dejmenek merged commit f0ccf51 into main Oct 27, 2025
2 checks passed
@Dejmenek
Dejmenek deleted the 51-refactor-card-strategy-instantiation-to-use-abstract-factory-pattern branch October 27, 2025 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor Card Strategy Instantiation to Use Abstract Factory Pattern

1 participant