Skip to content

Commit 07c6b4d

Browse files
author
Abhishek Kumar Singh
committed
Design Patterns
0 parents  commit 07c6b4d

File tree

10 files changed

+406
-0
lines changed

10 files changed

+406
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.vscode/
2+
*bin/
3+
*obj/
4+
*.out/
5+
*.vs/

AbstractFactory.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
namespace DesignPatterns.AbstractFactory
2+
{
3+
// Abstract Products
4+
interface IPizza
5+
{
6+
string Eat();
7+
}
8+
interface IBurger
9+
{
10+
string Eat();
11+
}
12+
13+
// Concrete Products
14+
class VegPizza : IPizza
15+
{
16+
public string Eat()
17+
{
18+
return "Eating Veg Pizza";
19+
}
20+
}
21+
class NonVegPizza : IPizza
22+
{
23+
public string Eat()
24+
{
25+
return "Eating Non-Veg Pizza";
26+
}
27+
}
28+
class VegBurger : IBurger
29+
{
30+
public string Eat()
31+
{
32+
return "Eating Veg Burger";
33+
}
34+
}
35+
class NonVegBurger : IBurger
36+
{
37+
public string Eat()
38+
{
39+
return "Eating Non-Veg Burger";
40+
}
41+
}
42+
43+
// Abstract Factory
44+
interface IChef
45+
{
46+
IPizza PreparePizza();
47+
IBurger PrepareBurger();
48+
}
49+
50+
// Concrete Factories
51+
class VegChef : IChef
52+
{
53+
public IPizza PreparePizza()
54+
{
55+
return new VegPizza();
56+
}
57+
public IBurger PrepareBurger()
58+
{
59+
return new VegBurger();
60+
}
61+
}
62+
class NonVegChef : IChef
63+
{
64+
public IPizza PreparePizza()
65+
{
66+
return new NonVegPizza();
67+
}
68+
public IBurger PrepareBurger()
69+
{
70+
return new NonVegBurger();
71+
}
72+
}
73+
74+
// Factory Producer
75+
class Waiter
76+
{
77+
private readonly IChef _chef;
78+
public Waiter(string pref)
79+
{
80+
if (pref == "Veg")
81+
_chef = new VegChef();
82+
else
83+
_chef = new NonVegChef();
84+
}
85+
public string GetPrizza()
86+
{
87+
return _chef.PreparePizza().Eat();
88+
}
89+
public string GetBurger()
90+
{
91+
return _chef.PrepareBurger().Eat();
92+
}
93+
}
94+
}

Adapter.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
3+
namespace DesignPatterns.AdapterPattern
4+
{
5+
// 1. Target interface - What your app expects
6+
public interface ILogger
7+
{
8+
void LogInfo(string msg);
9+
}
10+
11+
// Your own logger implementation
12+
public class Log : ILogger
13+
{
14+
public void LogInfo(string msg)
15+
{
16+
Console.WriteLine(msg);
17+
}
18+
}
19+
20+
// 2. Adaptee (3rd party NuGet package class - cannot be modified!)
21+
public class ThirdPartyLogger
22+
{
23+
public void WriteLog(string severity, string msg)
24+
{
25+
Console.WriteLine("[{0}] {1}", severity, msg);
26+
}
27+
}
28+
29+
// 3. Adapter - makes ThirdPartyLogger fit ILogger interface
30+
public class LoggerAdapter : ILogger
31+
{
32+
private readonly ThirdPartyLogger _thirdPartyLogger;
33+
34+
public LoggerAdapter(ThirdPartyLogger thirdPartyLogger)
35+
{
36+
_thirdPartyLogger = thirdPartyLogger;
37+
}
38+
39+
public void LogInfo(string msg)
40+
{
41+
// Converts ILogger's expected method into the ThirdPartyLogger call
42+
string severity = "Info";
43+
_thirdPartyLogger.WriteLog(severity, msg);
44+
}
45+
}
46+
}

App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
5+
</startup>
6+
</configuration>

DesignPatterns.csproj

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{F115DBF7-ED9F-4FBD-A985-F1ED267F3322}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>DesignPatterns</RootNamespace>
10+
<AssemblyName>DesignPatterns</AssemblyName>
11+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="AbstractFactory.cs" />
47+
<Compile Include="Adapter.cs" />
48+
<Compile Include="Factory.cs" />
49+
<Compile Include="Program.cs" />
50+
<Compile Include="Properties\AssemblyInfo.cs" />
51+
<Compile Include="Singleton.cs" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<None Include="App.config" />
55+
</ItemGroup>
56+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
57+
</Project>

DesignPatterns.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36401.2 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns", "DesignPatterns.csproj", "{F115DBF7-ED9F-4FBD-A985-F1ED267F3322}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F115DBF7-ED9F-4FBD-A985-F1ED267F3322}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F115DBF7-ED9F-4FBD-A985-F1ED267F3322}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F115DBF7-ED9F-4FBD-A985-F1ED267F3322}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F115DBF7-ED9F-4FBD-A985-F1ED267F3322}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {1162DD92-68AB-422C-AD94-9D7539E2EC45}
24+
EndGlobalSection
25+
EndGlobal

Factory.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace DesignPatterns.Factory
2+
{
3+
// Abstract Product
4+
interface IPizza
5+
{
6+
string Eat();
7+
}
8+
9+
// Concrete Products
10+
class VegPizza : IPizza
11+
{
12+
public string Eat()
13+
{
14+
return "Eating veg pizza";
15+
}
16+
}
17+
class NonVegPizza : IPizza
18+
{
19+
public string Eat()
20+
{
21+
return "Eating Non-veg pizza";
22+
}
23+
}
24+
25+
// Abstract Factory
26+
interface IChef
27+
{
28+
IPizza PreparePizza();
29+
}
30+
31+
// Concrete Factories
32+
class VegChef : IChef
33+
{
34+
public IPizza PreparePizza()
35+
{
36+
return new VegPizza();
37+
}
38+
}
39+
class NonVegChef : IChef
40+
{
41+
public IPizza PreparePizza()
42+
{
43+
return new NonVegPizza();
44+
}
45+
}
46+
47+
// Factory Producer
48+
class Waiter
49+
{
50+
private IChef _chef;
51+
public string GetPizza(string pref)
52+
{
53+
if (pref == "Veg")
54+
_chef = new VegChef();
55+
else
56+
_chef = new NonVegChef();
57+
58+
return _chef.PreparePizza().Eat();
59+
}
60+
}
61+
}

Program.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//using DesignPatterns;
2+
using DesignPatterns.AdapterPattern;
3+
//using DesignPatterns.Factory;
4+
using System;
5+
// using DesignPatterns.AbstractFactory;
6+
7+
namespace DesignPatterns
8+
{
9+
internal class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
// Singleton
14+
//Singleton singleton = Singleton.GetInstance();
15+
16+
// Consumer or Client
17+
18+
// Factory Pattern
19+
//Waiter waiter = new Waiter();
20+
//Console.WriteLine(waiter.GetPizza("Veg"));
21+
22+
// Abstract Factory Pattern
23+
//Waiter waiter = new Waiter("Veg");
24+
//Console.WriteLine(waiter.GetPrizza());
25+
//Console.WriteLine(waiter.GetBurger());
26+
27+
// Adapter Design Patter
28+
29+
// 4. Client - your application
30+
// Normally, client code depends only on ILogger
31+
ILogger logger = new LoggerAdapter(new ThirdPartyLogger());
32+
33+
// Behind the scenes, Adapter redirects to ThirdPartyLogger.WriteLog()
34+
logger.LogInfo("Behind the scenes, calling WriteLog() from ThirdPartyLogger.");
35+
}
36+
}
37+
}

Properties/AssemblyInfo.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("DesignPatterns")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Hexagon capability center")]
12+
[assembly: AssemblyProduct("DesignPatterns")]
13+
[assembly: AssemblyCopyright("Copyright © Hexagon capability center 2025")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("f115dbf7-ed9f-4fbd-a985-f1ed267f3322")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)