示例问题:
windows的消息机制,对于在Windows下做开发的人员来说,可以说是必备的知识。即键盘、鼠标、应用程序、应用程序窗口之前的消息通信,均通过Windows操作系统的消息队列、消息循环等完成,编写示例程序,描述应用程序窗口之间通过Windows操作系统完成相互通讯的过程。
分析:
若没有Windows操作系统提供的消息队列、消息循环等实现应用程序或应用程序窗口之间的通讯,而是由每个应用程序或者应用程序窗口之间直接完成通讯,那整个系统耦合程度就太高,就太过混乱了。Windows操作系统在应用程序或应用程序窗口通讯之间扮演的角色就是一个“中介者”。
解决方案:
Mediator.h
该文件实现了中介者的基类IMediator,及其子类COperationSysterm(操作系统)。窗口基类IMyWnd,及其子类CMyWndA(A类窗口)、CMyWndB(B类窗口)、CMyWndC(C类窗口)。窗口之间的通讯均通过中介者IMediator来完成。
#pragma once
#include <string>
#include <iostream>
#include <map>
class IMyWnd;
//中介者基类
class IMediator
{
public:
IMediator()
{
}
virtual ~IMediator()
{
}
virtual void InputWnd(std::string strName, IMyWnd* pWnd) = 0;
virtual void Send(std::string strTargetWndName, std::string strData) = 0;
};
//窗口基类
class IMyWnd
{
public:
IMyWnd(std::string strName, IMediator* pMediator)
: m_strName(strName)
, m_pMediator(pMediator)
{
if (nullptr != pMediator)
{
pMediator->InputWnd(m_strName, this);
}
}
virtual ~IMyWnd()
{
}
virtual void SendMessage(std::string strTargetWndName, std::string strData) = 0;
virtual void GetMessage(std::string strData) = 0;
std::string m_strName;
IMediator* m_pMediator;
};
//A类窗口
class CMyWndA : public IMyWnd
{
public:
CMyWndA(std::string strName, IMediator* pMediator) : IMyWnd(strName, pMediator)
{
}
virtual ~CMyWndA()
{
}
void SendMessage(std::string strTargetWndName, std::string strData)
{
if (nullptr != m_pMediator)
{
std::cout << m_strName << "(A类窗口)向" << strTargetWndName << "发送 : " << strData << std::endl;
m_pMediator->Send(strTargetWndName, strData);
}
}
void GetMessage(std::string strData)
{
std::cout << m_strName << "(A类窗口)收到 : " << strData << std::endl;
}
};
//B类窗口
class CMyWndB : public IMyWnd
{
public:
CMyWndB(std::string strName, IMediator* pMediator) : IMyWnd(strName, pMediator)
{
}
virtual ~CMyWndB()
{
}
void SendMessage(std::string strTargetWndName, std::string strData)
{
if (nullptr != m_pMediator)
{
std::cout << m_strName << "(B类窗口)向" << strTargetWndName << "发送 : " << strData << std::endl;
m_pMediator->Send(strTargetWndName, strData);
}
}
void GetMessage(std::string strData)
{
std::cout << m_strName << "(B类窗口)收到 : " << strData << std::endl;
}
};
//C类窗口
class CMyWndC : public IMyWnd
{
public:
CMyWndC(std::string strName, IMediator* pMediator) : IMyWnd(strName, pMediator)
{
}
virtual ~CMyWndC()
{
}
void SendMessage(std::string strTargetWndName, std::string strData)
{
if (nullptr != m_pMediator)
{
std::cout << m_strName << "(C类窗口)向" << strTargetWndName << "发送 : " << strData << std::endl;
m_pMediator->Send(strTargetWndName, strData);
}
}
void GetMessage(std::string strData)
{
std::cout << m_strName << "(C类窗口)收到 : " << strData << std::endl;
}
};
//操作系统
class COperationSysterm : public IMediator
{
public:
COperationSysterm()
{
}
virtual ~COperationSysterm()
{
}
void InputWnd(std::string strName, IMyWnd* pWnd)
{
m_mapWnd[strName] = pWnd;
}
void Send(std::string strTargetWndName, std::string strData)
{
IMyWnd* pWnd = m_mapWnd[strTargetWndName];
if (nullptr != pWnd)
{
pWnd->GetMessage(strData);
}
}
private:
std::map<std::string, IMyWnd*> m_mapWnd;
};
main.cpp
// main.cpp : Defines the entry point for the console application.
//
#include "Mediator.h"
int main()
{
COperationSysterm* pOS = new(std::nothrow) COperationSysterm();
CMyWndA* pWndA = new(std::nothrow) CMyWndA("配置窗口", pOS);
CMyWndB* pWndB = new(std::nothrow) CMyWndB("主界面窗口", pOS);
CMyWndC* pWndC = new(std::nothrow) CMyWndC("提示窗口", pOS);
pWndB->SendMessage("配置窗口", "更新配置");
pWndA->SendMessage("提示窗口", "提示配置保存成功");
system("pause");
return 0;
}
运行结果:
中介者模式的使用:
中介者模式:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地互相引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
中介者模式,又叫调停者模式、其实就是中间人或者调停者的意思。
中介者模式可避免多个对象之间交互过于复杂而导致修改一个对象对整个系统太大,中介者模式 将多个对象之间复杂的交互均通过中介者类来完成,将多个对象之间的复杂交互都变为对象和中介者类的交互。当然由于中介者控制了集中化,于是就把交互复杂性变成了中介者的复杂性,这就使得中介者变得比任何一个对象都复杂。但是细心维护一个中介者类比维护多个对象复杂的交互要清晰的多
何时使用中介者模式:
中介者模式一般应用于一组对象以定义良好但是复杂的方式进行通讯的场合,以及想定制一个分布在多个类中的行为,而又不想生成太多的子类的场合。
返回目录:设计模式(C++实现)(总)