-
Notifications
You must be signed in to change notification settings - Fork 10
System design flaw study #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tusharchou
wants to merge
1
commit into
tusharchou:main
Choose a base branch
from
DataDesignCode:docs/factory-pattern-proposal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,82 @@ | ||||||
| # Design Flaw: Inconsistent Object Creation and the Factory Pattern Solution | ||||||
|
|
||||||
| This document outlines a significant design flaw in our codebase related to object creation and proposes a solution using the **Factory Method** design pattern. | ||||||
|
|
||||||
| ## The Design Flaw: Scattered and Inconsistent Creation Logic | ||||||
|
|
||||||
| Currently, the instantiation of complex objects, particularly different types of "Environments" (e.g., `SingleAlfredTWEnv`, `ReformatWordleEnvironment`, `ScienceWorldEnv`), is handled by multiple, disparate `create` methods. | ||||||
|
|
||||||
| This approach has several critical disadvantages: | ||||||
|
|
||||||
| * **Violates the Open/Closed Principle:** Whenever a new environment type is introduced, we must modify existing client code or a central creator class. This means our system is not "closed for modification" but requires constant changes for extension. | ||||||
| * **Reduced Cohesion and Increased Coupling:** The logic for creating objects is scattered across the codebase. The client code that needs to create an object is tightly coupled to the concrete classes it needs to instantiate, as it must know which specific class or creation function to call. This makes the code harder to read, maintain, and test. | ||||||
| * **Inconsistency:** The presence of different `create` methods, including one that uses dynamic `__import__`, leads to an inconsistent and unpredictable API for object creation. | ||||||
|
|
||||||
| ## The Solution: The Factory Method Pattern | ||||||
|
|
||||||
| To address these issues, we should adopt the **Factory Method** design pattern. This pattern defines a common interface for creating objects but lets subclasses decide which specific class to instantiate. | ||||||
|
|
||||||
| ### How It Works | ||||||
|
|
||||||
| 1. **Define a `Creator` Base Class:** We will create an abstract base class with a "factory method" for producing objects. | ||||||
|
|
||||||
| ```python | ||||||
| from abc import ABC, abstractmethod | ||||||
|
|
||||||
| class EnvironmentCreator(ABC): | ||||||
| """ | ||||||
| Declares the factory method that returns an object of an Environment class. | ||||||
| """ | ||||||
| @abstractmethod | ||||||
| def create_environment(self): | ||||||
| pass | ||||||
|
|
||||||
| # (Your environment base class would be defined elsewhere) | ||||||
| class Environment(ABC): | ||||||
| pass | ||||||
| ``` | ||||||
|
|
||||||
| 2. **Create Concrete `Creator` Subclasses:** For each type of environment, we will create a concrete subclass that implements the factory method. All the complex setup logic for a specific environment is encapsulated within its creator. | ||||||
|
|
||||||
| ```python | ||||||
| class ScienceWorldCreator(EnvironmentCreator): | ||||||
| """ | ||||||
| Overrides the factory method to return an instance of a ScienceWorldEnv. | ||||||
| """ | ||||||
| def create_environment(self) -> Environment: | ||||||
| return ScienceWorldEnv() | ||||||
|
|
||||||
| class WordleCreator(EnvironmentCreator): | ||||||
| """ | ||||||
| Overrides the factory method to return an instance of a WordleEnv. | ||||||
| """ | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docstring is slightly misleading. It states that the method returns an instance of
Suggested change
|
||||||
| def create_environment(self) -> Environment: | ||||||
| # Encapsulate complex setup logic here | ||||||
| vocab = Vocabulary.from_file("...") | ||||||
| return ReformatWordleEnvironment(WordleEnvironment(vocab)) | ||||||
| ``` | ||||||
|
|
||||||
| 3. **Use the Factory in Client Code:** The client code will now work with the `EnvironmentCreator` interface, completely decoupled from the concrete environment classes. | ||||||
|
|
||||||
| ```python | ||||||
| def client_code(creator: EnvironmentCreator): | ||||||
| """ | ||||||
| The client code works with any creator's subclass through the base interface. | ||||||
| """ | ||||||
| env = creator.create_environment() | ||||||
| # ... do something with the environment | ||||||
| print(f"Created a {type(env).__name__}") | ||||||
|
|
||||||
|
|
||||||
| # Example usage | ||||||
| client_code(ScienceWorldCreator()) | ||||||
| client_code(WordleCreator()) | ||||||
| ``` | ||||||
|
|
||||||
| ### Benefits of This Approach | ||||||
|
|
||||||
| * **Adherence to SOLID Principles:** This change aligns our code with the Open/Closed and Dependency Inversion principles. | ||||||
| * **Improved Maintainability:** New environments can be added without changing existing code. We simply add a new `Environment` subclass and a corresponding `Creator` subclass. | ||||||
| * **Increased Flexibility and Decoupling:** The client code is no longer responsible for knowing how to create objects. This makes our system more modular and easier to refactor or test. | ||||||
|
|
||||||
| By implementing the Factory Method pattern, we will establish a clean, scalable, and maintainable architecture for object creation in our platform. | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better type safety and clarity, it's good practice to include a return type hint in the abstract method signature. Since the concrete implementations of
create_environmentreturn anEnvironmentinstance, the base method should declare this. Because theEnvironmentclass is defined afterEnvironmentCreator, you should use a forward reference (a string) for the type hint.