中介者模式-C#实现


中介者模式,定义了一个中介对象来封装一系列对象之间的交互关系。
中介者使各个对象之间不需要显式地相互引用,从而使耦合性降低,而且可以独立地改变它们之间的交互行为。

中介者模式设计两个具体对象,一个是用户类,另一个是中介者类;
根据针对接口编程原则,则需要把这两类角色进行抽象,
所以中介者模式中就有了4类角色,它们分别是:
1,抽象中介者角色,
2,具体中介者角色、
3,抽象同事类,
4,具体同事类。
中介者类是起到协调各个对象的作用,则抽象中介者角色中则需要保存各个对象的引用。


中介者模式的适用场景
一组定义良好的对象,现在要进行复杂的相互通信。
想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类。


中介者模式具有以下几点优点:
简化了对象之间的关系,将系统的各个对象之间的相互关系进行封装,将各个同事类解耦,使得系统变为松耦合。
提供系统的灵活性,使得各个同事对象独立而易于复用。

缺点:
中介者模式中,中介者角色承担了较多的责任,所以一旦这个中介者对象出现了问题,整个系统将会受到重大的影响。
新增加一个同事类时,不得不去修改抽象中介者类和具体中介者类,此时可以使用观察者模式和状态模式来解决这个问题。

该实例基于WPF实现,直接上代码,下面为三层架构的代码。

一 Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.中介者模式
{
    //1,定义抽象电脑类
    public abstract class AbstractCompter
    {
        
        public string InterfaceName { get; set; }

        public AbstractCompter()
        {
            InterfaceName = "type-c接口";
        }

        /// <summary>
        /// 设备数量改表
        /// </summary>
        /// <param name="deviceNum">设备接口名称</param>
        /// <param name="mediator">中介者类</param>
        public abstract void ChangeDeviceNum(string InterfaceName, AbstractMediator mediator);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.中介者模式
{
    //2,定义抽象中介者类
    public abstract class AbstractMediator
    {
        protected AbstractCompter XiaoMi;
        protected AbstractCompter Apple;
        protected AbstractCompter HuaWei;
        protected AbstractCompter LianXiang;

        protected AbstractMediator(AbstractCompter xiaoMi, AbstractCompter apple, AbstractCompter huaWei, AbstractCompter lianXiang)
        {
            XiaoMi = xiaoMi;
            Apple = apple;
            HuaWei = huaWei;
            LianXiang = lianXiang;
        }

        public abstract void XiaoMiInterface(string InterfaceName);
        public abstract void AppleInterface(string InterfaceName);
        public abstract void HuaWeiInterface(string InterfaceName);
        public abstract void LianXiangInterface(string InterfaceName);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.中介者模式
{
    //苹果电脑
    public class AppleCompter : AbstractCompter
    {
        //依赖与抽象中介者对象
        public override void ChangeDeviceNum(string InterfaceName, AbstractMediator mediator)
        {
            mediator.AppleInterface(InterfaceName);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.中介者模式
{
    //创建实体电脑:华为
    public class HuaWeiCompter : AbstractCompter
    {
        public override void ChangeDeviceNum(string InterfaceName, AbstractMediator mediator)
        {
            mediator.HuaWeiInterface(InterfaceName);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.中介者模式
{
    //创建实体电脑:联想
    public class LianXinagCompter : AbstractCompter
    {
        public override void ChangeDeviceNum(string InterfaceName, AbstractMediator mediator)
        {
            mediator.LianXiangInterface(InterfaceName);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.中介者模式
{
    //3,具体中介者类
    public class MediatorPater : AbstractMediator
    {
        public MediatorPater(AbstractCompter xiaoMi, AbstractCompter apple,
            AbstractCompter huaWei, AbstractCompter lianXiang) : base(xiaoMi, apple, huaWei, lianXiang)
        {
        }

        public override void AppleInterface(string InterfaceName)
        {
            Apple.InterfaceName = InterfaceName;
        }

        public override void HuaWeiInterface(string InterfaceName)
        {
            HuaWei.InterfaceName = InterfaceName;
        }

        public override void LianXiangInterface(string InterfaceName)
        {
            LianXiang.InterfaceName = InterfaceName;
        }

        public override void XiaoMiInterface(string InterfaceName)
        {
            XiaoMi.InterfaceName = InterfaceName;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.中介者模式
{
    //创建实体电脑类:小米
    public class XiaoMiCompter : AbstractCompter
    {
        public override void ChangeDeviceNum(string InterfaceName, AbstractMediator mediator)
        {
            mediator.XiaoMiInterface(InterfaceName);
        }
    }
}

二 View

<Window x:Class="设计模式练习.View.中介者模式.MediatorWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:设计模式练习.View.中介者模式"
        mc:Ignorable="d"
        Title="MediatorWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Text="{Binding Res}"/>
            <TextBlock Grid.Row="1" Text="{Binding Res2}"/>
            <TextBlock Grid.Row="2" Text="{Binding Res3}"/>
            <TextBlock Grid.Row="3" Text="{Binding Res4}"/>
        </Grid>
        <Button Content="中介者发送命令" Grid.Column="1" Command="{Binding sendCommand}"/>
    </Grid>
</Window>

三 ViewModel

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 设计模式练习.Model.中介者模式;

namespace 设计模式练习.ViewModel.中介者模式
{
    partial class MediatorWindow_ViewModel : ObservableObject
    {
        [ObservableProperty]
        private string res;

        [ObservableProperty]
        private string res2;

        [ObservableProperty]
        private string res3;

        [ObservableProperty]
        private string res4;


        [RelayCommand]
        private void send()
        {
            AbstractCompter A = new XiaoMiCompter();
            AbstractCompter B = new AppleCompter();
            AbstractCompter C = new HuaWeiCompter();
            AbstractCompter D = new LianXinagCompter();

   
            //初始化中介者
            AbstractMediator mediator = new MediatorPater(A, B, C, D);

            //随机变更接口
            string[] Is = { "Type-A", "Type-B", "Type-C", "Type-D", };
            int index = new Random().Next(0, Is.Length);

            //A, B, C, D统一变更接口
            A.ChangeDeviceNum(Is[index],mediator);
            B.ChangeDeviceNum(Is[index], mediator);
            C.ChangeDeviceNum(Is[index], mediator);
            D.ChangeDeviceNum(Is[index], mediator);

            Res = A.InterfaceName;
            Res2 = B.InterfaceName;
            Res3 = C.InterfaceName;
            Res4 = D.InterfaceName;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_shenbing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值