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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
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

Lines 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)