-
Notifications
You must be signed in to change notification settings - Fork 31
/
AbstractFactory.cs
145 lines (126 loc) · 3.64 KB
/
AbstractFactory.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
namespace DesignPatterns.Creational;
public class AbstractFactory
{
[Fact]
public void Execute()
{
var carDealer = new CarDealer(new VolvoVehicleFactory());
Car car = carDealer.Car;
Assert.Equal("Volvo", car.GetMake());
Truck truck = carDealer.Truck;
Assert.Equal("Volvo", truck.GetMake());
carDealer = new CarDealer(new FordVehicleFactory());
car = carDealer.Car;
Assert.Equal("Ford", car.GetMake());
truck = carDealer.Truck;
Assert.Equal("Ford", truck.GetMake());
}
/// <summary>
/// Abstract Factory
/// </summary>
/// <remarks>
/// Declares an interface for operations that create abstract products.
/// </remarks>
public interface IVehicleFactory
{
Car CreateCar();
Truck CreateTruck();
}
/// <summary>
/// Abstract Product
/// </summary>
/// <remarks>
/// Declares an interface for a type of product object.
/// </remarks>
public abstract class Car
{
public abstract string GetMake();
}
/// <summary>
/// Abstract Product
/// </summary>
/// <remarks>
/// Declares an interface for a type of product object.
/// </remarks>
public abstract class Truck
{
public abstract string GetMake();
}
/// <summary>
/// Client
/// </summary>
/// <remarks>
/// Uses interfaces declared by Abstract Factory and Abstract Product classes.
/// </remarks>
public class CarDealer(IVehicleFactory vehicleFactory)
{
public Car Car { get; } = vehicleFactory.CreateCar();
public Truck Truck { get; } = vehicleFactory.CreateTruck();
}
/// <summary>
/// Product
/// </summary>
/// <remarks>
/// - Defines a product object to be created by the corresponding concrete factory.
/// - Implements the Abstract Product interface.
/// </remarks>
public class FordCar : Car
{
public override string GetMake() => "Ford";
}
/// <summary>
/// Product
/// </summary>
/// <remarks>
/// - Defines a product object to be created by the corresponding concrete factory.
/// - Implements the Abstract Product interface.
/// </remarks>
public class FordTruck : Truck
{
public override string GetMake() => "Ford";
}
/// <summary>
/// Concrete Factory
/// </summary>
/// <remarks>
/// Implements the operations to create concrete product objects.
/// </remarks>
public class FordVehicleFactory : IVehicleFactory
{
public Car CreateCar() => new FordCar();
public Truck CreateTruck() => new FordTruck();
}
/// <summary>
/// Product
/// </summary>
/// <remarks>
/// - Defines a product object to be created by the corresponding concrete factory.
/// - Implements the Abstract Product interface.
/// </remarks>
public class VolvoCar : Car
{
public override string GetMake() => "Volvo";
}
/// <summary>
/// Product
/// </summary>
/// <remarks>
/// - Defines a product object to be created by the corresponding concrete factory.
/// - Implements the Abstract Product interface.
/// </remarks>
public class VolvoTruck : Truck
{
public override string GetMake() => "Volvo";
}
/// <summary>
/// Concrete Factory
/// </summary>
/// <remarks>
/// Implements the operations to create concrete product objects.
/// </remarks>
public class VolvoVehicleFactory : IVehicleFactory
{
public Car CreateCar() => new VolvoCar();
public Truck CreateTruck() => new VolvoTruck();
}
}