Skip to content

Commit 0024ff8

Browse files
committed
Added Factory pattern
1 parent 719c33d commit 0024ff8

16 files changed

+621
-4
lines changed

Assets/Patterns/20. Decorator/Tesla order system/Car parts/Extras/DracoThrusters.cs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Decorator.OrderSystem
66
{
7+
//Tesla is actually planning to put SpaceX Draco Thrusters on the Roadster to increase the performance so this class is not a lie!
78
public class DracoThruster : _CarExtras
89
{
910
private _Car prevCarPart;

Assets/Patterns/20. Decorator/Tesla order system/decorator-order-system.unity

+45-2
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,51 @@ Transform:
211211
m_LocalScale: {x: 1, y: 1, z: 1}
212212
m_Children: []
213213
m_Father: {fileID: 0}
214-
m_RootOrder: 1
214+
m_RootOrder: 2
215215
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
216+
--- !u!1 &167023543
217+
GameObject:
218+
m_ObjectHideFlags: 0
219+
m_CorrespondingSourceObject: {fileID: 0}
220+
m_PrefabInstance: {fileID: 0}
221+
m_PrefabAsset: {fileID: 0}
222+
serializedVersion: 6
223+
m_Component:
224+
- component: {fileID: 167023545}
225+
- component: {fileID: 167023544}
226+
m_Layer: 0
227+
m_Name: _Controller
228+
m_TagString: Untagged
229+
m_Icon: {fileID: 0}
230+
m_NavMeshLayer: 0
231+
m_StaticEditorFlags: 0
232+
m_IsActive: 1
233+
--- !u!114 &167023544
234+
MonoBehaviour:
235+
m_ObjectHideFlags: 0
236+
m_CorrespondingSourceObject: {fileID: 0}
237+
m_PrefabInstance: {fileID: 0}
238+
m_PrefabAsset: {fileID: 0}
239+
m_GameObject: {fileID: 167023543}
240+
m_Enabled: 1
241+
m_EditorHideFlags: 0
242+
m_Script: {fileID: 11500000, guid: 1f7a2dc5884308b4a8e27e642c4e5484, type: 3}
243+
m_Name:
244+
m_EditorClassIdentifier:
245+
--- !u!4 &167023545
246+
Transform:
247+
m_ObjectHideFlags: 0
248+
m_CorrespondingSourceObject: {fileID: 0}
249+
m_PrefabInstance: {fileID: 0}
250+
m_PrefabAsset: {fileID: 0}
251+
m_GameObject: {fileID: 167023543}
252+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
253+
m_LocalPosition: {x: 0, y: 0, z: 0}
254+
m_LocalScale: {x: 1, y: 1, z: 1}
255+
m_Children: []
256+
m_Father: {fileID: 0}
257+
m_RootOrder: 0
258+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
216259
--- !u!1 &1756844989
217260
GameObject:
218261
m_ObjectHideFlags: 0
@@ -294,5 +337,5 @@ Transform:
294337
m_LocalScale: {x: 1, y: 1, z: 1}
295338
m_Children: []
296339
m_Father: {fileID: 0}
297-
m_RootOrder: 0
340+
m_RootOrder: 1
298341
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

Assets/Patterns/21. Factory.meta

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

Assets/Patterns/21. Factory/Car Factory.meta

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

Assets/Patterns/21. Factory/Car Factory/Car Factories.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,52 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using Decorator.OrderSystem;
4+
using UnityEngine;
5+
6+
namespace Factory.CarFactory
7+
{
8+
public class ChinaFactory : _CarFactory
9+
{
10+
public override _Car ManufactureCar(CarModels carModel, List<CarExtras> carExtras)
11+
{
12+
_Car car = null;
13+
14+
if (carModel == CarModels.ModelS)
15+
{
16+
car = new ModelS();
17+
}
18+
else if (carModel == CarModels.Roadster)
19+
{
20+
car = new Roadster();
21+
}
22+
23+
//Notice that the Cybertruck is missing here, so we cant manufacture it!
24+
if (car == null)
25+
{
26+
Debug.Log("Sorry but this factory cant manufacture this model :(");
27+
28+
return car;
29+
}
30+
31+
32+
//Add the extras
33+
foreach (CarExtras carExtra in carExtras)
34+
{
35+
if (carExtra == CarExtras.DracoThruster)
36+
{
37+
car = new DracoThruster(car, 1);
38+
}
39+
else if (carExtra == CarExtras.EjectionSeat)
40+
{
41+
car = new EjectionSeat(car, 1);
42+
}
43+
else
44+
{
45+
Debug.Log("Sorry but this factory cant add this car extra :(");
46+
}
47+
}
48+
49+
return car;
50+
}
51+
}
52+
}

Assets/Patterns/21. Factory/Car Factory/Car Factories/ChinaFactory.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,52 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using Decorator.OrderSystem;
4+
using UnityEngine;
5+
6+
namespace Factory.CarFactory
7+
{
8+
public class USFactory : _CarFactory
9+
{
10+
public override _Car ManufactureCar(CarModels carModel, List<CarExtras> carExtras)
11+
{
12+
_Car car = null;
13+
14+
if (carModel == CarModels.Cybertruck)
15+
{
16+
car = new Cybertruck();
17+
}
18+
else if (carModel == CarModels.ModelS)
19+
{
20+
car = new ModelS();
21+
}
22+
else if (carModel == CarModels.Roadster)
23+
{
24+
car = new Roadster();
25+
}
26+
27+
if (car == null)
28+
{
29+
Debug.Log("Sorry but this factory cant manufacture this model :(");
30+
31+
return car;
32+
}
33+
34+
35+
//Add the extras
36+
foreach (CarExtras carExtra in carExtras)
37+
{
38+
if (carExtra == CarExtras.DracoThruster)
39+
{
40+
car = new DracoThruster(car, 1);
41+
}
42+
//Ejection seats are not legal in US so cant manufacture it (but we will still manufacture the rest of the car)
43+
else
44+
{
45+
Debug.Log("Sorry but this factory cant add this car extra :(");
46+
}
47+
}
48+
49+
return car;
50+
}
51+
}
52+
}

Assets/Patterns/21. Factory/Car Factory/Car Factories/USFactory.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,13 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using Decorator.OrderSystem;
5+
6+
namespace Factory.CarFactory
7+
{
8+
public abstract class _CarFactory
9+
{
10+
//This method is called the Factory Method
11+
public abstract _Car ManufactureCar(CarModels carModel, List<CarExtras> carExtras);
12+
}
13+
}

Assets/Patterns/21. Factory/Car Factory/Car Factories/_CarFactory.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,63 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using Factory.CarFactory;
5+
using Decorator.OrderSystem;
6+
7+
//Example of the Factory Programming Pattern
8+
//Uses classes from Decorator pattern order system example
9+
public class CarFactoryController : MonoBehaviour
10+
{
11+
12+
void Start()
13+
{
14+
//The factories we can choose from
15+
_CarFactory US_Factory = new USFactory();
16+
17+
_CarFactory China_Factory = new ChinaFactory();
18+
19+
20+
//Manufacture cars
21+
_Car order1 = US_Factory.ManufactureCar(CarModels.ModelS, new List<CarExtras>() { CarExtras.DracoThruster });
22+
23+
FinalizeOrder(order1);
24+
25+
26+
_Car order2 = China_Factory.ManufactureCar(CarModels.Cybertruck, new List<CarExtras>() { CarExtras.DracoThruster });
27+
28+
FinalizeOrder(order2);
29+
30+
31+
_Car order3 = US_Factory.ManufactureCar(CarModels.Roadster, new List<CarExtras>() { CarExtras.DracoThruster, CarExtras.EjectionSeat, CarExtras.DracoThruster });
32+
33+
FinalizeOrder(order3);
34+
}
35+
36+
37+
38+
private void FinalizeOrder(_Car finishedCar)
39+
{
40+
if (finishedCar == null)
41+
{
42+
Debug.Log($"Sorry but we cant manufacture your order, please try again!");
43+
}
44+
else
45+
{
46+
Debug.Log($"Your order: {finishedCar.GetDescription()} is ready for delivery as soon as you pay ${finishedCar.Cost()}");
47+
}
48+
}
49+
}
50+
51+
52+
public enum CarModels
53+
{
54+
ModelS,
55+
Roadster,
56+
Cybertruck
57+
}
58+
59+
public enum CarExtras
60+
{
61+
EjectionSeat,
62+
DracoThruster
63+
}

Assets/Patterns/21. Factory/Car Factory/CarFactoryController.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)