public void CompareNotifyContext() { // Create observer passing in notification method and context IObserver observer = new Observer("observerTestMethod", this); var negTestObj = new object(); // test assertions Assert.IsTrue(observer.CompareNotifyContext(negTestObj) == false, "Expecting observer.compareNotifyContext(negTestObj) == false"); Assert.IsTrue(observer.CompareNotifyContext(this), "Expecting observer.compareNotifyContext(this) == true"); }
public void ObserverAccessors() { // Create observer with null args, then // use accessors to set notification method and context IObserver observer = new Observer(null, null); observer.NotifyContext = this; observer.NotifyMethod = "observerTestMethod"; // create a test event, setting a payload value and notify // the observer with it. since the observer is this class // and the notification method is observerTestMethod, // successful notification will result in our local // observerTestVar being set to the value we pass in // on the note body. INotification note = new Notification("ObserverTestNote", 10); observer.NotifyObserver(note); // test assertions Assert.IsTrue(observerTestVar == 10, "Expecting observerTestVar = 10"); }
//注册视图组件 public virtual void RegisterMediator(IMediator mediator) { lock(m_syncLocker) { if (m_mediatorMap.ContainsKey(mediator.MediatorName)) return; m_mediatorMap[mediator.MediatorName] = mediator; //获取观察者上所有的消息 IList<string> interests = mediator.ListNotificationInterests(); if (interests.Count > 0) { IObserver server = new Observer("handleNotification",mediator); for (int i = 0; i < interests.Count; ++i ) { //notification列表 RegisterObserver(interests[i], server); } } } mediator.OnRegister(); }
public void ObserverConstructor() { // Create observer passing in notification method and context IObserver observer = new Observer("observerTestMethod", this); // create a test note, setting a body value and notify // the observer with it. since the observer is this class // and the notification method is observerTestMethod, // successful notification will result in our local // observerTestVar being set to the value we pass in // on the note body. INotification note = new Notification("ObserverTestNote", 5); observer.NotifyObserver(note); // test assertions Assert.IsTrue(observerTestVar == 5, "Expecting observerTestVar = 5"); }
/// <summary> /// Register an <c>IMediator</c> instance with the <c>View</c> /// </summary> /// <param name="mediator">A reference to the <c>IMediator</c> instance</param> /// <remarks> /// <para>Registers the <c>IMediator</c> so that it can be retrieved by name, and further interrogates the <c>IMediator</c> for its <c>INotification</c> interests</para> /// <para>If the <c>IMediator</c> returns any <c>INotification</c> names to be notified about, an <c>Observer</c> is created encapsulating the <c>IMediator</c> instance's <c>handleNotification</c> method and registering it as an <c>Observer</c> for all <c>INotifications</c> the <c>IMediator</c> is interested in</para> /// </remarks> /// <remarks>This method is thread safe and needs to be thread safe in all implementations.</remarks> public virtual void RegisterMediator(IMediator mediator) { lock (m_mediatorMap) { // do not allow re-registration (you must to removeMediator fist) if (m_mediatorMap.ContainsKey(mediator.MediatorName)) return; mediator.InitializeNotifier(m_multitonKey); // Register the Mediator for retrieval by name m_mediatorMap[mediator.MediatorName] = mediator; // Get Notification interests, if any. var interests = mediator.ListNotificationInterests; // Register Mediator as an observer for each of its notification interests // Create Observer IObserver observer = new Observer("handleNotification", mediator); // Register Mediator as Observer for its list of Notification interests foreach (var t in interests) { RegisterObserver(t, observer); } } // alert the mediator that it has been registered mediator.OnRegister(); }
public void RegisterAndNotifyObserver() { // Get the Singleton View instance IView view = View.Instance; // Create observer, passing in notification method and context IObserver observer = new Observer("ViewTestMethod", this); // Register Observer's interest in a particulat Notification with the View string name = Thread.CurrentThread.Name; if (name == null) name = ""; lock (m_viewTestVarsLock) { viewTestVars.Remove(name); } view.RegisterObserver(ViewTestNote.NAME + name, observer); // Create a ViewTestNote, setting // a body value, and tell the View to notify // Observers. Since the Observer is this class // and the notification method is viewTestMethod, // successful notification will result in our local // viewTestVar being set to the value we pass in // on the note body. INotification note = ViewTestNote.Create(name, 10); view.NotifyObservers(note); // test assertions Assert.IsTrue(viewTestVars.ContainsKey(name), "Expecting viewTestVars.ContainsKey(name)"); Assert.IsTrue(viewTestVars[name] == 10, "Expecting viewTestVar[name] = 10"); view.RemoveObserver(ViewTestNote.NAME + name, this); }
/// <summary> /// Register an <c>IMediator</c> instance with the <c>View</c> /// </summary> /// <param name="mediator">A reference to the <c>IMediator</c> instance</param> /// <remarks> /// <para>Registers the <c>IMediator</c> so that it can be retrieved by name, and further interrogates the <c>IMediator</c> for its <c>INotification</c> interests</para> /// <para>If the <c>IMediator</c> returns any <c>INotification</c> names to be notified about, an <c>Observer</c> is created encapsulating the <c>IMediator</c> instance's <c>handleNotification</c> method and registering it as an <c>Observer</c> for all <c>INotifications</c> the <c>IMediator</c> is interested in</para> /// </remarks> /// <remarks>This method is thread safe and needs to be thread safe in all implementations.</remarks> public virtual void RegisterMediator(IMediator mediator) { lock (m_syncRoot) { // do not allow re-registration (you must to removeMediator fist) if (m_mediatorMap.ContainsKey(mediator.MediatorName)) return; // Register the Mediator for retrieval by name m_mediatorMap[mediator.MediatorName] = mediator; // Get Notification interests, if any. IList<string> interests = mediator.ListNotificationInterests(); // Register Mediator as an observer for each of its notification interests if (interests.Count > 0) { // Create Observer IObserver observer = new Observer("handleNotification", mediator); // Register Mediator as Observer for its list of Notification interests for (int i = 0; i < interests.Count; i++) { RegisterObserver(interests[i].ToString(), observer); } } } // alert the mediator that it has been registered mediator.OnRegister(); }