Skip to content

Commit 2be8e64

Browse files
committed
Added example of the Facade pattern
1 parent 785977a commit 2be8e64

17 files changed

+611
-4
lines changed

Assets/Patterns/22. Facade.meta

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

Assets/Patterns/22. Facade/Random numbers.meta

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

Assets/Patterns/22. Facade/Random numbers/Facade.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,42 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Facade.RandomNumbers
6+
{
7+
public class RandomNumberFacade
8+
{
9+
private static IRandomNumberGenerator rng;
10+
11+
12+
static RandomNumberFacade()
13+
{
14+
//rng = new RandomNumbersNative();
15+
rng = new RandomNumbersUnity();
16+
}
17+
18+
19+
public static void InitSeed(int seed)
20+
{
21+
rng.InitSeed(seed);
22+
}
23+
24+
25+
public static float GetRandom01()
26+
{
27+
return rng.GetRandom01();
28+
}
29+
30+
31+
public static float GetRandom(float min, float max)
32+
{
33+
return rng.GetRandom(min, max);
34+
}
35+
36+
37+
public static int GetRandomInt(int min, int max)
38+
{
39+
return rng.GetRandomInt(min, max);
40+
}
41+
}
42+
}

Assets/Patterns/22. Facade/Random numbers/Facade/RandomNumberFacade.cs.meta

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

Assets/Patterns/22. Facade/Random numbers/Facade/RandomNumberGenerators.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 Facade.RandomNumbers
6+
{
7+
public interface IRandomNumberGenerator
8+
{
9+
public void InitSeed(int seed);
10+
11+
public float GetRandom01();
12+
13+
public float GetRandom(float min, float max);
14+
15+
public int GetRandomInt(int min, int max);
16+
}
17+
}

Assets/Patterns/22. Facade/Random numbers/Facade/RandomNumberGenerators/IRandomNumberGenerator.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,37 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Facade.RandomNumbers
6+
{
7+
//Generate random numbers by using C#'s System.Random
8+
public class RandomNumbersNative : IRandomNumberGenerator
9+
{
10+
private System.Random rng;
11+
12+
public RandomNumbersNative()
13+
{
14+
rng = new System.Random();
15+
}
16+
17+
public float GetRandom(float min, float max)
18+
{
19+
return (float)((rng.NextDouble() * (max - min)) + min);
20+
}
21+
22+
public float GetRandom01()
23+
{
24+
return (float)rng.NextDouble();
25+
}
26+
27+
public int GetRandomInt(int min, int max)
28+
{
29+
return rng.Next(min, max);
30+
}
31+
32+
public void InitSeed(int seed)
33+
{
34+
rng = new System.Random(seed);
35+
}
36+
}
37+
}

Assets/Patterns/22. Facade/Random numbers/Facade/RandomNumberGenerators/RandomNumbersNative.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,30 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Facade.RandomNumbers
6+
{
7+
//Geerate random numbers by using Unity's Random.Range
8+
public class RandomNumbersUnity : IRandomNumberGenerator
9+
{
10+
public float GetRandom(float min, float max)
11+
{
12+
return Random.Range(min, max);
13+
}
14+
15+
public float GetRandom01()
16+
{
17+
return Random.Range(0f, 1f);
18+
}
19+
20+
public int GetRandomInt(int min, int max)
21+
{
22+
return Random.Range(min, max);
23+
}
24+
25+
public void InitSeed(int seed)
26+
{
27+
Random.InitState(seed);
28+
}
29+
}
30+
}

Assets/Patterns/22. Facade/Random numbers/Facade/RandomNumberGenerators/RandomNumbersUnity.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,37 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using Facade.RandomNumbers;
5+
6+
//Example of the Facade programming pattern (and possibly Adapter because they are very similar)
7+
public class RandomNumbersController : MonoBehaviour
8+
{
9+
private void Start()
10+
{
11+
RandomNumberFacade.InitSeed(0);
12+
13+
Debug.Log("Float: 0 -> 1");
14+
15+
for (int i = 0; i < 5; i++)
16+
{
17+
Debug.Log(RandomNumberFacade.GetRandom01());
18+
}
19+
20+
Debug.Log("Float: -1 -> 2");
21+
22+
for (int i = 0; i < 10; i++)
23+
{
24+
Debug.Log(RandomNumberFacade.GetRandom(-1f, 2f));
25+
}
26+
27+
Debug.Log("Integer: -10 -> 20");
28+
29+
for (int i = 0; i < 10; i++)
30+
{
31+
Debug.Log(RandomNumberFacade.GetRandomInt(-10, 21));
32+
}
33+
}
34+
35+
36+
37+
}

Assets/Patterns/22. Facade/Random numbers/RandomNumbersController.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)