• <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 閱讀(1093) 評論(1)  編輯 收藏 引用

            Feedback

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

            国产 亚洲 欧美 另类 久久| 伊人久久大香线蕉成人| 久久精品国产清高在天天线| 亚洲va中文字幕无码久久| 无码精品久久久天天影视| 99久久精品费精品国产| 久久久久国产视频电影| 伊人久久国产免费观看视频| 色欲久久久天天天综合网精品| 久久久九九有精品国产| 91麻豆国产精品91久久久| 久久狠狠高潮亚洲精品| 久久综合给合综合久久| 狠狠狠色丁香婷婷综合久久五月| 亚洲成av人片不卡无码久久| 国内精品人妻无码久久久影院| 久久久青草青青国产亚洲免观| 久久精品人人做人人妻人人玩| 国产亚洲精午夜久久久久久| 久久婷婷五月综合97色一本一本| 欧洲性大片xxxxx久久久| 国产亚洲精品美女久久久| 亚洲国产成人乱码精品女人久久久不卡| 无码国内精品久久人妻蜜桃 | 久久亚洲中文字幕精品一区| 无码人妻久久一区二区三区| 怡红院日本一道日本久久| 久久综合亚洲鲁鲁五月天| 国产成人无码精品久久久免费| 亚洲级αV无码毛片久久精品 | 精品人妻伦一二三区久久| 国内精品久久久久影院优| 亚洲狠狠婷婷综合久久久久| 久久人妻少妇嫩草AV蜜桃| 久久久噜噜噜久久中文字幕色伊伊| 国内精品久久九九国产精品| 1000部精品久久久久久久久| 久久r热这里有精品视频| 久久久av波多野一区二区| 精品久久久久中文字幕日本| 亚洲va久久久噜噜噜久久狠狠|