Last active
May 5, 2018 03:36
-
-
Save asarnaout/3d1f420f04dd83ed44db2c76ed25eb8d to your computer and use it in GitHub Desktop.
Observer Design Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Observer | |
{ | |
class Program | |
{ | |
/* | |
* The Observer pattern allows you to define a one-to-many dependency between objects so that when one object changes state, all its | |
* dependents are notified and updated automatically. Note that the `event` keyword is a dot net implementation of the observer pattern. | |
*/ | |
static void Main(string[] args) | |
{ | |
var chatRoom = new ChatRoom(); | |
chatRoom.AttachObserver(new User1()); | |
chatRoom.AttachObserver(new User2()); | |
chatRoom.BroadcastMessage("Hello from the server!"); | |
Console.ReadLine(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Observer | |
{ | |
public class ChatRoom | |
{ | |
private readonly ICollection<IObserver> _observers; | |
public ChatRoom() | |
{ | |
_observers = new List<IObserver>(); | |
} | |
public void AttachObserver(IObserver observer) => _observers.Add(observer); | |
public void BroadcastMessage(string message) | |
{ | |
foreach(var observer in _observers) | |
{ | |
observer.Notify(message); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Observer | |
{ | |
public interface IObserver | |
{ | |
void Notify<T>(T message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Observer | |
{ | |
public class User2 : IObserver | |
{ | |
public User2() | |
{ | |
} | |
public void Notify<T>(T message) | |
{ | |
Console.WriteLine($"{message} was received by user2"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Observer | |
{ | |
public class User1 : IObserver | |
{ | |
public User1() | |
{ | |
} | |
public void Notify<T>(T message) | |
{ | |
Console.WriteLine($"{message} was received by user1"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment