Skip to content

Commit 3301586

Browse files
committed
Added Template pattern
1 parent 78d8f8b commit 3301586

14 files changed

+615
-1
lines changed

Assets/Patterns/22. Template.meta

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

Assets/Patterns/22. Template/Assemble cars.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,28 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Template.AssembleCars
6+
{
7+
//Example of the Template Programming Pattern
8+
//Is using some code from the Factory pattern
9+
public class AssembleCarsController : MonoBehaviour
10+
{
11+
private void Start()
12+
{
13+
//Init the assembly lines
14+
AssembleCybertruck cybertruck = new AssembleCybertruck();
15+
16+
AssembleModelS modelS = new AssembleModelS();
17+
18+
19+
//Assemble different orders
20+
cybertruck.AssembleCar(new List<CarExtras>() { CarExtras.DracoThruster, CarExtras.EjectionSeat });
21+
22+
modelS.AssembleCar(new List<CarExtras>() { CarExtras.DracoThruster, CarExtras.EjectionSeat, CarExtras.EjectionSeat });
23+
24+
modelS.AssembleCar();
25+
}
26+
27+
}
28+
}

Assets/Patterns/22. Template/Assemble cars/AssembleCarsController.cs.meta

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

Assets/Patterns/22. Template/Assemble cars/Assembly line.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,36 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Template.AssembleCars
6+
{
7+
public class AssembleCybertruck : _AssemblyLine
8+
{
9+
protected override void GetCarBase()
10+
{
11+
Debug.Log("Get Cybertruck base");
12+
}
13+
14+
protected override void GetCarBattery()
15+
{
16+
Debug.Log("Get Cybertruck battery");
17+
}
18+
19+
protected override void GetCarBody()
20+
{
21+
Debug.Log("Get Cybertruck body");
22+
}
23+
24+
protected override void GetWheels()
25+
{
26+
Debug.Log("Get Cybertruck wheels");
27+
}
28+
29+
protected override bool CanManuFactureCar()
30+
{
31+
Debug.Log("Sorry but the Cybertruck is still a prototype so we can't manufacture it!");
32+
33+
return false;
34+
}
35+
}
36+
}

Assets/Patterns/22. Template/Assemble cars/Assembly line/AssembleCybertruck.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,29 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Template.AssembleCars
6+
{
7+
public class AssembleModelS : _AssemblyLine
8+
{
9+
protected override void GetCarBase()
10+
{
11+
Debug.Log("Get Model S base");
12+
}
13+
14+
protected override void GetCarBattery()
15+
{
16+
Debug.Log("Get Model S battery");
17+
}
18+
19+
protected override void GetCarBody()
20+
{
21+
Debug.Log("Get Model S body");
22+
}
23+
24+
protected override void GetWheels()
25+
{
26+
Debug.Log("Get Model S wheels");
27+
}
28+
}
29+
}

Assets/Patterns/22. Template/Assemble cars/Assembly line/AssembleModelS.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,84 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using Factory.CarFactory;
5+
6+
namespace Template.AssembleCars
7+
{
8+
//This is the Template class, which includes methods for how to assemble a car
9+
public abstract class _AssemblyLine
10+
{
11+
//This is the template itself we want all children to use
12+
//A problem is that a child can hide this method because there's no way to prevent that in C#
13+
//Java has the final keyword that prevents that...
14+
public void AssembleCar(List<CarExtras> carExtras = null)
15+
{
16+
InitAssemblyProcess();
17+
18+
if (!CanManuFactureCar())
19+
{
20+
return;
21+
}
22+
23+
GetCarBase();
24+
GetCarBattery();
25+
GetCarBody();
26+
27+
CoffeeBreak();
28+
29+
GetWheels();
30+
GetCarExtras(carExtras);
31+
}
32+
33+
34+
//It can define concrete methods
35+
protected void CoffeeBreak()
36+
{
37+
Debug.Log("Take a coffee break");
38+
}
39+
40+
protected void InitAssemblyProcess()
41+
{
42+
Debug.Log("");
43+
}
44+
45+
protected void GetCarExtras(List<CarExtras> carExtras)
46+
{
47+
if (carExtras == null)
48+
{
49+
Debug.Log("This car comes with no extras");
50+
51+
return;
52+
}
53+
54+
foreach (CarExtras extra in carExtras)
55+
{
56+
if (extra == CarExtras.DracoThruster)
57+
{
58+
Debug.Log("Get Draco Thruster");
59+
}
60+
else if (extra == CarExtras.EjectionSeat)
61+
{
62+
Debug.Log("Get Ejection Seat");
63+
}
64+
}
65+
}
66+
67+
68+
//..abstract methods
69+
protected abstract void GetCarBody();
70+
71+
protected abstract void GetCarBase();
72+
73+
protected abstract void GetCarBattery();
74+
75+
protected abstract void GetWheels();
76+
77+
78+
//...and hooks which is a method the child can override if needed
79+
protected virtual bool CanManuFactureCar()
80+
{
81+
return true;
82+
}
83+
}
84+
}

Assets/Patterns/22. Template/Assemble cars/Assembly line/_AssemblyLine.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)