-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicleFactory.cs
60 lines (45 loc) · 1.13 KB
/
VehicleFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public interface IVehicle
{
int NumberOfWheels { get; }
string PaintColour { get; }
}
public abstract class Vehicle : IVehicle
{
protected string _paintColour = "White";
public Vehicle() {}
public Vehicle(string paintColour)
{
_paintColour = paintColour;
}
public abstract int NumberOfWheels { get; }
public virtual string PaintColour { get => _paintColour; }
public void PaintVehicle(string colour){
_paintColour = colour;
}
}
public class Car : Vehicle
{
public Car() {}
public Car(string paintColour) : base(paintColour) {}
public override int NumberOfWheels { get => 4; }
}
public class Motorbike : Vehicle
{
public Motorbike() {}
public Motorbike(string paintColour) : base(paintColour) {}
public override int NumberOfWheels { get => 2; }
}
public class Bus
{
public int NumberOfWheels { get => 6; }
}
public class VehicleFactory{
public static IVehicle Create<T>() where T : Vehicle, IVehicle, new() {
if (typeof(T) == typeof(Car)){
return new Car();
}
else{
return new Motorbike();
}
}
}