public class SubscriptionEngine {
List<Subscription> _listOfSubscriptions;
public SubscriptionEngine()
{
_listOfSubscriptions = new List<Subscription>();
}
public bool Contains(Subscription item)
{
foreach (Subscription subscription in _listOfSubscriptions)
{
if (subscription.Observer_.Name == item.Observer_.Name
&& subscription.Subject_.SubjectName == item.Subject_.SubjectName)
return true;
}
return false;
}
public void Register(Subject s, Observer o)
{
Subscription item = new Subscription(s, o);
if (!this.Contains(item))
_listOfSubscriptions.Add(new Subscription(s, o));
}
public void Unregister(Subject s, Observer o)
{
for (int i = 0; i < _listOfSubscriptions.Count; i++)
{
if (_listOfSubscriptions[i].Observer_ == o && _listOfSubscriptions[i].Subject_ == s)
_listOfSubscriptions.RemoveAt(i);
}
}
public void Notify(Subject s)
{
foreach (Subscription subscription in _listOfSubscriptions)
{
if (subscription.Observer_.Update(s))
MessageBox.Show(s.SubjectName + " has sent a message to " + subscription.Observer_.Name);
}
}
}
|