Skip to content

Commit 31b2e3d

Browse files
author
anupavanm
committed
abstract factory pattern example
1 parent 4e8594e commit 31b2e3d

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

AbstractFactoryPattern/Program.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
3+
namespace AbstractFactoryPattern
4+
{
5+
interface IDoor
6+
{
7+
8+
void GetDescription();
9+
10+
}
11+
class WoodenDoor : IDoor
12+
{
13+
public void GetDescription()
14+
{
15+
Console.WriteLine("I am a wooden door");
16+
}
17+
}
18+
19+
class IronDoor : IDoor
20+
{
21+
public void GetDescription()
22+
{
23+
Console.WriteLine("I am a iron door");
24+
}
25+
}
26+
27+
interface IDoorFittingExpert
28+
{
29+
void GetDescription();
30+
}
31+
32+
class Welder : IDoorFittingExpert
33+
{
34+
public void GetDescription()
35+
{
36+
Console.WriteLine("I can only fit iron doors");
37+
}
38+
}
39+
40+
class Carpenter : IDoorFittingExpert
41+
{
42+
public void GetDescription()
43+
{
44+
Console.WriteLine("I can only fit wooden doors");
45+
}
46+
}
47+
48+
interface IDoorFactory
49+
{
50+
IDoor MakeDoor();
51+
IDoorFittingExpert MakeFittingExpert();
52+
}
53+
54+
// Wooden factory to return carpenter and wooden door
55+
class WoodenDoorFactory : IDoorFactory
56+
{
57+
public IDoor MakeDoor()
58+
{
59+
return new WoodenDoor();
60+
}
61+
// Iron door factory to get iron door and the relevant fitting expert
62+
public IDoorFittingExpert MakeFittingExpert()
63+
{
64+
return new Carpenter();
65+
}
66+
}
67+
68+
class IronDoorFactory : IDoorFactory
69+
{
70+
public IDoor MakeDoor()
71+
{
72+
return new IronDoor();
73+
}
74+
75+
public IDoorFittingExpert MakeFittingExpert()
76+
{
77+
return new Welder();
78+
}
79+
}
80+
81+
82+
class Program
83+
{
84+
static void Main(string[] args)
85+
{
86+
var woodenDoorFactory = new WoodenDoorFactory();
87+
var woodenDoor = woodenDoorFactory.MakeDoor();
88+
var woodenDoorFittingExpert = woodenDoorFactory.MakeFittingExpert();
89+
90+
woodenDoor.GetDescription(); //Output : I am a wooden door
91+
woodenDoorFittingExpert.GetDescription();//Output : I can only fit woooden doors
92+
93+
var ironDoorFactory = new IronDoorFactory();
94+
var ironDoor = ironDoorFactory.MakeDoor();
95+
var ironDoorFittingExpert = ironDoorFactory.MakeFittingExpert();
96+
97+
ironDoor.GetDescription();//Output : I am a iron door
98+
ironDoorFittingExpert.GetDescription();//Output : I can only fit iron doors
99+
100+
Console.ReadLine();
101+
}
102+
}
103+
}

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryPattern", "FactoryPa
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryMethodPattern", "FactoryMethodPattern\FactoryMethodPattern.csproj", "{493DBC90-47D2-49F5-906D-14F3AFDDF9F8}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFactoryPattern", "AbstractFactoryPattern\AbstractFactoryPattern.csproj", "{081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{493DBC90-47D2-49F5-906D-14F3AFDDF9F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{493DBC90-47D2-49F5-906D-14F3AFDDF9F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{493DBC90-47D2-49F5-906D-14F3AFDDF9F8}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)