Skip to content

Commit 8973073

Browse files
committed
Added the service locator pattern
1 parent 911e92f commit 8973073

17 files changed

+564
-0
lines changed

Assets/Patterns/15. Service Locator.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Patterns/15. Service Locator/Audio service locator.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Patterns/15. Service Locator/Audio service locator/Scripts.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace ServiceLocator.AudioService
6+
{
7+
//Implementation of the Service Locator example from the book Game Programming Patters
8+
//This class used to test that the implementation is working
9+
public class GameController : MonoBehaviour
10+
{
11+
12+
void Start()
13+
{
14+
//Register the service provider in the Locator
15+
ConsoleAudio audio = new ConsoleAudio();
16+
17+
Locator.Provide(audio);
18+
//Locator.Provide(null);
19+
}
20+
21+
22+
void Update()
23+
{
24+
Audio audio = Locator.GetAudio();
25+
26+
if (Input.GetKeyDown(KeyCode.P))
27+
{
28+
audio.PlaySound(23);
29+
}
30+
else if (Input.GetKeyDown(KeyCode.S))
31+
{
32+
audio.StopSound(23);
33+
}
34+
else if (Input.GetKeyDown(KeyCode.Space))
35+
{
36+
audio.StopAllSounds();
37+
}
38+
}
39+
}
40+
}

Assets/Patterns/15. Service Locator/Audio service locator/Scripts/GameController.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Patterns/15. Service Locator/Audio service locator/Scripts/Service Locator.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace ServiceLocator.AudioService
6+
{
7+
//Service provider parent class
8+
//This is the abstract class that determines which methods the service will be exposing
9+
public abstract class Audio
10+
{
11+
public abstract void PlaySound(int soundID);
12+
13+
public abstract void StopSound(int soundID);
14+
15+
public abstract void StopAllSounds();
16+
}
17+
}

Assets/Patterns/15. Service Locator/Audio service locator/Scripts/Service Locator/Audio.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace ServiceLocator.AudioService
6+
{
7+
//Service provider child class
8+
public class ConsoleAudio : Audio
9+
{
10+
public override void PlaySound(int soundID)
11+
{
12+
Debug.Log($"Sound {soundID} has started");
13+
}
14+
15+
public override void StopSound(int soundID)
16+
{
17+
Debug.Log($"Sound {soundID} has stopped");
18+
}
19+
20+
public override void StopAllSounds()
21+
{
22+
Debug.Log($"All sounds have stopped");
23+
}
24+
}
25+
}

Assets/Patterns/15. Service Locator/Audio service locator/Scripts/Service Locator/ConsoleAudio.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace ServiceLocator.AudioService
6+
{
7+
//This is the service locator
8+
public class Locator
9+
{
10+
private static NullAudio nullService;
11+
12+
private static Audio service;
13+
14+
15+
static Locator()
16+
{
17+
nullService = new NullAudio();
18+
19+
//Init with null in case we forget to inject a reference to Audio
20+
service = nullService;
21+
}
22+
23+
24+
//Does the locating
25+
public static Audio GetAudio()
26+
{
27+
return service;
28+
}
29+
30+
31+
//Use dependency injection to get a reference to the audio service we need
32+
//Call this method before you do anything else with the audio
33+
public static void Provide(Audio _service)
34+
{
35+
//Sometimes we want to set it to null if we want to disable audio in the game
36+
if (_service == null)
37+
{
38+
service = nullService;
39+
}
40+
else
41+
{
42+
service = _service;
43+
}
44+
}
45+
}
46+
}

Assets/Patterns/15. Service Locator/Audio service locator/Scripts/Service Locator/Locator.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace ServiceLocator.AudioService
6+
{
7+
//Service provider child class
8+
//Used if the service locator hasnt been given a reference to a service provider or if we dont want to play audio
9+
public class NullAudio : Audio
10+
{
11+
public override void PlaySound(int soundID)
12+
{
13+
Debug.Log("Do nothing");
14+
}
15+
16+
public override void StopSound(int soundID)
17+
{
18+
Debug.Log("Do nothing");
19+
}
20+
21+
public override void StopAllSounds()
22+
{
23+
Debug.Log("Do nothing");
24+
}
25+
}
26+
}

Assets/Patterns/15. Service Locator/Audio service locator/Scripts/Service Locator/NullAudio.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)