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.
We'll cover the following...
We'll cover the following...
Solution explanation
Lines 1–16: We define two interchangeable service classes:
AnalyticsServiceandStubService.AnalyticsServicelogs events and generates reports when analytics is enabled.StubServiceprovides empty.track()and.report()methods so calls remain safe even when analytics is disabled.
Lines 18–21: The
serviceMapobject maps theanalyticsEnabled...