Ejemplo n.º 1
0
    private void Start()
    {
        SubjectA  sA = new SubjectA();
        ObserverA oA = new ObserverA(sA);

        //ObserverB oB = new ObserverB(sA);
        sA.AttachObserver(oA);
        sA.AttachObserver(new ObserverB(sA));

        sA.SetSubjectName("状态2");
    }
Ejemplo n.º 2
0
        public void Test_Subscribe_OneSub_SubCreateAndSubscribe()
        {
            ObsInt    oiA  = new ObsInt(17);
            ObserverA obsA = new ObserverA();

            Assert.AreEqual(obsA.InitialState, obsA.State);

            oiA.Value = 22;
            Assert.AreEqual(obsA.InitialState, obsA.State);

            oiA.ValueChanged += obsA.MethodA;
            Assert.AreEqual(obsA.InitialState, obsA.State);
        }
Ejemplo n.º 3
0
 private static void TraditionalDemo()
 {
     using (var subject = new Traditional.Observables.Subject())
         using (var observerA = new ObserverA(subject))
             using (var observerB = new ObserverB(subject))
             {
                 for (int i = 0; i < 10; i++)
                 {
                     subject.ChangeState();
                     Thread.Sleep(i);
                 }
             }
 }
        public void Observer_Test()
        {
            var sub  = new Subject();
            var obsA = new ObserverA(sub);
            var obsB = new ObserverB(sub);

            sub.State = "Mike";

            var resA = obsA.State;
            var resB = obsB.State;

            Assert.IsTrue(resA.Contains(sub.State));
            Assert.IsTrue(resB.Contains(sub.State));
        }
Ejemplo n.º 5
0
        public void Test_Subscribe_TwoSub_Complete()
        {
            ObsInt    oiA  = new ObsInt(17);
            ObserverA obsA = new ObserverA();
            ObserverB obsB = new ObserverB();

            Assert.AreEqual(obsA.InitialState, obsA.State);
            Assert.AreEqual(obsB.InitialState, obsB.State);

            oiA.ValueChanged += obsA.MethodA;
            oiA.ValueChanged += obsB.MethodB;
            oiA.Value         = 35;
            Assert.AreEqual($"Value was updated to 35", obsA.State);
            Assert.AreEqual($"Value is now 35", obsB.State);
        }
Ejemplo n.º 6
0
        public void Test_Subscribe_OneSub_Complete()
        {
            ObsInt    oiA  = new ObsInt(17);
            ObserverA obsA = new ObserverA();

            Assert.AreEqual(obsA.InitialState, obsA.State);

            oiA.Value = 22;
            Assert.AreEqual(obsA.InitialState, obsA.State);

            oiA.ValueChanged += obsA.MethodA;
            oiA.Value         = 35;
            Assert.AreNotEqual(obsA.InitialState, obsA.State);
            Assert.AreNotEqual($"Value was updated to 22", obsA.State);
            Assert.AreEqual($"Value was updated to 35", obsA.State);
        }
Ejemplo n.º 7
0
        public void ObserverPattern_TestIfObbserversWerNotified()
        {
            // Arrange
            var subject   = new SubjectObserver();
            var observerA = new ObserverA(subject);
            var observerB = new ObserverB(subject);

            // Act
            const string state = "bla bla";

            subject.BuisnessMethod(state);
            subject.Notify();

            // Assert
            Assert.AreEqual(observerA.State, state);
            Assert.AreEqual(observerB.State, state);
        }
Ejemplo n.º 8
0
    static void Main(string[] args)
    {
        var subject   = new Subject();
        var observerA = new ObserverA();

        subject.Attach(observerA);

        var observerB = new ObserverB();

        subject.Attach(observerB);

        subject.SomeBusinessLogic();
        subject.SomeBusinessLogic();

        subject.Detach(observerB);

        subject.SomeBusinessLogic();
    }
Ejemplo n.º 9
0
        public void DisplayExample()
        {
            var subject = new Subject();

            var observerA = new ObserverA();
            var observerB = new ObserverB();

            subject.Attach(observerA);
            subject.Attach(observerB);

            subject.TriggerExample();

            subject.Detach(observerA);

            Thread.Sleep(20); // Simulate time

            subject.TriggerExample();
        }
Ejemplo n.º 10
0
        public void Observer()
        {
            // permet à des objets de s'inscrire à des events d'un autre objet
            // permet d'éviter de boucler périodiquement pour checker l'état #Hedia #Polling

            Subject subject = new Subject();

            ObserverA observerA = new ObserverA();

            subject.Attach(observerA);

            ObserverB observerB = new ObserverB();

            subject.Attach(observerB);

            subject.SomeWork();
            subject.SomeWork();

            subject.Detach(observerB);

            subject.SomeWork();
        }
Ejemplo n.º 11
0
        public void Test_Subscribe_OneSub_SubCreate()
        {
            ObserverA obsA = new ObserverA();

            Assert.AreEqual(obsA.InitialState, obsA.State);
        }