我用过的设计模式(5)-- 中介者模式
发布日期:2021-06-30 19:47:13
浏览次数:3
分类:技术文章
本文共 2424 字,大约阅读时间需要 8 分钟。
文章目录
前言
什么是中介者模式呢?看上面那张图应该是一目了然了吧。
我在做FTP本地云小项目的后端建设时用了中介者模式,将几个大块的职责分布到不同的服务器中去。我有写日报的好习惯,所以当时的事情是可以基本复现的。
具体在这篇:,细节可查。中介者模式
又叫调停者模式,但是我更喜欢中介者这个名字,因为一下就表达出这个设计的意图了。
用一个中介对象封装一系列对象交互,中介者使各对象不需要显示的相互作用,从而使其耦合松散,而且可以改变他们之间的交互。
缺点
中介者负荷过高。
应用场景
用于一堆乱麻式的交互场景。中介者模式简单,但是也容易误用,不要为了使用中介者模式而使用它。
案例:大型相亲现场 VS 传统媒人模式
(圆的是女的,五角星是男的)
有没有发现这个图,耦合度极高。
好,现在哪两对互看不顺眼了,要换人,然后再换人,再换人,这个图越弄越乱了。
媒人相亲模式是这样的:
这样就实现了每对男男女女之间的解耦。有啥事儿跟中间人说一下,让中间人去解决。
但是这样中间人的责任就过大,是违背了单一职责原则。因为中间人就需要认识那么多人,你有办法?
中介者模式的优势就在于它的集中处理,但是缺点也在这里。
中介者模式类图
类图代码阐释
#includeusing namespace std;class Mediator; //定义一个类,但是不马上实现,这招我以前还真没试过。涨知识了//因为下面两个类要互包//抽象同事类class Colleague { protected : Mediator *mediator;public: Colleague(Mediator* m) { mediator = m; } virtual void Sent(string message) = 0;};//抽象中介者类class Mediator { public: virtual void Send(string message, Colleague *colleague) = 0;};//具体同事类class ConcreteColleague1 :public Colleague { public: ConcreteColleague1(Mediator* m) : Colleague(m) { } void Sent(string message) { mediator->Send(message, this); } void Notify(string message) { cout << "同事1得到消息:" << message << endl; }};class ConcreteColleague2 :public Colleague { public: ConcreteColleague2(Mediator* m) : Colleague(m) { } void Sent(string message) { mediator->Send(message, this); } void Notify(string message) { cout << "同事2得到消息:" << message << endl; }};//具体中介者类class Concretemediator :public Mediator { private: ConcreteColleague1* colleague1; ConcreteColleague2* colleague2;public://无法将父类对象转化为子类对象,所以采用第二种方法 /*void SetColleague1(Colleague* pColleague){ colleague1 = pColleague; } void SetColleague2(Colleague* pColleague){ colleague2 = pColleague; }*/ void SetColleague1(Colleague* pColleague){ colleague1 = dynamic_cast (pColleague); } void SetColleague2(Colleague * pColleague){ colleague2 = dynamic_cast (pColleague); } void Send(string message, Colleague* colleague){ if (colleague == colleague1) colleague2->Notify(message); else if (colleague == colleague2) colleague1->Notify(message); }};int main(){ Concretemediator* mediator = new Concretemediator(); ConcreteColleague1* colleague1 = new ConcreteColleague1(mediator); //让他俩认识一下中介 ConcreteColleague2* colleague2 = new ConcreteColleague2(mediator); mediator->SetColleague1(colleague1); //让中介认识一下他俩 mediator->SetColleague2(colleague2); colleague1->Sent("有男朋友没?"); colleague2->Sent("有了"); return 0;}
转载地址:https://lion-wu.blog.csdn.net/article/details/113763456 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月11日 00时20分02秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
操作系统实验之猴子过桥问题的模拟程序
2019-04-30
POJ - 3067 Japan (树状数组 思维)
2019-04-30
POJ - 2352 Stars (树状数组 入门题)
2019-04-30
HDU - 1166 敌兵布阵 (树状数组模板题/线段树模板题)
2019-04-30
CodeForces - 761C Dasha and Password (思维 暴力)
2019-04-30
POJ - 2481 Cows (树状数组 入门题)
2019-04-30
ACM-ICPC 2018 焦作赛区网络预赛 I. Save the Room
2019-04-30
CodeForces - 987C Three displays (暴力/dp)
2019-04-30
计蒜客 NAIPC 2016 F. Mountain Scenes(dp)
2019-04-30
牛客国庆集训派对Day4——I 连通块计数(思维)
2019-04-30
牛客国庆集训派对Day4——G 区间权值(找规律,双重前缀和)
2019-04-30
牛客国庆集训派对Day5——L 数论之神(找规律/数论)
2019-04-30
CodeForces - 629B Far Relative’s Problem(模拟)
2019-04-30
CodeForces - 1059B Forgery (思维)
2019-04-30
CodeForces - 456C Boredom (dp)
2019-04-30
CodeForces - 960B Minimize the error (思维,贪心)
2019-04-30