observable design pattern
יש לממש observable design pattern
- public interface IObserver {
- void Update(ISubject subject);
- }
- public interface ISubject {
- void Attach(IObserver observer);
- void Detach(IObserver observer);
- void Notify();
- }
- public class Subject:Isubject {
- public int State {get;set;}
- private List<IObserver> _observers = new List<IObserver>();
- public void Attach(IObserver observer) {
- this._observers.Add(observer);
- }
- public void Detach(IObserver observer) {
- this._observers.Remove(observer);
- }
- public void Notify() {
- foreach(var observer in _observers) {
- observer.Update(this);
- }
- }
- public void SomeBusinessLogic() {
- this.State = new Random().Next(0,10);
- Thread.Sleep(15);
- this.Notify();
- }
- }