Skip to content

Commit 078ca4d

Browse files
author
Abhishek Kumar Singh
committed
Factory pattern
1 parent cbbc92e commit 078ca4d

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

DesignPatterns.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<WarningLevel>4</WarningLevel>
3434
</PropertyGroup>
3535
<PropertyGroup>
36-
<StartupObject>DesignPatterns.Adapter.Program</StartupObject>
36+
<StartupObject>DesignPatterns.Factory.Program</StartupObject>
3737
</PropertyGroup>
3838
<ItemGroup>
3939
<Reference Include="System" />
@@ -54,6 +54,9 @@
5454
<Compile Include="Adapter\Program.cs" />
5555
<Compile Include="Adapter\ThirdPartyLogger.cs" />
5656
<Compile Include="Factory.cs" />
57+
<Compile Include="Factory\VehicleFactory.cs" />
58+
<Compile Include="Factory\Vehicle.cs" />
59+
<Compile Include="Factory\Program.cs" />
5760
<Compile Include="Program.cs" />
5861
<Compile Include="Properties\AssemblyInfo.cs" />
5962
<Compile Include="Singleton\Program.cs" />

Factory/Program.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace DesignPatterns.Factory
2+
{
3+
internal class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
// Normally without any pattern, we might do something like the below one,
8+
// The client code directly depends on concrete classes(Car, Bike, Truck)
9+
10+
//Car car = new Car();
11+
//car.Drive();
12+
13+
//Bike bike = new Bike();
14+
//bike.Drive();
15+
16+
//Truck truck = new Truck();
17+
//truck.Drive();
18+
19+
20+
// Client uses Factory instead of new, and only knows about IVehicle, not about concrete classes
21+
22+
IVehicle car = VehicleFactory.GetVehicle("car");
23+
car.Drive();
24+
25+
IVehicle bike = VehicleFactory.GetVehicle("bike");
26+
bike.Drive();
27+
28+
IVehicle truck = VehicleFactory.GetVehicle("truck");
29+
truck.Drive();
30+
}
31+
}
32+
}

Factory/Vehicle.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
3+
namespace DesignPatterns.Factory
4+
{
5+
// My Product
6+
interface IVehicle
7+
{
8+
void Drive();
9+
}
10+
internal class Car : IVehicle
11+
{
12+
public void Drive()
13+
{
14+
Console.WriteLine("Driving Car...");
15+
}
16+
}
17+
internal class Bike : IVehicle
18+
{
19+
public void Drive()
20+
{
21+
Console.WriteLine("Driving Bike...");
22+
}
23+
}
24+
internal class Truck : IVehicle
25+
{
26+
public void Drive()
27+
{
28+
Console.WriteLine("Driving Truck...");
29+
}
30+
}
31+
}

Factory/VehicleFactory.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace DesignPatterns.Factory
2+
{
3+
// Factory manages object creation logic in one place,
4+
// So the client code depends only on an interface/abstract class, not on concrete implementations.
5+
internal class VehicleFactory
6+
{
7+
public static IVehicle GetVehicle(string type)
8+
{
9+
IVehicle vehicle;
10+
11+
switch(type.ToUpper())
12+
{
13+
case "CAR":
14+
vehicle = new Car();
15+
break;
16+
case "BIKE":
17+
vehicle = new Bike();
18+
break;
19+
default:
20+
vehicle = new Truck();
21+
break;
22+
}
23+
return vehicle;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)