Skip to content

Commit ad19664

Browse files
author
Abhishek Kumar Singh
committed
Factory & Abstract factory
1 parent 078ca4d commit ad19664

File tree

11 files changed

+170
-251
lines changed

11 files changed

+170
-251
lines changed

AbstractFactory.cs

Lines changed: 0 additions & 94 deletions
This file was deleted.

AbstractFactory/Product.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
3+
namespace DesignPatterns.AbstractFactory
4+
{
5+
// Abstract Products (2)
6+
interface IPizza
7+
{
8+
string Eat();
9+
}
10+
interface IBurger
11+
{
12+
string Eat();
13+
}
14+
15+
// Concrete Products
16+
class VegPizza : IPizza
17+
{
18+
public string Eat()
19+
{
20+
return "Eating Veg Pizza";
21+
}
22+
}
23+
class NonVegPizza : IPizza
24+
{
25+
public string Eat()
26+
{
27+
return "Eating Non-Veg Pizza";
28+
}
29+
}
30+
class VegBurger : IBurger
31+
{
32+
public string Eat()
33+
{
34+
return "Eating Veg Burger";
35+
}
36+
}
37+
class NonVegBurger : IBurger
38+
{
39+
public string Eat()
40+
{
41+
return "Eating Non-Veg Burger";
42+
}
43+
}
44+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using DesignPatterns.Factory;
2+
3+
namespace DesignPatterns.AbstractFactory
4+
{
5+
// Factory manages object creation logic in one place,
6+
// So the client code depends only on an interface/abstract class, not on concrete implementations.
7+
8+
// Abstract Factory
9+
interface I_productFactory
10+
{
11+
IPizza PreparePizza();
12+
IBurger PrepareBurger();
13+
}
14+
15+
// Concrete Factories
16+
class VegFactory : I_productFactory
17+
{
18+
public IPizza PreparePizza()
19+
{
20+
return new VegPizza();
21+
}
22+
public IBurger PrepareBurger()
23+
{
24+
return new VegBurger();
25+
}
26+
}
27+
28+
class NonVegFactory : I_productFactory
29+
{
30+
public IPizza PreparePizza()
31+
{
32+
return new NonVegPizza();
33+
}
34+
public IBurger PrepareBurger()
35+
{
36+
return new NonVegBurger();
37+
}
38+
}
39+
40+
// Factory Producer
41+
internal class Service
42+
{
43+
private I_productFactory _product;
44+
public Service(string type)
45+
{
46+
switch (type.ToUpper())
47+
{
48+
case "veg":
49+
_product = new VegFactory();
50+
break;
51+
default:
52+
_product = new NonVegFactory();
53+
break;
54+
}
55+
}
56+
57+
public string GetPrizza()
58+
{
59+
return _product.PreparePizza().Eat();
60+
}
61+
public string GetBurger()
62+
{
63+
return _product.PrepareBurger().Eat();
64+
}
65+
}
66+
}

AbstractFactory/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using DesignPatterns.Factory;
2+
using System;
3+
4+
namespace DesignPatterns.AbstractFactory
5+
{
6+
internal class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
// Client does not know concrete implementations, only the Abstract Factory
11+
Service service = new Service("veg");
12+
Console.WriteLine(service.GetPrizza());
13+
Console.WriteLine(service.GetBurger());
14+
}
15+
}
16+
}

DesignPatterns.csproj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<WarningLevel>4</WarningLevel>
3434
</PropertyGroup>
3535
<PropertyGroup>
36-
<StartupObject>DesignPatterns.Factory.Program</StartupObject>
36+
<StartupObject>DesignPatterns.AbstractFactory.Program</StartupObject>
3737
</PropertyGroup>
3838
<ItemGroup>
3939
<Reference Include="System" />
@@ -48,22 +48,23 @@
4848
<Reference Include="System.Xml" />
4949
</ItemGroup>
5050
<ItemGroup>
51-
<Compile Include="AbstractFactory.cs" />
51+
<Compile Include="AbstractFactory\Program.cs" />
52+
<Compile Include="AbstractFactory\Product.cs" />
53+
<Compile Include="AbstractFactory\ProductAbstractFactory.cs" />
5254
<Compile Include="Adapter\Adapter.cs" />
5355
<Compile Include="Adapter\MyLogger.cs" />
5456
<Compile Include="Adapter\Program.cs" />
5557
<Compile Include="Adapter\ThirdPartyLogger.cs" />
56-
<Compile Include="Factory.cs" />
57-
<Compile Include="Factory\VehicleFactory.cs" />
58-
<Compile Include="Factory\Vehicle.cs" />
58+
<Compile Include="Factory\ProductFactory.cs" />
59+
<Compile Include="Factory\Product.cs" />
5960
<Compile Include="Factory\Program.cs" />
60-
<Compile Include="Program.cs" />
6161
<Compile Include="Properties\AssemblyInfo.cs" />
6262
<Compile Include="Singleton\Program.cs" />
6363
<Compile Include="Singleton\Singleton.cs" />
6464
</ItemGroup>
6565
<ItemGroup>
6666
<None Include="App.config" />
6767
</ItemGroup>
68+
<ItemGroup />
6869
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6970
</Project>

Factory.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

Factory/Product.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace DesignPatterns.Factory
4+
{
5+
// My Product
6+
interface IPizza
7+
{
8+
void Eat();
9+
}
10+
internal class VegPizza : IPizza
11+
{
12+
public void Eat()
13+
{
14+
Console.WriteLine("Eating VegPizza...");
15+
}
16+
}
17+
internal class NonVegPizza : IPizza
18+
{
19+
public void Eat()
20+
{
21+
Console.WriteLine("Eating NonVegPizza...");
22+
}
23+
}
24+
}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,21 @@
22
{
33
// Factory manages object creation logic in one place,
44
// So the client code depends only on an interface/abstract class, not on concrete implementations.
5-
internal class VehicleFactory
5+
internal class ProductFactory
66
{
7-
public static IVehicle GetVehicle(string type)
7+
private static IPizza _pizza;
8+
public static IPizza GetPizza(string type)
89
{
9-
IVehicle vehicle;
10-
1110
switch(type.ToUpper())
1211
{
13-
case "CAR":
14-
vehicle = new Car();
15-
break;
16-
case "BIKE":
17-
vehicle = new Bike();
12+
case "VEG":
13+
_pizza = new VegPizza();
1814
break;
1915
default:
20-
vehicle = new Truck();
16+
_pizza = new NonVegPizza();
2117
break;
2218
}
23-
return vehicle;
19+
return _pizza;
2420
}
2521
}
2622
}

0 commit comments

Comments
 (0)