Example #1
0
        public void Update(ConcreteSubjectAccount account)
        {
            var state = account.State;
            var data  = account.Data;

            if (state == LoginStatus.EXPIRED)
            {
                Console.WriteLine($"Account {data.Email} has expired. Email sent!");
            }
        }
Example #2
0
        public void Update(ConcreteSubjectAccount account)
        {
            var state = account.State;
            var data  = account.Data;

            if (state == LoginStatus.INVALID)
            {
                Console.WriteLine($"Account {data.Email} with ip {data.IP} are trying to hack our system");
            }
        }
        public void Update(ConcreteSubjectAccount account)
        {
            var state = account.State;
            var data  = account.Data;

            if (state == LoginStatus.SUCCESS)
            {
                Console.WriteLine($"User {data.Email} vừa online");
            }
        }
        public static void Run()
        {
            Console.WriteLine("===== Observer, Chain of Responsibility =====");
            Console.WriteLine("\n");

            var account  = new ConcreteSubjectAccount();
            var security = new ConcreteObserverSecurity();
            var logger   = new ConcreteObserverLogger();
            var mailer   = new ConcreteObserverMailer();

            // Attach các observer vào subject
            account.Attach(security);
            account.Attach(logger);
            account.Attach(mailer);

            // Login
            account.Login(new LoginRequestModel
            {
                Email = "*****@*****.**",
                IP    = "192.168.0.1"
            });

            // Change state
            account.State = LoginStatus.EXPIRED;
            account.Save();

            // Login again
            account.Login(new LoginRequestModel
            {
                Email = "*****@*****.**",
                IP    = "127.0.0.1"
            });

            // Xóa security observer
            account.Detach(security);
            account.Login(new LoginRequestModel
            {
                Email = "*****@*****.**",
                IP    = "127.0.0.2"
            });

            Console.WriteLine("\n");
        }