Last active
February 20, 2018 16:03
-
-
Save PoisonousJohn/6d1b3733e372239902ac5de07e22ef2c to your computer and use it in GitHub Desktop.
Example of static-class locator anti-pattern
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
| 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