Skip to content

Commit 96f5876

Browse files
committed
Added code to illustrate Flyweight pattern by sharing data
1 parent 6876399 commit 96f5876

File tree

11 files changed

+487
-0
lines changed

11 files changed

+487
-0
lines changed

Assets/Patterns/2. Flyweight.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/2. Flyweight/Data.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Flyweight
6+
{
7+
//Data that can be shared across all objects
8+
public class Data
9+
{
10+
private double number1 = 5d;
11+
private double number2 = 5d;
12+
private double number3 = 5d;
13+
private double number4 = 5d;
14+
private double number5 = 5d;
15+
private double number6 = 5d;
16+
private double number7 = 5d;
17+
private double number8 = 5d;
18+
private double number9 = 5d;
19+
private double number10 = 5d;
20+
private double number11 = 5d;
21+
private double number12 = 5d;
22+
private double number13 = 5d;
23+
private double number14 = 5d;
24+
private double number15 = 5d;
25+
private double number16 = 5d;
26+
private double number17 = 5d;
27+
private double number18 = 5d;
28+
private double number19 = 5d;
29+
private double number20 = 5d;
30+
}
31+
}

Assets/Patterns/2. Flyweight/Data.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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Flyweight
6+
{
7+
public class Flyweight
8+
{
9+
//Data for each individual object
10+
private float health;
11+
12+
//This is the data that's being shared among all objects so you have to inject it in the constructor
13+
private Data data;
14+
15+
16+
public Flyweight(Data data)
17+
{
18+
health = Random.Range(10f, 100f);
19+
20+
this.data = data;
21+
}
22+
}
23+
}

Assets/Patterns/2. Flyweight/Flyweight.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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Flyweight
6+
{
7+
//Illustrates the flyweight pattern
8+
//Open the profiler and click on Memory to see how much memory is being used
9+
//Switch between Heavy and Flyweight to compare and you should see that the difference is several hundred megabytes even though the data in this case is just 20 doubles
10+
public class FlyweightController : MonoBehaviour
11+
{
12+
private List<Heavy> heavyObjects = new List<Heavy>();
13+
14+
private List<Flyweight> flyweightObjects = new List<Flyweight>();
15+
16+
17+
void Start()
18+
{
19+
int numberOfObjects = 1000000;
20+
21+
22+
//Generate Heavy objects that doesn't share any data
23+
for (int i = 0; i < numberOfObjects; i++)
24+
{
25+
Heavy newHeavy = new Heavy();
26+
27+
heavyObjects.Add(newHeavy);
28+
}
29+
30+
31+
//Generate Flyweight objects
32+
33+
//Generate the data that's being shared among all objects
34+
//Data data = new Data();
35+
36+
//for (int i = 0; i < numberOfObjects; i++)
37+
//{
38+
// Flyweight newFlyweight = new Flyweight(data);
39+
40+
// flyweightObjects.Add(newFlyweight);
41+
//}
42+
}
43+
}
44+
}

Assets/Patterns/2. Flyweight/FlyweightController.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/2. Flyweight/Heavy.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Flyweight
6+
{
7+
//This class doesn't share any data among all objects
8+
public class Heavy
9+
{
10+
private float health;
11+
12+
private Data data;
13+
14+
15+
public Heavy()
16+
{
17+
health = Random.Range(10f, 100f);
18+
19+
data = new Data();
20+
}
21+
}
22+
}

Assets/Patterns/2. Flyweight/Heavy.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)