Search⌘ K
AI Features

Solution: Create a Feature-Flagged Analytics Service

Understand how to implement a factory that produces either a real or stub analytics service based on feature flags, ensuring safe method calls regardless of analytics availability.

Solution explanation

  • Lines 1–16: We define two interchangeable service classes: AnalyticsService and StubService.

    • AnalyticsService logs events and generates reports when analytics is enabled.

    • StubService provides empty .track() and .report() methods so calls remain safe even when analytics is disabled.

  • Lines 18–21: The serviceMap object maps the analyticsEnabled ...