示例#1
0
        public ObserverTest()
        {
            _subject   = new ConcreteSubject();
            _observerA = new ConcreteObserverA(_subject);
            _observerB = new ConcreteObserverB(_subject);

            _subject.Attach(_observerA);
            _subject.Attach(_observerB);
        }
示例#2
0
    private void Start()
    {
        ConcreteSubjectA subjectA = new ConcreteSubjectA();

        IObserver observerA = new ConcreteObserverA(subjectA);
        IObserver observerB = new ConcreteObserverB(subjectA);

        subjectA.AddObserber(observerA);
        subjectA.AddObserber(observerB);

        //改变subjectA的状态
        subjectA.State = "状态A";
    }
        public void RunTest()
        {
            var o1 = new ConcreteObserverA();
            var o2 = new ConcreteObserverB();

            var subject = new Subject();

            subject.RegisterObserver(o1);
            subject.RegisterObserver(o2);

            subject.NotifyListeners();

            Console.WriteLine("Listeners notified!");
            Assert.Pass();
        }
示例#4
0
        static void Main(string[] args)
        {
            var subject = new Subject();

            var concreteObserverA = new ConcreteObserverA();
            var concreteObserverB = new ConcreteObserverB();

            subject.Notify += concreteObserverA.Update;
            subject.Notify += concreteObserverB.Update;

            subject.StateDelegate += concreteObserverA.Update;
            subject.StateDelegate += concreteObserverB.Update;

            subject.SomeBusinessLogic();
        }
示例#5
0
        static void Main(string[] args)
        {
            //Observer Pattern
            var subject   = new Subject();
            var observerA = new ConcreteObserverA();

            subject.Attach(observerA);

            var observerB = new ConcreteObserverB();

            subject.Attach(observerB);
            subject.SomeBusinessLogic();
            subject.SomeBusinessLogic();
            subject.Detach(observerB);
            subject.SomeBusinessLogic();
        }
示例#6
0
        private static void DemonstrateObserver()
        {
            var subject   = new Subject();
            var observerA = new ConcreteObserverA();
            var observerB = new ConcreteObserverB();

            observerA.Subscribe(subject);
            observerB.Subscribe(subject);

            subject.AddItem(2);
            subject.AddItem(3);

            observerA.Unsubscribe();
            subject.AddItem(5);
            observerB.Unsubscribe();
        }
示例#7
0
        public static void RunPubSub()
        {
            var subject   = new Subject();
            var observerA = new ConcreteObserverA();

            subject.Attach(observerA);

            var observerB = new ConcreteObserverB();

            subject.Attach(observerB);

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

            subject.Detach(observerB);

            subject.SomeBusinessLogic();
        }
示例#8
0
        public static void ClientCode()
        {
            var subj = new Subject();
            var o1   = new ConcreteObserverA();

            subj.attach(o1);

            var o2 = new ConcreteObserverB();

            subj.attach(o2);

            subj.someBusinessLogic();
            subj.someBusinessLogic();

            subj.detach(o2);

            subj.someBusinessLogic();
        }
示例#9
0
        public void ObserverTest()
        {
            // The client code.
            var subject   = new Subject();
            var observerA = new ConcreteObserverA();

            subject.Attach(observerA);

            var observerB = new ConcreteObserverB();

            subject.Attach(observerB);

            subject.SomeBusinessLogic().Should().BeInRange(0, 10);
            subject.SomeBusinessLogic().Should().BeInRange(0, 10);

            subject.Detach(observerB);

            subject.SomeBusinessLogic().Should().BeInRange(0, 10);
        }
示例#10
0
        public void Get()
        {
            // The client code.
            var subject   = new Subject();
            var observerA = new ConcreteObserverA();

            subject.Attach(observerA);

            var observerB = new ConcreteObserverB();

            subject.Attach(observerB);

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

            subject.Detach(observerB);

            subject.SomeBusinessLogic();
        }
示例#11
0
        static void MainABC(string[] args)
        {
            // The client code.
            var subject   = new ConcreteSubject();
            var observerA = new ConcreteObserverA();

            subject.Attach(observerA);

            var observerB = new ConcreteObserverB();

            subject.Attach(observerB);

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

            subject.Detach(observerB);

            subject.SomeBusinessLogic();
        }
示例#12
0
    static void Main(string[] args)
    {
        // The client code.
        var subject   = new Subject();
        var observerA = new ConcreteObserverA();

        subject.AddOberver(observerA);

        var observerB = new ConcreteObserverB();

        subject.AddOberver(observerB);

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

        subject.DeleteOberver(observerB);

        subject.SomeBusinessLogic();
    }
示例#13
0
        private void RunObserverTest()
        {
            ConcreteObservable someProcess = new ConcreteObservable();

            for (int i = 0; i < 10; i++)
            {
                if (i % 2 == 0)
                {
                    var tempObject = new ConcreteObserverA(String.Format("ObserverA{0}", i), someProcess);
                }
                else
                {
                    var tempObject = new ConcreteObserverB(String.Format("ObserverB{0}", i), someProcess);
                }
            }
            for (int i = 0; i < 10; i++)
            {
                someProcess.ReinitInnerState();
            }
        }