Skip to content

Commit 331de4d

Browse files
committed
Added type object pattern
1 parent f0e059d commit 331de4d

24 files changed

+634
-0
lines changed

Assets/Patterns/12. Type Object.meta

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

Assets/Patterns/12. Type Object/Animal.meta

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

Assets/Patterns/12. Type Object/Animal/Scripts.meta

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

Assets/Patterns/12. Type Object/Animal/Scripts/Animals.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 TypeObject.Animal
6+
{
7+
//Parent animal class
8+
public class Animal
9+
{
10+
protected string name;
11+
12+
public virtual void Talk()
13+
{
14+
15+
}
16+
}
17+
}

Assets/Patterns/12. Type Object/Animal/Scripts/Animals/Animal.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,26 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace TypeObject.Animal
6+
{
7+
public class Bird : Animal
8+
{
9+
//This is the Type Object
10+
private IFlyingType flyingType;
11+
12+
public Bird(string name, bool canFly)
13+
{
14+
this.name = name;
15+
16+
this.flyingType = canFly ? new ICanFly() as IFlyingType : new ICantFly();
17+
}
18+
19+
public override void Talk()
20+
{
21+
string canFlyString = flyingType.CanIFly() ? "can" : "can't";
22+
23+
Debug.Log($"Hello this is {name}, I'm a bird, and I {canFlyString} fly!");
24+
}
25+
}
26+
}

Assets/Patterns/12. Type Object/Animal/Scripts/Animals/Bird.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,26 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace TypeObject.Animal
6+
{
7+
public class Fish : Animal
8+
{
9+
//This is the Type Object
10+
private IFlyingType flyingType;
11+
12+
public Fish(string name, bool canFly)
13+
{
14+
this.name = name;
15+
16+
this.flyingType = canFly ? new ICanFly() as IFlyingType : new ICantFly();
17+
}
18+
19+
public override void Talk()
20+
{
21+
string canFlyString = flyingType.CanIFly() ? "can" : "can't";
22+
23+
Debug.Log($"Hello this is {name}, I'm a fish, and I {canFlyString} fly!");
24+
}
25+
}
26+
}

Assets/Patterns/12. Type Object/Animal/Scripts/Animals/Fish.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,26 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace TypeObject.Animal
6+
{
7+
public class Mammal : Animal
8+
{
9+
//This is the Type Object
10+
private IFlyingType flyingType;
11+
12+
public Mammal(string name, bool canFly)
13+
{
14+
this.name = name;
15+
16+
this.flyingType = canFly ? new ICanFly() as IFlyingType : new ICantFly();
17+
}
18+
19+
public override void Talk()
20+
{
21+
string canFlyString = flyingType.CanIFly() ? "can" : "can't";
22+
23+
Debug.Log($"Hello this is {name}, I'm a mammal, and I {canFlyString} fly!");
24+
}
25+
}
26+
}

Assets/Patterns/12. Type Object/Animal/Scripts/Animals/Mammal.cs.meta

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

Assets/Patterns/12. Type Object/Animal/Scripts/Animals/Types.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,15 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace TypeObject.Animal
6+
{
7+
//This type can fly
8+
public class ICanFly : IFlyingType
9+
{
10+
public bool CanIFly()
11+
{
12+
return true;
13+
}
14+
}
15+
}

Assets/Patterns/12. Type Object/Animal/Scripts/Animals/Types/ICanFly.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,15 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace TypeObject.Animal
6+
{
7+
//This type can't fly
8+
public class ICantFly : IFlyingType
9+
{
10+
public bool CanIFly()
11+
{
12+
return false;
13+
}
14+
}
15+
}

Assets/Patterns/12. Type Object/Animal/Scripts/Animals/Types/ICantFly.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 TypeObject.Animal
6+
{
7+
//Defines the Type Object if whatever implements it can fly
8+
public interface IFlyingType
9+
{
10+
bool CanIFly();
11+
}
12+
}

Assets/Patterns/12. Type Object/Animal/Scripts/Animals/Types/IFlyingType.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,34 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace TypeObject.Animal
6+
{
7+
//Main class to illustrate the Type object pattern
8+
public class TypeObjectController : MonoBehaviour
9+
{
10+
void Start()
11+
{
12+
Bird ostrich = new Bird("ostrich", canFly: false);
13+
14+
Bird pigeon = new Bird("pigeon", canFly: true);
15+
16+
Mammal rat = new Mammal("rat", canFly: false);
17+
18+
Mammal bat = new Mammal("bat", canFly: true);
19+
20+
Fish flyingFish = new Fish("Flying fish", canFly: true);
21+
22+
23+
ostrich.Talk();
24+
25+
pigeon.Talk();
26+
27+
rat.Talk();
28+
29+
bat.Talk();
30+
31+
flyingFish.Talk();
32+
}
33+
}
34+
}

Assets/Patterns/12. Type Object/Animal/Scripts/TypeObjectController.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)