File tree Expand file tree Collapse file tree 5 files changed +93
-2
lines changed Expand file tree Collapse file tree 5 files changed +93
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -12,10 +12,25 @@ static string GetUsername(string emailAddress)
12
12
13
13
//Singleton Pattern
14
14
Console . WriteLine ( "Singleton Pattern" ) ;
15
+ Singleton singleton = Singleton . Instance ;
15
16
Console . WriteLine ( $ "Hello, { Singleton . Instance . GetUsername ( "[email protected] " ) } !") ;
16
17
18
+
17
19
//Factory Pattern
18
20
ProductFactory factory = new ProductFactory ( ) ;
19
21
IProduct product = factory . CreateProduct ( ) ;
20
22
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 ) ;
Original file line number Diff line number Diff line change 6
6
7
7
namespace DesignPatterns
8
8
{
9
- public class Singleton
9
+ public class Singleton : ISubject
10
10
{
11
11
private static Singleton instance ;
12
+ private List < IObserver > observers = new List < IObserver > ( ) ;
12
13
13
14
private Singleton ( )
14
15
{
@@ -27,6 +28,24 @@ public static Singleton Instance
27
28
}
28
29
}
29
30
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
+
30
49
public string GetUsername ( string emailAddress )
31
50
{
32
51
int atIndex = emailAddress . IndexOf ( '@' ) ;
You can’t perform that action at this time.
0 commit comments