Vezano za .NET, da li je observer u sustini ista stvar kao delegat u .NET-u i da li je u primeru kako sam ga ja sastvaio dole "Update()" u stvari ta Callback metoda?
Primer kako ja mislim da se radi sa observerom:
public partial class frmSubject : Form, ISubject
{
public List<IObserver> ObserversList = new List<IObserver>();
public frmSubject()
{
InitializeComponent();
RegisterObserver(new frmObserver());
}
private void btnNotify_Click(object sender, EventArgs e)
{
NotifyObservers();
}
public void RegisterObserver(IObserver observer)
{
if (observer is IObserver)
{
ObserversList.Add(observer);
}
}
public void RemoveObserver(IObserver observer)
{
if (observer is IObserver)
{
ObserversList.Remove(observer);
}
}
public void NotifyObservers()
{
foreach (IObserver io in ObserversList)
{
io.Update("Clicked");
}
}
}
public partial class frmObserver : Form, IObserver
{
public frmObserver()
{
InitializeComponent();
this.Show();
}
public void Update(string msg)
{
lstEmployee.Items.Add(msg);
lstManager.Items.Add(msg);
}
}
[Ovu poruku je menjao Shadowed dana 25.05.2010. u 17:44 GMT+1]