设计模式-中介者模式
中介者模式(Mediator)定义一个对象封装一系列多个对象如何相互作用,使得对象间不需要显式地相互引用,从而使其耦合更加松散,并且还让我们可以独立变化多个对象相互作用。结构图如下:
以一个聊天室的例子进行说明,一个聊天室可以很多成员,成员可以加入不同的讨论组,聊天室就是一个中介,讨论组中的成员通过聊天室发送消息。结构图如下:
实现代码:
最后输出:
组A成员收到消息-UserA to UserB:你好,UserB
组B成员收到消息-UserB to UserC:你好,UserC
组A成员收到消息-UserC to UserA:你好,UserA
以一个聊天室的例子进行说明,一个聊天室可以很多成员,成员可以加入不同的讨论组,聊天室就是一个中介,讨论组中的成员通过聊天室发送消息。结构图如下:
实现代码:
//
IChatroom.h
class User;
class IChatroom
{
public :
IChatroom();
virtual ~ IChatroom();
virtual void Register(User * ) = 0 ;
virtual void Send( char * pFrom, char * pTo, char * pMsg) = 0 ;
};
// IChatroom.cpp
#include " stdafx.h "
#include " IChatroom.h "
IChatroom::IChatroom()
{
}
IChatroom:: ~ IChatroom()
{
}
// Chatroom.h
#include " IChatroom.h "
#include < map >
using namespace std;
class Chatroom : public IChatroom
{
public :
Chatroom();
virtual ~ Chatroom();
void Register(User * );
void Send( char * pFrom, char * pTo, char * pMsg);
private :
map < char * , User *> m_mapUsers;
};
// Chatroom.cpp
#include " stdafx.h "
#include " Chatroom.h "
#include " User.h "
Chatroom::Chatroom()
{
}
Chatroom:: ~ Chatroom()
{
}
void Chatroom::Register(User * pUser)
{
char * a = pUser -> m_pName;
if (m_mapUsers[pUser -> m_pName] == NULL)
{
m_mapUsers[pUser -> m_pName] = pUser;
}
pUser -> SetChatroom( this );
}
void Chatroom::Send( char * pFrom, char * pTo, char * pMsg)
{
User * pUserTo = (User * )m_mapUsers[pTo];
if (pUserTo != NULL)
{
pUserTo -> Receive(pFrom, pMsg);
}
}
// User.h
class Chatroom;
class User
{
public :
User( char * );
virtual ~ User();
void Send( char * pTo, char * pMsg);
virtual void Receive( char * pFrom, char * pMsg);
void SetChatroom(Chatroom * );
friend class Chatroom;
private :
char * m_pName;
Chatroom * m_pChatroom;
};
// User.cpp
#include " stdafx.h "
#include " User.h "
#include " Chatroom.h "
#include < iostream >
using namespace std;
User::User( char * pName)
{
m_pName = pName;
}
User:: ~ User()
{
if (m_pChatroom != NULL)
{
delete m_pChatroom;
m_pChatroom = NULL;
}
}
void User::Send( char * pTo, char * pMsg)
{
m_pChatroom -> Send(m_pName, pTo, pMsg);
}
void User::SetChatroom(Chatroom * pChatroom)
{
m_pChatroom = pChatroom;
}
void User::Receive( char * pFrom, char * pMsg)
{
cout << pFrom << " to " << this -> m_pName << " : " << pMsg << endl;
}
// UserInGroupA.h
#include " User.h "
class UserInGroupA : public User
{
public :
UserInGroupA( char * );
virtual ~ UserInGroupA();
virtual void Receive( char * pFrom, char * pMsg);
};
// UserInGroupA.cpp
#include " stdafx.h "
#include " UserInGroupA.h "
#include < iostream >
using namespace std;
UserInGroupA::UserInGroupA( char * pName) : User(pName)
{
}
UserInGroupA:: ~ UserInGroupA()
{
}
void UserInGroupA::Receive( char * pFrom, char * pMsg)
{
cout << " 组A成员收到消息- " ;
User::Receive(pFrom, pMsg);
}
// UserInGroupB.h
#include " User.h "
class UserInGroupB : public User
{
public :
UserInGroupB( char * );
virtual ~ UserInGroupB();
virtual void Receive( char * pFrom, char * pMsg);
};
// UserInGroupB.cpp
#include " stdafx.h "
#include " UserInGroupB.h "
#include < iostream >
using namespace std;
UserInGroupB::UserInGroupB( char * pName) : User(pName)
{
}
UserInGroupB:: ~ UserInGroupB()
{
}
void UserInGroupB::Receive( char * pFrom, char * pMsg)
{
cout << " 组B成员收到消息- " ;
User::Receive(pFrom, pMsg);
}
// main.cpp
#include " stdafx.h "
#include " Chatroom.h "
#include " UserInGroupA.h "
#include " UserInGroupB.h "
int main( int argc, char * argv[])
{
Chatroom * pChatroom = new Chatroom;
User * pUserA = new UserInGroupA( " UserA " );
User * pUserB = new UserInGroupA( " UserB " );
User * pUserC = new UserInGroupB( " UserC " );
pChatroom -> Register(pUserA);
pChatroom -> Register(pUserB);
pChatroom -> Register(pUserC);
pUserA -> Send( " UserB " , " 你好,UserB " );
pUserB -> Send( " UserC " , " 你好,UserC " );
pUserC -> Send( " UserA " , " 你好,UserA " );
return 0 ;
}
class User;
class IChatroom
{
public :
IChatroom();
virtual ~ IChatroom();
virtual void Register(User * ) = 0 ;
virtual void Send( char * pFrom, char * pTo, char * pMsg) = 0 ;
};
// IChatroom.cpp
#include " stdafx.h "
#include " IChatroom.h "
IChatroom::IChatroom()
{
}
IChatroom:: ~ IChatroom()
{
}
// Chatroom.h
#include " IChatroom.h "
#include < map >
using namespace std;
class Chatroom : public IChatroom
{
public :
Chatroom();
virtual ~ Chatroom();
void Register(User * );
void Send( char * pFrom, char * pTo, char * pMsg);
private :
map < char * , User *> m_mapUsers;
};
// Chatroom.cpp
#include " stdafx.h "
#include " Chatroom.h "
#include " User.h "
Chatroom::Chatroom()
{
}
Chatroom:: ~ Chatroom()
{
}
void Chatroom::Register(User * pUser)
{
char * a = pUser -> m_pName;
if (m_mapUsers[pUser -> m_pName] == NULL)
{
m_mapUsers[pUser -> m_pName] = pUser;
}
pUser -> SetChatroom( this );
}
void Chatroom::Send( char * pFrom, char * pTo, char * pMsg)
{
User * pUserTo = (User * )m_mapUsers[pTo];
if (pUserTo != NULL)
{
pUserTo -> Receive(pFrom, pMsg);
}
}
// User.h
class Chatroom;
class User
{
public :
User( char * );
virtual ~ User();
void Send( char * pTo, char * pMsg);
virtual void Receive( char * pFrom, char * pMsg);
void SetChatroom(Chatroom * );
friend class Chatroom;
private :
char * m_pName;
Chatroom * m_pChatroom;
};
// User.cpp
#include " stdafx.h "
#include " User.h "
#include " Chatroom.h "
#include < iostream >
using namespace std;
User::User( char * pName)
{
m_pName = pName;
}
User:: ~ User()
{
if (m_pChatroom != NULL)
{
delete m_pChatroom;
m_pChatroom = NULL;
}
}
void User::Send( char * pTo, char * pMsg)
{
m_pChatroom -> Send(m_pName, pTo, pMsg);
}
void User::SetChatroom(Chatroom * pChatroom)
{
m_pChatroom = pChatroom;
}
void User::Receive( char * pFrom, char * pMsg)
{
cout << pFrom << " to " << this -> m_pName << " : " << pMsg << endl;
}
// UserInGroupA.h
#include " User.h "
class UserInGroupA : public User
{
public :
UserInGroupA( char * );
virtual ~ UserInGroupA();
virtual void Receive( char * pFrom, char * pMsg);
};
// UserInGroupA.cpp
#include " stdafx.h "
#include " UserInGroupA.h "
#include < iostream >
using namespace std;
UserInGroupA::UserInGroupA( char * pName) : User(pName)
{
}
UserInGroupA:: ~ UserInGroupA()
{
}
void UserInGroupA::Receive( char * pFrom, char * pMsg)
{
cout << " 组A成员收到消息- " ;
User::Receive(pFrom, pMsg);
}
// UserInGroupB.h
#include " User.h "
class UserInGroupB : public User
{
public :
UserInGroupB( char * );
virtual ~ UserInGroupB();
virtual void Receive( char * pFrom, char * pMsg);
};
// UserInGroupB.cpp
#include " stdafx.h "
#include " UserInGroupB.h "
#include < iostream >
using namespace std;
UserInGroupB::UserInGroupB( char * pName) : User(pName)
{
}
UserInGroupB:: ~ UserInGroupB()
{
}
void UserInGroupB::Receive( char * pFrom, char * pMsg)
{
cout << " 组B成员收到消息- " ;
User::Receive(pFrom, pMsg);
}
// main.cpp
#include " stdafx.h "
#include " Chatroom.h "
#include " UserInGroupA.h "
#include " UserInGroupB.h "
int main( int argc, char * argv[])
{
Chatroom * pChatroom = new Chatroom;
User * pUserA = new UserInGroupA( " UserA " );
User * pUserB = new UserInGroupA( " UserB " );
User * pUserC = new UserInGroupB( " UserC " );
pChatroom -> Register(pUserA);
pChatroom -> Register(pUserB);
pChatroom -> Register(pUserC);
pUserA -> Send( " UserB " , " 你好,UserB " );
pUserB -> Send( " UserC " , " 你好,UserC " );
pUserC -> Send( " UserA " , " 你好,UserA " );
return 0 ;
}
最后输出:
组A成员收到消息-UserA to UserB:你好,UserB
组B成员收到消息-UserB to UserC:你好,UserC
组A成员收到消息-UserC to UserA:你好,UserA