装饰器模式,实现aop

在不改变原来类的基础上,给原来的类的某方法扩展功能

 public class DirectorAop
    {
        public static void Show()
        {
            User user = new User() {
                Name = "jump",
                Password="123123"
            };

            Console.WriteLine("原来只干注册的事情");
            IUserProcessor userProcessor = new UserProcessor();
            userProcessor.RegUser(user);

            Console.WriteLine("实现aop后,的装饰器模式实现:");
            userProcessor = new UserProcessorDirecotr(userProcessor);//装饰了一层
            userProcessor.RegUser(user);

        }
    }

    public class User
    {
        public string Name { get; set; }
        public string Password { get; set; }
    }

    public interface IUserProcessor
    {
        void RegUser(User user);
    }

    /// <summary>
    /// 需要被装饰的类【就是要被扩展功能的类】
    /// </summary>
    public class UserProcessor : IUserProcessor
    {
        public void RegUser(User user)
        {
            Console.WriteLine("注册用户:" + user.Name);
        }
    }

    /// <summary>
    /// 装饰器类【就是扩展功能的类】
    /// </summary>
    public class UserProcessorDirecotr : IUserProcessor
    {
        private IUserProcessor _userProcessor;
        public UserProcessorDirecotr(IUserProcessor userProcessor)
        {
            this._userProcessor = userProcessor;
        }
        public void RegUser(User user)
        {
            Console.WriteLine("注册前,做点其他的吧");
            _userProcessor.RegUser(user);
            Console.WriteLine("注册成功,发个邮件吧");
        }
    }

 

posted @ 2020-01-08 10:18  凌晨10点13分  阅读(224)  评论(0编辑  收藏  举报