Skip to content

Commit 0164b3f

Browse files
committed
observer pattern example
1 parent 917edc6 commit 0164b3f

File tree

1 file changed

+84
-44
lines changed

1 file changed

+84
-44
lines changed

README.md

Lines changed: 84 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,76 +1795,116 @@ Translating our example from above. First of all we have job seekers that need t
17951795
```C#
17961796
class JobPost
17971797
{
1798-
protected $title;
1798+
public string Title { get; private set; }
17991799

1800-
public function __construct(string $title)
1801-
{
1802-
$this->title = $title;
1803-
}
1804-
1805-
public function getTitle()
1806-
{
1807-
return $this->title;
1808-
}
1800+
public JobPost(string title)
1801+
{
1802+
Title = title;
1803+
}
18091804
}
1810-
1811-
class JobSeeker implements Observer
1805+
class JobSeeker : IObserver<JobPost>
18121806
{
1813-
protected $name;
1807+
public string Name { get; private set; }
18141808

1815-
public function __construct(string $name)
1816-
{
1817-
$this->name = $name;
1818-
}
1809+
public JobSeeker(string name)
1810+
{
1811+
Name = name;
1812+
}
18191813

1820-
public function onJobPosted(JobPost $job)
1821-
{
1822-
// Do something with the job posting
1823-
echo 'Hi ' . $this->name . '! New job posted: '. $job->getTitle();
1824-
}
1814+
//Method is not being called by JobPostings class currently
1815+
public void OnCompleted()
1816+
{
1817+
//No Implementation
1818+
}
1819+
1820+
//Method is not being called by JobPostings class currently
1821+
public void OnError(Exception error)
1822+
{
1823+
//No Implementation
1824+
}
1825+
1826+
public void OnNext(JobPost value)
1827+
{
1828+
Console.WriteLine("Hi {0} ! New job posted: {1}", Name, value.Title);
1829+
}
18251830
}
18261831
```
18271832
Then we have our job postings to which the job seekers will subscribe
18281833
```C#
1829-
class JobPostings implements Observable
1834+
class JobPostings : IObservable<JobPost>
18301835
{
1831-
protected $observers = [];
1836+
private List<IObserver<JobPost>> mObservers;
1837+
private List<JobPost> mJobPostings;
18321838

1833-
protected function notify(JobPost $jobPosting)
1834-
{
1835-
foreach ($this->observers as $observer) {
1836-
$observer->onJobPosted($jobPosting);
1837-
}
1838-
}
1839+
public JobPostings()
1840+
{
1841+
mObservers = new List<IObserver<JobPost>>();
1842+
mJobPostings = new List<JobPost>();
1843+
}
18391844

1840-
public function attach(Observer $observer)
1845+
public IDisposable Subscribe(IObserver<JobPost> observer)
1846+
{
1847+
// Check whether observer is already registered. If not, add it
1848+
if (!mObservers.Contains(observer))
18411849
{
1842-
$this->observers[] = $observer;
1850+
mObservers.Add(observer);
18431851
}
1852+
return new Unsubscriber<JobPost>(mObservers, observer);
1853+
}
18441854

1845-
public function addJob(JobPost $jobPosting)
1855+
private void Notify(JobPost jobPost)
1856+
{
1857+
foreach(var observer in mObservers)
18461858
{
1847-
$this->notify($jobPosting);
1859+
observer.OnNext(jobPost);
18481860
}
1861+
}
1862+
1863+
public void AddJob(JobPost jobPost)
1864+
{
1865+
mJobPostings.Add(jobPost);
1866+
Notify(jobPost);
1867+
}
1868+
1869+
}
1870+
1871+
internal class Unsubscriber<JobPost> : IDisposable
1872+
{
1873+
private List<IObserver<JobPost>> mObservers;
1874+
private IObserver<JobPost> mObserver;
1875+
1876+
internal Unsubscriber(List<IObserver<JobPost>> observers, IObserver<JobPost> observer)
1877+
{
1878+
this.mObservers = observers;
1879+
this.mObserver = observer;
1880+
}
1881+
1882+
public void Dispose()
1883+
{
1884+
if (mObservers.Contains(mObserver))
1885+
mObservers.Remove(mObserver);
1886+
}
18491887
}
18501888
```
18511889
Then it can be used as
18521890
```C#
1853-
// Create subscribers
1854-
$johnDoe = new JobSeeker('John Doe');
1855-
$janeDoe = new JobSeeker('Jane Doe');
1891+
//Create Subscribers
1892+
var johnDoe = new JobSeeker("John Doe");
1893+
var janeDoe = new JobSeeker("Jane Doe");
18561894

1857-
// Create publisher and attach subscribers
1858-
$jobPostings = new JobPostings();
1859-
$jobPostings->attach($johnDoe);
1860-
$jobPostings->attach($janeDoe);
1895+
//Create publisher and attch subscribers
1896+
var jobPostings = new JobPostings();
1897+
jobPostings.Subscribe(johnDoe);
1898+
jobPostings.Subscribe(janeDoe);
18611899

1862-
// Add a new job and see if subscribers get notified
1863-
$jobPostings->addJob(new JobPost('Software Engineer'));
1900+
//Add a new job and see if subscribers get notified
1901+
jobPostings.AddJob(new JobPost("Software Engineer"));
18641902

1865-
// Output
1903+
//Output
18661904
// Hi John Doe! New job posted: Software Engineer
18671905
// Hi Jane Doe! New job posted: Software Engineer
1906+
1907+
Console.ReadLine();
18681908
```
18691909

18701910
🏃 Visitor

0 commit comments

Comments
 (0)