Repository navigation
Expand file tree
/
Copy pathAbstarctFactory.cs
More file actions
143 lines (122 loc) · 2.51 KB
/
Copy pathAbstarctFactory.cs
File metadata and controls
143 lines (122 loc) · 2.51 KB
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
using System;
// Abstract Product: Engine
public interface IEngine
{
void Start();
}
// Concrete Products
public class SedanEngine : IEngine
{
public void Start()
{
Console.WriteLine("Sedan Engine started");
}
}
public class SUVEngine : IEngine
{
public void Start()
{
Console.WriteLine("SUV Engine started");
}
}
// Abstract Product: Interior
public interface IInterior
{
void AssembleInterior();
}
// Concrete Products
public class SedanInterior : IInterior
{
public void AssembleInterior()
{
Console.WriteLine("Assembling Sedan Interior");
}
}
public class SUVInterior : IInterior
{
public void AssembleInterior()
{
Console.WriteLine("Assembling SUV Interior");
}
}
// Abstract Product: Exterior
public interface IExterior
{
void AssembleExterior();
}
// Concrete Products
public class SedanExterior : IExterior
{
public void AssembleExterior()
{
Console.WriteLine("Assembling Sedan Exterior");
}
}
public class SUVExterior : IExterior
{
public void AssembleExterior()
{
Console.WriteLine("Assembling SUV Exterior");
}
}
// Abstract Factory
public interface IVehicleFactory
{
IEngine CreateEngine();
IInterior CreateInterior();
IExterior CreateExterior();
}
// Concrete Factories
public class SedanFactory : IVehicleFactory
{
public IEngine CreateEngine()
{
return new SedanEngine();
}
public IInterior CreateInterior()
{
return new SedanInterior();
}
public IExterior CreateExterior()
{
return new SedanExterior();
}
}
public class SUVFactory : IVehicleFactory
{
public IEngine CreateEngine()
{
return new SUVEngine();
}
public IInterior CreateInterior()
{
return new SUVInterior();
}
public IExterior CreateExterior()
{
return new SUVExterior();
}
}
// Client Code
class Program
{
static void Main()
{
// Create a Sedan
Console.WriteLine("Building Sedan:");
BuildVehicle(new SedanFactory());
// Create an SUV
Console.WriteLine("\nBuilding SUV:");
BuildVehicle(new SUVFactory());
Console.ReadLine();
}
static void BuildVehicle(IVehicleFactory factory)
{
var engine = factory.CreateEngine();
var interior = factory.CreateInterior();
var exterior = factory.CreateExterior();
engine.Start();
interior.AssembleInterior();
exterior.AssembleExterior();
}
}