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));
        }
Example #3
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);
        }
Example #4
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);
        }
Example #5
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();
    }
Example #6
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();
        }
Example #7
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();
        }
Example #8
0
        static void Main(string[] args)
        {
            var sujet     = new Sujet();
            var observer  = new Observer();
            var observerB = new ObserverB();

            Console.ForegroundColor = ConsoleColor.White;

            sujet.AjouterObserver(observer);
            sujet.AjouterObserver(observerB);

            for (int i = 0; i < 10; i++)
            {
                sujet.Affichage();
                Console.WriteLine(Environment.NewLine);
            }

            sujet.SuprimerObserver(observer);
            sujet.SuprimerObserver(observerB);


            Console.ReadLine();
        }