Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 75a5706

Browse filesBrowse files
Implementation of the Service Locator pattern is added.
1 parent 3301586 commit 75a5706
Copy full SHA for 75a5706
Expand file treeCollapse file tree

14 files changed

+666
-0
lines changed

‎Assets/Patterns/16. Service Locator.meta

Copy file name to clipboardExpand all lines: Assets/Patterns/16. Service Locator.meta
+8Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace ServiceLocatorPattern
2+
{
3+
using UnityEngine;
4+
5+
public class GameController : MonoBehaviour
6+
{
7+
private void Start()
8+
{
9+
var firstService = ServiceLocator.Resolve<FirstService>();
10+
var secondService = ServiceLocator.Resolve<SecondService>();
11+
var thirdService = ServiceLocator.Resolve<ThirdService>();
12+
13+
firstService?.SayHi();
14+
secondService?.SimpleMethod();
15+
thirdService?.Foo();
16+
}
17+
}
18+
}

‎Assets/Patterns/16. Service Locator/GameController.cs.meta

Copy file name to clipboardExpand all lines: Assets/Patterns/16. Service Locator/GameController.cs.meta
+11Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace ServiceLocatorPattern
5+
{
6+
public static class ServiceLocator
7+
{
8+
private static readonly HashSet<object> _registeredObjects;
9+
10+
static ServiceLocator()
11+
{
12+
_registeredObjects = new HashSet<object>();
13+
}
14+
15+
public static void Register<T>(T obj) where T : class
16+
{
17+
_registeredObjects.Add(obj);
18+
}
19+
20+
public static void Unregister<T>(T obj) where T : class
21+
{
22+
_registeredObjects.Remove(obj);
23+
}
24+
25+
public static T Resolve<T>() where T : class
26+
{
27+
var obj = _registeredObjects.SingleOrDefault(x => x is T);
28+
if (obj != null)
29+
return obj as T;
30+
31+
return null;
32+
}
33+
}
34+
}

‎Assets/Patterns/16. Service Locator/ServiceLocator.cs.meta

Copy file name to clipboardExpand all lines: Assets/Patterns/16. Service Locator/ServiceLocator.cs.meta
+11Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.