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

            Feedback

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

            日韩一区二区久久久久久| 波多野结衣中文字幕久久 | 三级片免费观看久久| 日本亚洲色大成网站WWW久久| 久久久人妻精品无码一区 | a级毛片无码兔费真人久久| 99久久人妻无码精品系列蜜桃| 潮喷大喷水系列无码久久精品 | 精品人妻伦九区久久AAA片69| 伊人久久大香线蕉精品不卡 | 99久久精品国产一区二区蜜芽| 久久亚洲欧洲国产综合| 色欲综合久久躁天天躁蜜桃| 国产精品免费久久| 日本人妻丰满熟妇久久久久久| 九九久久精品国产| 99久久国语露脸精品国产| 亚洲欧美精品一区久久中文字幕 | 国产精品美女久久久久AV福利| 老色鬼久久亚洲AV综合| 久久人人超碰精品CAOPOREN| 国产成人精品白浆久久69| 伊人久久大香线蕉无码麻豆| 久久久久18| 国产精品免费久久久久久久久| 精品熟女少妇av免费久久| 欧美日韩精品久久久免费观看| 精品久久久久久久久久中文字幕 | 久久精品国产AV一区二区三区| 办公室久久精品| 99久久综合狠狠综合久久止| 精品久久8x国产免费观看| 久久精品国产亚洲av麻豆蜜芽| 亚洲日韩欧美一区久久久久我| 久久精品国产精品亚洲人人| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 伊人久久免费视频| 久久综合丁香激情久久| 久久这里只有精品久久| 日本精品久久久久中文字幕8| 国产欧美久久久精品|