---Mediator mode of design pattern

Source: Internet
Author: User
Mediator (mediator) mode (mediator) defines an object that encapsulates how a series of objects interact so that objects do not need to be explicitly referenced to each other, which makes them more loosely coupled, and allows us to change the interaction of multiple objects independently. The structure diagram is as follows:



With a chat room example, a chat room can be a lot of members, members can join different discussion groups, chat room is an intermediary, the members of the discussion group to send messages through the chat room. The structure diagram is as follows:


Implementation code:
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 << E Ndl
}

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 << "Group A member receives message-";
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 << "Group B members Receive message-";
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", "Hello, UserB");
Puserb, Send ("UserC", "Hello, UserC");
Puserc, Send ("UserA", "Hello, UserA");

return 0;
}
Final output:
Group A member receives message-usera to UserB: Hello, UserB.
Group B member receives message-userb to UserC: Hello, UserC.
Group A member receives message-USERC to UserA: Hello, UserA original address: http://www.cppblog.com/emptysoul/archive/2009/02/15/73892.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.