• 设计模式之-中介者模式


    中介者模式是一个行为设计模式,它允许我们公开一个统一的接口,系统的 不同部分 可以通过该接口进行 通信,而 不需要 显示的相互作用;

    主要解决:对象与对象之间存在大量的关联关系,这样势必会导致系统的结构变得很复杂,同时若一个对象发生改变,我们也需要跟踪与之相关联的对象,同时做出相应的处理。

    关键:各个原本关联的对象不在互相关联,而是由中介者掌握他们关联的信息。类似我们在找中介租房,我们并不知道房东是谁,房东也不知道我们是谁,因为房东把房子的信息交给了中介。因此,在中介者模式中,只需要让中介者知道如何关联即可。

    以下例子是我的理解,或许有不正确的地方。

        class Program
        {
            static void Main(string[] args)
            {
                var register = new Dictionary<Person, Person>();
                IMediator mediator = new Mediator(register);
                var student = new Student(mediator,"萌新","学生");
                var teacher = new Teacher(mediator, "大佬", "老师");
    
                register.Add(student, teacher);
                register.Add(teacher, student);
                student.Send("老师你好");
                teacher.Send("你好同学");
                Console.ReadKey();
            }
        }
    
        public abstract class Person
        {
            /// <summary>
            /// 构造函数。
            /// </summary>
            /// <param name="name">名字。</param>
            /// <param name="identity">身份。</param>
            protected Person(string name, string identity)
            {
                this.Name = name;
                this.Identity = identity;
            }
    
            /// <summary>
            /// 名字。
            /// </summary>
            public string Name { get; }
    
            /// <summary>
            /// 身份。
            /// </summary>
            public string Identity { get; }
    
            /// <summary>
            /// 发送信息。
            /// </summary>
            /// <param name="msg">信息。</param>
            public abstract void Send(string msg);
    
            /// <summary>
            /// 接收信息。
            /// </summary>
            /// <param name="msg">信息。</param>
            public abstract void Receive(string msg);
        }
    
        /// <summary>
        /// 中介者接口。
        /// </summary>
        public interface IMediator
        {
            /// <summary>
            /// 转发信息。
            /// </summary>
            /// <param name="msg">信息。</param>
            /// <param name="person">信息发起人。</param>
            void Transmit(string msg, Person person);
        }
    
        /// <summary>
        /// 实现类。
        /// </summary>
        public class Mediator : IMediator
        {
            /// <summary>
            /// 信息发起人对信息接收人。
            /// </summary>
            private readonly Dictionary<Person, Person> _register;
    
            /// <summary>
            /// 由中介者管理关系。
            /// </summary>
            /// <param name="register"></param>
            public Mediator(Dictionary<Person, Person> register)
            {
                _register = register;
            }
    
            /// <summary>
            /// 中介者调用关系。
            /// </summary>
            /// <param name="msg">信息。</param>
            /// <param name="person">发信息的人。</param>
            public void Transmit(string msg, Person person)
            {
                var pr = _register[person];
                pr.Receive(msg);
            }
        }
    
        public class Student : Person
        {
            /// <summary>
            /// 中介者接口。
            /// </summary>
            private readonly IMediator _mediator;
    
            /// <summary>
            /// 构造函数。
            /// </summary>
            /// <param name="mediator">中介者。</param>
            /// <param name="name">名字。</param>
            /// <param name="identity">身份。</param>
            public Student(IMediator mediator, string name, string identity) : base(name, identity)
            {
                _mediator = mediator;
            }
    
            /// <summary>
            /// 接收信息。
            /// </summary>
            /// <param name="msg">信息。</param>
            public override void Receive(string msg)
            {
                Console.WriteLine($"{Identity}-{Name}:收到信息:{msg}");
            }
    
            /// <summary>
            /// 发送信息。
            /// </summary>
            /// <param name="msg">信息。</param>
            public override void Send(string msg)
            {
                _mediator.Transmit(msg, this);
            }
        }
    
        public class Teacher : Person
        {
            private readonly IMediator _mediator;
    
            public Teacher(IMediator mediator, string name, string identity) : base(name, identity)
            {
                _mediator = mediator;
            }
    
            public override void Receive(string msg)
            {
                Console.WriteLine($"{Identity}-{Name}:收到信息:{msg}");
            }
    
            public override void Send(string msg)
            {
                _mediator.Transmit(msg, this);
            }
        }

    由nuget包:MediatR的请求响应模式的中介者。

        class Program
        {
            static void Main(string[] args)
            {
                var register = new Dictionary<Person, Person>();
                var student = new Student("萌新", "学生");
                var teacher = new Teacher("大佬", "老师");
                register.Add(student, teacher);
                register.Add(teacher, student);
                var service = new ServiceCollection();
                service.AddMediatR(typeof(Program));
                service.AddScoped<IMediatorHandler, MediatorHandler>();
                service.AddScoped<SenPersonHandler>();
                service.AddSingleton(register);
                service.AddScoped<IRequestHandler<Student, bool>, SenPersonHandler>();
                service.AddScoped<IRequestHandler<Teacher, bool>, SenPersonHandler>();
                var provide = service.BuildServiceProvider();
                var medi = provide.GetService<IMediatorHandler>();
                student.Send("老师你好");
                medi.SendRequest(student);
                teacher.Send("你好同学");
                medi.SendRequest(teacher);
                Console.ReadKey();
            }
        }
    
        /// <summary>
        /// 由MediatR需要的实现关联。
        /// </summary>
        public class SenPersonHandler : IRequestHandler<Student, bool>, IRequestHandler<Teacher, bool>
        {
            private readonly Dictionary<Person, Person> _register;
            public SenPersonHandler(Dictionary<Person, Person> register)
            {
                _register = register;
            }
    
            public Task<bool> Handle(Student request, CancellationToken cancellationToken)
            {
                var pr = _register[request];
                pr.Receive(request.Msg);
                return Task.FromResult(true);
            }
    
            public Task<bool> Handle(Teacher request, CancellationToken cancellationToken)
            {
                var pr = _register[request];
                pr.Receive(request.Msg);
                return Task.FromResult(true);
            }
        }
    
        /// <summary>
        /// 人。
        /// </summary>
        public abstract class Person : IRequest<bool>
        {
            /// <summary>
            /// 信息。
            /// </summary>
            public string Msg { get; protected set; }
    
            /// <summary>
            /// 必须要性名和身份。
            /// </summary>
            /// <param name="name">名字。</param>
            /// <param name="identity">身份。</param>
            protected Person(string name, string identity)
            {
                Name = name;
                Identity = identity;
            }
    
            /// <summary>
            /// 名字。
            /// </summary>
            public string Name { get; }
    
            /// <summary>
            /// 身份信息。
            /// </summary>
            public string Identity { get; }
    
            /// <summary>
            /// 发送。
            /// </summary>
            /// <param name="msg">发送信息。</param>
            public abstract void Send(string msg);
    
            /// <summary>
            /// 接收信息。
            /// </summary>
            /// <param name="msg">信息。</param>
            public abstract void Receive(string msg);
        }
    
        /// <summary>
        /// 中介者接口。
        /// </summary>
        public interface IMediatorHandler
        {
            /// <summary>
            /// 发送请求。
            /// </summary>
            /// <typeparam name="T">类型。</typeparam>
            /// <param name="request">请求。</param>
            /// <returns></returns>
            Task SendRequest<T>(T request) where T : Person;
        }
    
        /// <summary>
        /// 中介者实现类。
        /// </summary>
        public class MediatorHandler : IMediatorHandler
        {
            /// <summary>
            /// 内置IMediator。
            /// </summary>
            private readonly IMediator _mediator;
    
            public MediatorHandler(IMediator mediator)
            {
                _mediator = mediator;
            }
    
            /// <summary>
            /// 发送请求。
            /// </summary>
            /// <typeparam name="T">Person类型。</typeparam>
            /// <param name="request">请求者。</param>
            /// <returns></returns>
            public Task SendRequest<T>(T request) where T : Person
            {
                _mediator.Send(request);
                return Task.CompletedTask;
            }
        }
    
        public class Student : Person
        {
            public Student(string name, string identity) : base(name, identity)
            {
            }
    
            public override void Receive(string msg)
            {
                Console.WriteLine($"{Identity}-{Name}:收到信息:{msg}");
            }
    
            public override void Send(string msg)
            {
                Msg = msg;
            }
        }
    
    
        public class Teacher : Person
        {
            public Teacher(string name, string identity) : base(name, identity)
            {
            }
    
            public override void Receive(string msg)
            {
                Console.WriteLine($"{Identity}-{Name}:收到信息:{msg}");
            }
    
            public override void Send(string msg)
            {
                Msg = msg;
            }
        }

  • 相关阅读:
    IDEA中设置自动build-改动代码,不用重启工程,刷新页面即可
    前端上传大文件并支持中途取消上传
    网页上传大文件并支持中途取消上传
    HTTP上传大文件并支持中途取消上传
    B/S上传大文件并支持中途取消上传
    如何将word公式粘贴到CKEditor里面
    如何将word公式粘贴到HTML编辑器里面
    如何将word图片粘贴到HTML编辑器里面
    如何将word图片粘贴到Web编辑器里面
    如何将word公式粘贴到Web编辑器里面
  • 原文地址:https://www.cnblogs.com/yeqifeng2288/p/13026343.html
Copyright © 2020-2023  润新知