From 27045cc4b9e6ec3dbbb7f5aaa1b571d784b81e60 Mon Sep 17 00:00:00 2001 From: Alex Hyett Date: Mon, 4 Sep 2023 10:56:02 +0100 Subject: [PATCH] Add observer pattern --- Observer/IPublisher.cs | 8 ++++++++ Observer/ISubscriber.cs | 5 +++++ Observer/Observer.csproj | 10 ++++++++++ Observer/Program.cs | 16 ++++++++++++++++ Observer/Publisher.cs | 35 +++++++++++++++++++++++++++++++++++ Observer/Subscriber.cs | 24 ++++++++++++++++++++++++ 6 files changed, 98 insertions(+) create mode 100644 Observer/IPublisher.cs create mode 100644 Observer/ISubscriber.cs create mode 100644 Observer/Observer.csproj create mode 100644 Observer/Program.cs create mode 100644 Observer/Publisher.cs create mode 100644 Observer/Subscriber.cs diff --git a/Observer/IPublisher.cs b/Observer/IPublisher.cs new file mode 100644 index 0000000..c139146 --- /dev/null +++ b/Observer/IPublisher.cs @@ -0,0 +1,8 @@ +public interface IPublisher +{ + void Subscribe(ISubscriber subscriber); + + void Unsubscribe(ISubscriber subscriber); + + void Notify(); +} \ No newline at end of file diff --git a/Observer/ISubscriber.cs b/Observer/ISubscriber.cs new file mode 100644 index 0000000..ae39ef6 --- /dev/null +++ b/Observer/ISubscriber.cs @@ -0,0 +1,5 @@ +public interface ISubscriber +{ + string Name { get; } + void Update(IPublisher subject); +} \ No newline at end of file diff --git a/Observer/Observer.csproj b/Observer/Observer.csproj new file mode 100644 index 0000000..d439800 --- /dev/null +++ b/Observer/Observer.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/Observer/Program.cs b/Observer/Program.cs new file mode 100644 index 0000000..43d27e4 --- /dev/null +++ b/Observer/Program.cs @@ -0,0 +1,16 @@ + +var publisher = new Publisher(); + +var subscriberA = new Subscriber("Alex"); +publisher.Subscribe(subscriberA); + +var subscriberB = new Subscriber("Bob"); +publisher.Subscribe(subscriberB); + +publisher.PublishNewsletter(); +publisher.PublishNewsletter(); +publisher.PublishNewsletter(); + +publisher.Unsubscribe(subscriberB); + +publisher.PublishNewsletter(); diff --git a/Observer/Publisher.cs b/Observer/Publisher.cs new file mode 100644 index 0000000..ef81d3c --- /dev/null +++ b/Observer/Publisher.cs @@ -0,0 +1,35 @@ +public class Publisher : IPublisher +{ + public int IssueNumber { get; set; } = 0; + + private List _subscribers = new List(); + + public void Subscribe(ISubscriber subscriber) + { + Console.WriteLine($"Publisher: {subscriber.Name} just subscribed"); + _subscribers.Add(subscriber); + } + + public void Unsubscribe(ISubscriber subscriber) + { + _subscribers.Remove(subscriber); + Console.WriteLine($"Publisher: {subscriber.Name} just unsubscribed"); + } + + public void Notify() + { + Console.WriteLine("Publisher: Notify subscribers..."); + + foreach (var subscriber in _subscribers) + { + subscriber.Update(this); + } + } + + public void PublishNewsletter() + { + this.IssueNumber++; + Console.WriteLine($"Published #{this.IssueNumber} of Newsletter"); + this.Notify(); + } +} \ No newline at end of file diff --git a/Observer/Subscriber.cs b/Observer/Subscriber.cs new file mode 100644 index 0000000..3cabbaf --- /dev/null +++ b/Observer/Subscriber.cs @@ -0,0 +1,24 @@ +public class Subscriber : ISubscriber +{ + private int _latestIssue = 0; + + public Subscriber(string name) + { + Name = name ?? throw new ArgumentNullException(nameof(name)); + } + + public string Name + { + get; private set; + } + + public void Update(IPublisher publisher) + { + var concretePublisher = (publisher as Publisher); + if (concretePublisher.IssueNumber > _latestIssue) + { + _latestIssue = concretePublisher.IssueNumber; + Console.WriteLine($"{Name}: Received Issue {_latestIssue}"); + } + } +} \ No newline at end of file