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

            Feedback

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

            国产精品女同久久久久电影院| 97久久精品午夜一区二区| 欧美国产精品久久高清| 精品久久久久久国产| 久久免费小视频| 日韩AV无码久久一区二区| 久久久久九九精品影院| 亚洲国产二区三区久久| 无码精品久久久久久人妻中字| 久久久久无码中| 国产成人精品久久| 久久综合亚洲色HEZYO社区 | 久久久精品国产Sm最大网站| 久久久久亚洲精品中文字幕| 午夜不卡久久精品无码免费| 精品久久人人爽天天玩人人妻 | 奇米影视7777久久精品人人爽| 国产精品久久久久久一区二区三区| 青青青青久久精品国产h久久精品五福影院1421| 久久久久亚洲AV片无码下载蜜桃| 久久精品国产半推半就| 51久久夜色精品国产| 亚洲精品乱码久久久久久按摩| 欧美激情精品久久久久久| 久久免费小视频| 久久r热这里有精品视频| 色欲久久久天天天综合网| 久久久久久久综合综合狠狠| 97精品国产91久久久久久| 热re99久久精品国99热| 伊人久久大香线蕉亚洲| 欧美日韩久久中文字幕| 亚洲?V乱码久久精品蜜桃 | 五月丁香综合激情六月久久| 三级韩国一区久久二区综合| 国产精品99久久久久久董美香| 国内精品久久久久影院网站 | 婷婷久久精品国产| 开心久久婷婷综合中文字幕| 青青热久久国产久精品| 亚洲国产成人久久综合野外|