Skip to content

Commit cbbc92e

Browse files
author
Abhishek Kumar Singh
committed
Singleton and Adapter
1 parent 07c6b4d commit cbbc92e

File tree

10 files changed

+146
-96
lines changed

10 files changed

+146
-96
lines changed

.gitignore

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

Adapter.cs

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

Adapter/Adapter.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace DesignPatterns.Adapter
2+
{
3+
// 3. Adapter - makes ThirdPartyLogger fit IMyLogger interface
4+
internal class Adapter : IMyLogger
5+
{
6+
private readonly ThirdPartyLogger _thirdpartylogger;
7+
8+
public Adapter(ThirdPartyLogger thirdpartylogger)
9+
{
10+
_thirdpartylogger = thirdpartylogger;
11+
}
12+
13+
public void LogInfo(string msg)
14+
{
15+
// Will write some logic to make MyLogger,cs compatible with ThirdPartLogger.cs
16+
_thirdpartylogger.WriteLog("Success",msg);
17+
}
18+
19+
public void LogError(string msg)
20+
{
21+
// Will write some logic to make MyLogger,cs compatible with ThirdPartLogger.cs
22+
_thirdpartylogger.WriteLog("Error", msg);
23+
}
24+
}
25+
}

Adapter/MyLogger.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace DesignPatterns.Adapter
4+
{
5+
// 1. Target interface - What your app expects
6+
interface IMyLogger
7+
{
8+
void LogInfo(string msg);
9+
void LogError(string msg);
10+
}
11+
internal class MyLogger : IMyLogger
12+
{
13+
public void LogInfo(string msg)
14+
{
15+
Console.WriteLine("Info - Log");
16+
}
17+
public void LogError(string msg)
18+
{
19+
Console.WriteLine("Error - Log");
20+
}
21+
}
22+
}

Adapter/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace DesignPatterns.Adapter
4+
{
5+
internal class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
IMyLogger myLogger = new Adapter(new ThirdPartyLogger());
10+
11+
// Behind the scenes, Adapter redirects to ThirdPartyLogger.WriteLog()
12+
myLogger.LogInfo("This is success msg");
13+
myLogger.LogError("This is error msg");
14+
}
15+
}
16+
}

Adapter/ThirdPartyLogger.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
namespace DesignPatterns.Adapter
3+
{
4+
// 2. Adaptee (3rd party NuGet package class - cannot be modified!)
5+
internal class ThirdPartyLogger
6+
{
7+
public void WriteLog(string severity, string msg)
8+
{
9+
Console.WriteLine("[{0}] - {1}", severity, msg);
10+
}
11+
}
12+
}

DesignPatterns.csproj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@
3232
<ErrorReport>prompt</ErrorReport>
3333
<WarningLevel>4</WarningLevel>
3434
</PropertyGroup>
35+
<PropertyGroup>
36+
<StartupObject>DesignPatterns.Adapter.Program</StartupObject>
37+
</PropertyGroup>
3538
<ItemGroup>
3639
<Reference Include="System" />
3740
<Reference Include="System.Core" />
41+
<Reference Include="System.Drawing" />
42+
<Reference Include="System.Windows.Forms" />
3843
<Reference Include="System.Xml.Linq" />
3944
<Reference Include="System.Data.DataSetExtensions" />
4045
<Reference Include="Microsoft.CSharp" />
@@ -44,11 +49,15 @@
4449
</ItemGroup>
4550
<ItemGroup>
4651
<Compile Include="AbstractFactory.cs" />
47-
<Compile Include="Adapter.cs" />
52+
<Compile Include="Adapter\Adapter.cs" />
53+
<Compile Include="Adapter\MyLogger.cs" />
54+
<Compile Include="Adapter\Program.cs" />
55+
<Compile Include="Adapter\ThirdPartyLogger.cs" />
4856
<Compile Include="Factory.cs" />
4957
<Compile Include="Program.cs" />
5058
<Compile Include="Properties\AssemblyInfo.cs" />
51-
<Compile Include="Singleton.cs" />
59+
<Compile Include="Singleton\Program.cs" />
60+
<Compile Include="Singleton\Singleton.cs" />
5261
</ItemGroup>
5362
<ItemGroup>
5463
<None Include="App.config" />

Program.cs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
1-
//using DesignPatterns;
2-
using DesignPatterns.AdapterPattern;
3-
//using DesignPatterns.Factory;
4-
using System;
5-
// using DesignPatterns.AbstractFactory;
1+
////using DesignPatterns;
2+
//using DesignPatterns.AdapterPattern;
3+
////using DesignPatterns.Factory;
4+
//using System;
5+
//// using DesignPatterns.AbstractFactory;
66

7-
namespace DesignPatterns
8-
{
9-
internal class Program
10-
{
11-
static void Main(string[] args)
12-
{
13-
// Singleton
14-
//Singleton singleton = Singleton.GetInstance();
7+
//namespace DesignPatterns
8+
//{
9+
// internal class Program
10+
// {
11+
// static void Main(string[] args)
12+
// {
13+
// // Singleton
14+
// //Singleton singleton = Singleton.GetInstance();
1515

16-
// Consumer or Client
16+
// // Consumer or Client
1717

18-
// Factory Pattern
19-
//Waiter waiter = new Waiter();
20-
//Console.WriteLine(waiter.GetPizza("Veg"));
18+
// // Factory Pattern
19+
// //Waiter waiter = new Waiter();
20+
// //Console.WriteLine(waiter.GetPizza("Veg"));
2121

22-
// Abstract Factory Pattern
23-
//Waiter waiter = new Waiter("Veg");
24-
//Console.WriteLine(waiter.GetPrizza());
25-
//Console.WriteLine(waiter.GetBurger());
22+
// // Abstract Factory Pattern
23+
// //Waiter waiter = new Waiter("Veg");
24+
// //Console.WriteLine(waiter.GetPrizza());
25+
// //Console.WriteLine(waiter.GetBurger());
2626

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-
}
27+
// }
28+
// }
29+
//}

Singleton/Program.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace DesignPatterns.Singleton
4+
{
5+
internal class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Singleton s1 = Singleton.GetInstance();
10+
Singleton s2 = Singleton.GetInstance();
11+
12+
// Check if both references point to the same object
13+
if (ReferenceEquals(s1, s2))
14+
{
15+
Console.WriteLine("Both objects are the same instance.");
16+
}
17+
else
18+
{
19+
Console.WriteLine("Different instances created!");
20+
}
21+
}
22+
}
23+
}

Singleton.cs renamed to Singleton/Singleton.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
namespace DesignPatterns
1+
namespace DesignPatterns.Singleton
22
{
3-
class Singleton
3+
/// <summary>
4+
/// The Singleton class ensures that only one instance of the class is created and provides a global point of access to that instance.
5+
/// Key Points:
6+
/// - Private constructor to prevent instantiation from outside this class.
7+
/// - A static field to store the single instance.
8+
/// - Thread-safety ensured using 'lock' (double-checked locking).
9+
/// - Lazy initialization: instance is created only when first accessed.
10+
/// </summary>
11+
internal class Singleton
412
{
5-
/// <summary>
6-
/// The Singleton class ensures that only one instance of the class is created
7-
/// and provides a global point of access to that instance.
8-
///
9-
/// Key Points:
10-
/// - Private constructor to prevent instantiation from outside this class.
11-
/// - A static field to store the single instance.
12-
/// - Thread-safety ensured using 'lock' (double-checked locking).
13-
/// - Lazy initialization: instance is created only when first accessed.
14-
/// </summary>
15-
///
1613
private Singleton() { }
1714

1815
// Holds the single instance of the Singleton class.

0 commit comments

Comments
 (0)