Skip to content

Commit 719c33d

Browse files
committed
Added decorator pattern
1 parent aa68219 commit 719c33d

25 files changed

+648
-2
lines changed

Assets/Patterns/20. Decorator.meta

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

Assets/Patterns/20. Decorator/Tesla order system.meta

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

Assets/Patterns/20. Decorator/Tesla order system/Car parts.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,18 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Decorator.OrderSystem
6+
{
7+
public class Cybertruck : _Car
8+
{
9+
public Cybertruck()
10+
{
11+
this.description = "Cybertruck";
12+
}
13+
14+
public override string GetDescription() => this.description;
15+
16+
public override float Cost() => PriceList.cybertruck;
17+
}
18+
}

Assets/Patterns/20. Decorator/Tesla order system/Car parts/Cybertruck.cs.meta

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

Assets/Patterns/20. Decorator/Tesla order system/Car parts/Extras.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,24 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Decorator.OrderSystem
6+
{
7+
public class DracoThruster : _CarExtras
8+
{
9+
private _Car prevCarPart;
10+
11+
12+
public DracoThruster(_Car prevCarPart, int howMany)
13+
{
14+
this.prevCarPart = prevCarPart;
15+
this.howMany = howMany;
16+
}
17+
18+
19+
public override string GetDescription() => $"{prevCarPart.GetDescription()}, {howMany} Draco Thruster";
20+
21+
22+
public override float Cost() => (PriceList.dracoThruster * howMany) + prevCarPart.Cost();
23+
}
24+
}

Assets/Patterns/20. Decorator/Tesla order system/Car parts/Extras/DracoThrusters.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,24 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Decorator.OrderSystem
6+
{
7+
public class EjectionSeat : _CarExtras
8+
{
9+
private _Car prevCarPart;
10+
11+
12+
public EjectionSeat(_Car prevCarPart, int howMany)
13+
{
14+
this.prevCarPart = prevCarPart;
15+
this.howMany = howMany;
16+
}
17+
18+
19+
public override string GetDescription() => $"{prevCarPart.GetDescription()}, {howMany} Ejection Seat";
20+
21+
22+
public override float Cost() => (PriceList.ejectionSeat * howMany) + prevCarPart.Cost();
23+
}
24+
}

Assets/Patterns/20. Decorator/Tesla order system/Car parts/Extras/EjectionSeat.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,12 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Decorator.OrderSystem
6+
{
7+
//The class that "decorates" the car class
8+
public abstract class _CarExtras : _Car
9+
{
10+
protected int howMany;
11+
}
12+
}

Assets/Patterns/20. Decorator/Tesla order system/Car parts/Extras/_CarExtras.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,18 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Decorator.OrderSystem
6+
{
7+
public class ModelS : _Car
8+
{
9+
public ModelS()
10+
{
11+
this.description = "Model S";
12+
}
13+
14+
public override string GetDescription() => this.description;
15+
16+
public override float Cost() => PriceList.modelS;
17+
}
18+
}

Assets/Patterns/20. Decorator/Tesla order system/Car parts/ModelS.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,18 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Decorator.OrderSystem
6+
{
7+
public class Roadster : _Car
8+
{
9+
public Roadster()
10+
{
11+
this.description = "Roadster";
12+
}
13+
14+
public override string GetDescription() => this.description;
15+
16+
public override float Cost() => PriceList.roadster;
17+
}
18+
}

Assets/Patterns/20. Decorator/Tesla order system/Car parts/Roadster.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,16 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Decorator.OrderSystem
6+
{
7+
//The class we want to add behavior to
8+
public abstract class _Car
9+
{
10+
protected string description;
11+
12+
public abstract string GetDescription();
13+
14+
public abstract float Cost();
15+
}
16+
}

Assets/Patterns/20. Decorator/Tesla order system/Car parts/_Car.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,46 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Decorator.OrderSystem
6+
{
7+
//Decorator programming pattern example
8+
public class OrderSystemController : MonoBehaviour
9+
{
10+
private void Start()
11+
{
12+
//Order 1
13+
_Car roadster = new Roadster();
14+
15+
roadster = new DracoThruster(roadster, 5);
16+
17+
Debug.Log($"You ordered: {roadster.GetDescription()} and have to pay ${roadster.Cost()}");
18+
19+
//Order 2
20+
_Car cybertruck = new Cybertruck();
21+
22+
cybertruck = new DracoThruster(cybertruck, 2);
23+
cybertruck = new EjectionSeat(cybertruck, 1);
24+
25+
Debug.Log($"You ordered: {cybertruck.GetDescription()} and have to pay ${cybertruck.Cost()}");
26+
27+
//Order 3
28+
_Car modelS = new ModelS();
29+
30+
Debug.Log($"You ordered: {modelS.GetDescription()} and have to pay ${modelS.Cost()}");
31+
32+
33+
34+
//Some time passes while you wait for your order and Elon Musk finds a cheap way to manufacture the Cybertruck
35+
PriceList.cybertruck -= 50f;
36+
//But the price of ejections seats goes up
37+
PriceList.ejectionSeat += 30f;
38+
39+
Debug.Log("Price changes!");
40+
41+
Debug.Log($"You ordered: {roadster.GetDescription()} and have to pay ${roadster.Cost()}");
42+
Debug.Log($"You ordered: {cybertruck.GetDescription()} and have to pay ${cybertruck.Cost()}");
43+
Debug.Log($"You ordered: {modelS.GetDescription()} and have to pay ${modelS.Cost()}");
44+
}
45+
}
46+
}

Assets/Patterns/20. Decorator/Tesla order system/OrderSystemController.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,18 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Decorator.OrderSystem
6+
{
7+
public static class PriceList
8+
{
9+
//Base models
10+
public static float cybertruck = 150f;
11+
public static float modelS = 200f;
12+
public static float roadster = 350f;
13+
14+
//Extras
15+
public static float dracoThruster = 20f;
16+
public static float ejectionSeat = 200f;
17+
}
18+
}

Assets/Patterns/20. Decorator/Tesla order system/PriceList.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)