...

/

Solution: Create an Environment-Specific Database Client Factory

Solution: Create an Environment-Specific Database Client Factory

Use a map-based factory to return the correct client class based on the environment, eliminating the need for conditionals.

We'll cover the following...

Solution explanation

  • Lines 1–11: We define two database client classes: FakeDbClient and PostgresClient.

    • FakeDbClient simulates a lightweight in-memory database, used in development.

    • PostgresClient represents a production-grade client that connects to a real database.

    • Both expose the same .query(sql) interface, which is key for substitutability.

  • Lines 13–16: We create ...