Skip to content

Instantly share code, notes, and snippets.

@PoisonousJohn
Last active February 20, 2018 16:03
Show Gist options
  • Select an option

  • Save PoisonousJohn/6d1b3733e372239902ac5de07e22ef2c to your computer and use it in GitHub Desktop.

Select an option

Save PoisonousJohn/6d1b3733e372239902ac5de07e22ef2c to your computer and use it in GitHub Desktop.
Example of static-class locator anti-pattern
public static class Services
{
public static AdService ads { get; set; }
public static APIService api { get; set; }
// tons of other services
public static AnalyticsService analytics { get; set; }
}
public class Loader : MonoBehaviour {
private void Awake()
{
Services.api = new APIService();
Services.api.Login(); // Services.api.UserId is available only after login operation succeeds
StartCoroutine(InitAnalytics());
// if anybody calls Services.analytics
// before it's initialized, you get NRE exception
}
private IEnumerator InitAnalytics()
{
while (!Services.api.isAuthorized) {
yield return null;
}
Services.analytics = new AnalyticsService(Services.api.UserId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment