Projektdateien hinzufügen.
This commit is contained in:
43
WindowsFormsApp1/OP/Subject.cs
Normal file
43
WindowsFormsApp1/OP/Subject.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace WindowsFormsApp1.OP
|
||||
{
|
||||
/// <summary>
|
||||
/// Owns some important state and notifies observers when the state changes.
|
||||
/// </summary>
|
||||
public class Subject<T> : ISubject<T>
|
||||
{
|
||||
private protected List<IObserver<T>> _observers = null;
|
||||
private object state { get;set; }
|
||||
|
||||
public Subject()
|
||||
{
|
||||
_observers = new List<IObserver<T>>();
|
||||
}
|
||||
|
||||
public virtual void Subscribe(IObserver<T> observer)
|
||||
{
|
||||
_observers.Add(observer);
|
||||
}
|
||||
public virtual void Unsubscribe(IObserver<T> observer)
|
||||
{
|
||||
_observers.Remove(observer);
|
||||
}
|
||||
public void Notify()
|
||||
{
|
||||
foreach (IObserver<T> observer in _observers)
|
||||
{
|
||||
observer.Update(this);
|
||||
}
|
||||
}
|
||||
public virtual void SetState(T state)
|
||||
{
|
||||
this.state = state;
|
||||
Notify();
|
||||
}
|
||||
public virtual T GetState()
|
||||
{
|
||||
return (T)state;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user