You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Observer.Observers
|
|
|
|
|
{
|
|
|
|
|
internal class Subject : ISubject
|
|
|
|
|
{
|
|
|
|
|
public int State { get; set; } = -0;
|
|
|
|
|
|
|
|
|
|
private List<IObserver> _observers = new List<IObserver>();
|
|
|
|
|
|
|
|
|
|
public void Attach(IObserver observer)
|
|
|
|
|
{
|
|
|
|
|
_observers.Add(observer);
|
|
|
|
|
Console.WriteLine("Subject: Attached an observers.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Detach(IObserver observer)
|
|
|
|
|
{
|
|
|
|
|
_observers.Remove(observer);
|
|
|
|
|
Console.WriteLine("Subject: Detached an observers.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Notify()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Subject: Notifying observers...");
|
|
|
|
|
foreach (IObserver observer in _observers)
|
|
|
|
|
{
|
|
|
|
|
observer.Update(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SomeBusinessLogic()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("Subject: I'm doing something important.");
|
|
|
|
|
this.State = new Random().Next(0, 10);
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(15);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"Subject: My state has just changed to: {this.State}");
|
|
|
|
|
Notify();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|