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 ();
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");
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
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.