Skip to content

Commit 0294e4a

Browse files
committed
Observer Pattern
1 parent 8d0ab3b commit 0294e4a

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
lines changed

IObserver.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DesignPatterns
8+
{
9+
public interface IObserver
10+
{
11+
void Update(string message);
12+
}
13+
}

ISubject.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DesignPatterns
8+
{
9+
public interface ISubject
10+
{
11+
void Attach(IObserver observer);
12+
void Detach(IObserver observer);
13+
void NotifyObservers(string message);
14+
}
15+
}

Observer.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DesignPatterns
8+
{
9+
public class Observer : IObserver
10+
{
11+
private string username;
12+
13+
public void Update(string message)
14+
{
15+
username = GetUsername(message);
16+
}
17+
18+
public string GetUsername(string emailAddress)
19+
{
20+
int atIndex = emailAddress.IndexOf('@');
21+
return (atIndex > 0) ? emailAddress.Substring(0, atIndex) : string.Empty;
22+
}
23+
24+
public string GetUsername()
25+
{
26+
return username;
27+
}
28+
}
29+
}

Program.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,25 @@ static string GetUsername(string emailAddress)
1212

1313
//Singleton Pattern
1414
Console.WriteLine("Singleton Pattern");
15+
Singleton singleton = Singleton.Instance;
1516
Console.WriteLine($"Hello, {Singleton.Instance.GetUsername("[email protected]")}!");
1617

18+
1719
//Factory Pattern
1820
ProductFactory factory = new ProductFactory();
1921
IProduct product = factory.CreateProduct();
2022
Console.WriteLine("Factory Pattern");
21-
Console.WriteLine($"Hello, {product.GetUsername("[email protected]")}!");
23+
Console.WriteLine($"Hello, {product.GetUsername("[email protected]")}!");
24+
25+
26+
// Observer Pattern
27+
Console.WriteLine("Observer Pattern");
28+
Observer observer = new Observer();
29+
singleton.Attach(observer);
30+
singleton.NotifyObservers("[email protected]");
31+
string username = observer.GetUsername();
32+
Console.WriteLine($"Hello, {username}!");
33+
34+
// Other methods and code...
35+
36+
singleton.Detach(observer);

Singleton.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
namespace DesignPatterns
88
{
9-
public class Singleton
9+
public class Singleton : ISubject
1010
{
1111
private static Singleton instance;
12+
private List<IObserver> observers = new List<IObserver>();
1213

1314
private Singleton()
1415
{
@@ -27,6 +28,24 @@ public static Singleton Instance
2728
}
2829
}
2930

31+
public void Attach(IObserver observer)
32+
{
33+
observers.Add(observer);
34+
}
35+
36+
public void Detach(IObserver observer)
37+
{
38+
observers.Remove(observer);
39+
}
40+
41+
public void NotifyObservers(string message)
42+
{
43+
foreach (var observer in observers)
44+
{
45+
observer.Update(message);
46+
}
47+
}
48+
3049
public string GetUsername(string emailAddress)
3150
{
3251
int atIndex = emailAddress.IndexOf('@');

0 commit comments

Comments
 (0)