设计模式---Mediator Pattern

Mediator Pattern

中介者模式:用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散。

Mediator 角色分析

中介者模式包含四个角色:

  • Mediator: 抽象中介者
    用于定义一个接口,该接口用于与各同事对象之间的通信;
  • ConcreteMediator: 具体中介者
    是抽象中介者的子类,通过协调各个同事对象来实现协作行为,了解并维护它的各个同事对象的引用;
  • Colleague: 抽象同事类
    抽象同事类定义各同事的公有方法;
  • ConcreteColleague: 具体同事类
    具体同事类是抽象同事类的子类,每一个同事对象都引用一个中介者对象;每一个同事对象在需要和其他同事对象通信时,先与中介者通信,通过中介者来间接完成与其他同事类的通信;

类图

在这里插入图片描述

代码示例

下面代码模拟,两个同事通过中介(mediator)互发消息。

/*
 *Meditor Pattern :中介模式
 */
using System;

namespace Pattern01
{
    class Program
    {
        static void Main(string[] args)
        {

            ConcreteMediator m = new ConcreteMediator();

            ConcreteColleague1 c1 = new ConcreteColleague1(m);
            Concretecolleague2 c2 = new Concretecolleague2(m);

            m.Colleague1 = c1;
            m.Colleague2 = c2;


            //模拟两个colleague通过mediator 来通信,此处mediator是一个转发器的角色。
            //将对象之间的交互,解耦,抽象出中介者(mediator)角色,所有对象只和中介交互。

            string msg = "嗨,最近怎么样?";
            Console.WriteLine("c1 发出消息: " + msg);
            
            c1.Send(msg);

            string ack = "哈哈,我还行";
            Console.WriteLine("c2 发出消息: "+ack);
            c2.Send(ack);

            Console.ReadKey();
        }
    }


    /// <summary>
    /// Mediator
    /// </summary>
    abstract class Mediator
    {
        public abstract void Send(string message, Colleague colleague);
    }
    /// <summary>
    /// ConcreteMediator,
    /// </summary>
    class ConcreteMediator : Mediator
    {
        private ConcreteColleague1 _colleage1;
        private Concretecolleague2 _colleague2;

        public ConcreteColleague1 Colleague1
        {
            set { _colleage1 = value; }
        }
        public Concretecolleague2 Colleague2
        {
            set { _colleague2 = value; }
        }
        /// <summary>
        /// 将colleague1 与colleague2 交换信息。
        /// </summary>
        /// <param name="message"></param>
        /// <param name="colleague"></param>
        public override void Send(string message, Colleague colleague)
        {
        //主要业务逻辑
            if (colleague == _colleage1)
            {
                _colleague2.Notify(message);
            }
            else
            {
                _colleage1.Notify(message);
            }
        }
    }
    /// <summary>
    /// Colleague
    /// </summary>
    abstract class Colleague
    {
        protected Mediator mediator;//维护一个Mediator 对象
        public Colleague(Mediator mediator)
        {
            this.mediator = mediator;
        }
    }
    /// <summary>
    /// ConcreteColleague
    /// </summary>
    class ConcreteColleague1 : Colleague
    {
        public ConcreteColleague1(Mediator mediator) : base(mediator)
        { }
        public void Send(string message)
        {
            mediator.Send(message, this);
        }
        public void Notify(string message)
        {
            Console.WriteLine("Colleague1 收到 message:" + message); 
        }
    }
    class Concretecolleague2 : Colleague
    {
        public Concretecolleague2(Mediator mediator) : base(mediator)
        {
        }
        public void Send(string message)
        {
            mediator.Send(message, this);
        }
        public void Notify(string message)
        {
            Console.WriteLine("Colleague2 收到 meaasge: " + message);
        }
    }

}

总结

  • Mediator 接口,大多数情况下定义一个方法就够用了
  • ConcreteMediator 用于管理所有的Colleagues对象
  • 每个具体的colleague对象都有一个mediator的引用,可以在其构造函数中,将mediator作为参数传入。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值