中介者模式学习总结

目录

 

中介者模式

引言

定义

模式结构图

实例

实例说明

实例类图

代码实现

总结

模式优点

模式缺点 


中介者模式

引言

对于那些存在对象之间复杂交互关系的系统,中介者模式提供了一种简化复杂交互的解决方案,它通过引入一个中介者,将原本对象之间的俩俩交互转换为每个对象与中介者之间的交互,中介者可以对象之间的通信进行控制与协调,极大降低了原有系统的耦合度,使得系统更加灵活,也更加易于扩展。

定义

英文定义:“Define an object that encapsultates how a set of objects interact.Mediator promotes loose coupling by keeping objects from referring to each other explicitly,and it lets you vary their interaction independently.”。

中文定义:用一个中介者对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其松耦合,而且可以独立地改变它们之间地交互。

                                       中介者模式重要等级★★☆☆☆    中介者模式难度等级★★★★☆

模式结构图

中介者模式学习总结_第1张图片

实例

实例说明

使用中介者模式设计一个聊天室,聊天室会对敏感字符进行过滤,钻石会员拥有踢人权限。

实例类图

中介者模式学习总结_第2张图片

代码实现

抽象聊天室类

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

abstract class AbstractChatroom
{
    protected Dictionary members = new Dictionary();

    public void Join(Member member)
    {
        members.Add(member.Name,member);
        member.Chatroom = this;
    }

    public virtual void SendText(string from,string to,string message)
    {
        members[to].ReceiveText(from,message);
    }

    public virtual void Remove(string from,string name)
    {
        members[from].Remove(name);
    }

    public void Remove(string name)
    {
        members.Remove(name);
    }
}

具体聊天室类:实现对敏感字符的屏蔽

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

class ChatGroup:AbstractChatroom
{
    public override void SendText(string from, string to, string message)
    {
        message = message.Replace("日","*");
        message = message.Replace("操", "爱");
        base.SendText(from, to, message);
    }
}

抽象会员类

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

abstract class Member
{
    private AbstractChatroom chatroom;

    private string name;

    public string Name
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }

    public AbstractChatroom Chatroom
    {
        get
        {
            return chatroom;
        }

        set
        {
            chatroom = value;
        }
    }

    public Member(string name)
    {
        this.Name = name;
    }
    public virtual void SendText(string to,string message)
    {
        Chatroom.SendText(name,to,message);
    }

    public virtual void ReceiveText(string from,string message)
    {
        Console.WriteLine("{0}@{1}:\n\t{2}\n", from,name,message);
    }

    public abstract void Remove(string name);
}

普通会员

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

class CommonMember : Member
{
    public CommonMember(string name) : base(name) { }

    public override void Remove(string name)
    {
        Console.WriteLine("--- 系统信息:Sorry,【{0}】你没有权限踢除【{1}】 ---", this.Name,name);
    }
}

钻石会员

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

class DiamondMember : Member
{
    public DiamondMember(string name) : base(name) { }

    public override void Remove(string name)
    {
        Console.WriteLine("--- 系统信息:【{1}】已被钻石会员【{0}】踢出聊天室 ---", this.Name, name);
        Chatroom.Remove(name);
    }
}

测试代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        AbstractChatroom room = new ChatGroup();
        Member a = new DiamondMember("刘醒");
        Member b = new CommonMember("梁非凡");

        room.Join(a);
        room.Join(b);

        b.SendText("刘醒","吖屎啦你!");
        a.SendText("梁非凡", "说对不住,非凡哥!");
        b.SendText("刘醒", "吖屎啦你!");
        a.SendText("梁非凡", "我操你啊!");
        b.Remove("刘醒");
        a.Remove("梁非凡");

        Console.ReadKey();
    }
}

运行截图

中介者模式学习总结_第3张图片

总结

模式优点

1)简化了对象之间的交互。将原本的网状结构转换成以中介者为中心的星状结构。

2)各个同事之间解耦。

3)简化了同事类的设计和实现。

模式缺点 

1)中介者包含了同事之间的交互细节,可能会出现中介者类非常复杂的情况,使得系统难以维护。

你可能感兴趣的:(设计模式)