• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            訪問者模式(Visitor)是一種分離對象數據結構與行為的方法,通過這種分離,可以為一個已存在的類或類群增加新的操作而無需為它們作任何修改。 結構圖為:


            公司的人事評估需要人事部訪問每一個員工列表,逐個對員工作出評估,人事部有兩個訪問者,一個訪問者評估員工假期,另一個評估員工薪資。
            實現代碼:
            //Element.h
            class Visitor;
            class Element  
            {
            public:
                Element();
                
            virtual ~Element();

                
            virtual void Accept(Visitor*= 0;
                friend 
            class Visitor;
            };

            //Element.cpp
            #include "stdafx.h"
            #include 
            "Element.h"

            Element::Element()
            {

            }

            Element::
            ~Element()
            {

            }

            //Employee.h
            #include "Element.h"

            class Employee : public Element
            {
            public:
                Employee(
            char*doubleint);
                
            virtual ~Employee();

                
            void Accept(Visitor*);
                
            char* GetName();
                
            double GetSalary();
                
            int GetVacationDays();
                
            void SetSalary(double);
                
            void SetVacationDays(int);
            private:
                
            char* m_pName;
                
            double m_dSalary;
                
            int m_nVacationDays;
            };

            //Employee.cpp
            #include "stdafx.h"
            #include 
            "Employee.h"
            #include 
            "Visitor.h"

            Employee::Employee(
            char* pName, double dSalary, int nVacationDays)
            {
                m_pName 
            = pName;
                m_dSalary 
            = dSalary;
                m_nVacationDays 
            = nVacationDays;
            }

            Employee::
            ~Employee()
            {

            }

            void Employee::Accept(Visitor* pVisitor)
            {
                pVisitor
            ->Visit(this);
            }

            char* Employee::GetName()
            {
                
            return m_pName;
            }

            double Employee::GetSalary()
            {
                
            return m_dSalary;
            }

            int Employee::GetVacationDays()
            {
                
            return m_nVacationDays;
            }

            void Employee::SetSalary(double dSalary)
            {
                m_dSalary 
            = dSalary;
            }

            void Employee::SetVacationDays(int nVacationDays)
            {
                m_nVacationDays 
            = nVacationDays;
            }

            //Visitor.h
            class Element;
            class Visitor  
            {
            public:
                
            virtual ~Visitor() = 0;

                
            virtual void Visit(Element*= 0;
            protected:
                Visitor();
            };

            //Visitor.cpp
            #include "stdafx.h"
            #include 
            "Visitor.h"

            Visitor::Visitor()
            {

            }

            Visitor::
            ~Visitor()
            {

            }

            //IncomeVisitor.h
            #include "Visitor.h"

            class IncomeVisitor : public Visitor
            {
            public:
                IncomeVisitor();
                
            virtual ~IncomeVisitor();

                
            void Visit(Element*);
            };

            //IncomeVisitor.cpp
            #include "stdafx.h"
            #include 
            "IncomeVisitor.h"
            #include 
            "Employee.h"
            #include 
            <iostream>

            using namespace std;

            IncomeVisitor::IncomeVisitor()
            {

            }

            IncomeVisitor::
            ~IncomeVisitor()
            {

            }

            void IncomeVisitor::Visit(Element* pElement)
            {
                
            //訪問者提高員工10%的薪資
                Employee* pEmployee = static_cast<Employee*>(pElement);
                
            double dSalary = pEmployee->GetSalary();
                pEmployee
            ->SetSalary(dSalary * 1.10);
                cout 
            << "員工 " << pEmployee->GetName() 
                    
            << " 的新工資是:" << pEmployee->GetSalary() << "" << endl;
            }

            //VacationVisitor.h
            #include "Visitor.h"

            class VacationVisitor : public Visitor
            {
            public:
                VacationVisitor();
                
            virtual ~VacationVisitor();

                
            void Visit(Element*);
            };

            //VacationVisitor.cpp
            #include "stdafx.h"
            #include 
            "VacationVisitor.h"
            #include 
            "Employee.h"
            #include 
            <iostream>

            using namespace std;

            VacationVisitor::VacationVisitor()
            {

            }

            VacationVisitor::
            ~VacationVisitor()
            {

            }

            void VacationVisitor::Visit(Element* pElement)
            {
                
            //訪問者為員工增加3天假期
                Employee* pEmployee = static_cast<Employee*>(pElement);
                
            int nVacationDays = pEmployee->GetVacationDays();
                pEmployee
            ->SetVacationDays(nVacationDays + 3);
                cout 
            << "員工 " << pEmployee->GetName() 
                    
            << " 的新假期是:" << pEmployee->GetVacationDays() << "" << endl;
            }

            //main.cpp
            #include "stdafx.h"
            #include 
            "Employee.h"
            #include 
            "IncomeVisitor.h"
            #include 
            "VacationVisitor.h"

            int main(int argc, char* argv[])
            {
                Element
            * pEmployeeA = new Employee("張三"10000.0010);
                Element
            * pEmployeeB = new Employee("李四"15000.0020);

                IncomeVisitor incomeV;
                VacationVisitor vacationV;

                incomeV.Visit(pEmployeeA);
                vacationV.Visit(pEmployeeB);

                
            return 0;
            }

            代碼中,我們通過訪問者(IncomeVisitor、VacationVisitor)對員工張三與李四進行評估,并給張三增加10%的工資,給李四增加3天假期。

            最后輸出為:
            員工 張三 的新工資是:11000元
            員工 李四 的新假期是:23天

            posted on 2009-02-21 20:21 emptysoul 閱讀(1102) 評論(1)  編輯 收藏 引用

            Feedback

            # re: 設計模式-訪問者模式[未登錄] 2012-09-15 00:19 Matrix
            你好 我發現無論用reinterpret_cast還是static_cast 都無法Employee* pEmployee = static_cast<Employee*>(pElement);轉化成功。  回復  更多評論
              

            一本色道久久88综合日韩精品 | 国产精品久久久久蜜芽| 无码人妻少妇久久中文字幕| 日本加勒比久久精品| 一本一道久久综合狠狠老| 久久久亚洲欧洲日产国码aⅴ| MM131亚洲国产美女久久| 久久久精品久久久久久 | 久久精品黄AA片一区二区三区| 国产精品久久久久久吹潮| 久久se精品一区精品二区国产| 久久亚洲精品国产精品婷婷 | 精品国产乱码久久久久久浪潮| 奇米影视7777久久精品人人爽| 久久久一本精品99久久精品88| 久久精品这里只有精99品| 乱亲女H秽乱长久久久| 久久久免费观成人影院| 国产高潮国产高潮久久久| 国产精品久久久久久久人人看| 蜜桃麻豆www久久| 久久精品国产亚洲AV无码偷窥| 久久精品国产国产精品四凭 | 国产精品九九九久久九九| 亚洲综合久久综合激情久久| 要久久爱在线免费观看| 伊人久久综在合线亚洲2019| 久久精品国产亚洲AV嫖农村妇女| 久久婷婷色综合一区二区| 国产精品美女久久久久AV福利| 久久人妻少妇嫩草AV无码专区| 尹人香蕉久久99天天拍| 久久最新免费视频| 人妻精品久久久久中文字幕一冢本| 久久久精品视频免费观看 | 久久亚洲电影| 国产ww久久久久久久久久| 91精品观看91久久久久久| 99久久国产热无码精品免费久久久久| 国产人久久人人人人爽| 久久久久AV综合网成人|