• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            emptysoul

              C++博客 :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            中介者模式(Mediator)定義一個(gè)對(duì)象封裝一系列多個(gè)對(duì)象如何相互作用,使得對(duì)象間不需要顯式地相互引用,從而使其耦合更加松散,并且還讓我們可以獨(dú)立變化多個(gè)對(duì)象相互作用。結(jié)構(gòu)圖如下:



            以一個(gè)聊天室的例子進(jìn)行說明,一個(gè)聊天室可以很多成員,成員可以加入不同的討論組,聊天室就是一個(gè)中介,討論組中的成員通過聊天室發(fā)送消息。結(jié)構(gòu)圖如下:


            實(shí)現(xiàn)代碼:
            //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;
            }

            最后輸出:
            組A成員收到消息-UserA to UserB:你好,UserB
            組B成員收到消息-UserB to UserC:你好,UserC
            組A成員收到消息-UserC to UserA:你好,UserA
            posted on 2009-02-15 20:50 emptysoul 閱讀(1486) 評(píng)論(1)  編輯 收藏 引用

            Feedback

            # re: 設(shè)計(jì)模式-中介者模式 2009-04-17 14:10 張濤
            這個(gè)例子在vc6.0下編譯不通過
            原因是user.h(2) : error C2011: 'User' : 'class' type redefinition  回復(fù)  更多評(píng)論
              


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            .精品久久久麻豆国产精品| 99久久人人爽亚洲精品美女| 99国内精品久久久久久久| 99久久综合国产精品免费| 久久最新免费视频| 久久久精品视频免费观看 | 91久久九九无码成人网站| 2021少妇久久久久久久久久| 欧美噜噜久久久XXX| 久久精品国产亚洲av影院| 亚洲中文字幕久久精品无码APP| 中文字幕无码久久人妻| 综合久久精品色| 伊人久久大香线蕉综合Av| 九九久久自然熟的香蕉图片| 欧美噜噜久久久XXX| 久久电影网2021| 久久人人爽人爽人人爽av| 久久91精品国产91久| 亚洲AV无码久久| 精品久久一区二区| 久久伊人精品青青草原日本| 久久久这里有精品| AV色综合久久天堂AV色综合在| 久久综合九色综合欧美狠狠| 久久久WWW成人免费精品| 狠狠色婷婷久久综合频道日韩| 欧美熟妇另类久久久久久不卡| 久久精品国产亚洲欧美| 亚洲精品99久久久久中文字幕| 亚洲精品无码久久千人斩| 麻豆精品久久精品色综合| 精品久久久久久久久免费影院| 99久久国语露脸精品国产| 亚洲欧美一区二区三区久久| 色婷婷久久综合中文久久蜜桃av| 久久无码av三级| 久久久久亚洲AV无码网站| 亚洲v国产v天堂a无码久久| 国内精品久久久久影院一蜜桃| 久久影院亚洲一区|