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

            Feedback

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

            97久久精品人人澡人人爽| 亚洲v国产v天堂a无码久久| 久久精品毛片免费观看| 久久婷婷五月综合97色| 久久国产免费观看精品| 久久久99精品一区二区| 中文字幕无码久久人妻| 久久精品麻豆日日躁夜夜躁| 国内精品久久久久久久涩爱 | 亚洲国产精品久久66| 午夜精品久久久久久| 久久精品人人做人人妻人人玩| 精品久久人人妻人人做精品| 久久人人爽人人爽人人爽 | 久久w5ww成w人免费| 精品久久久久久久中文字幕| 久久精品国产亚洲av高清漫画 | 国产偷久久久精品专区| 久久福利青草精品资源站| 亚洲国产成人久久综合区| 中文精品久久久久国产网址| 久久AV高潮AV无码AV| 久久久久亚洲AV无码专区桃色 | 亚洲va久久久噜噜噜久久天堂 | 性高朝久久久久久久久久| 国产精品久久久久久影院| 国产aⅴ激情无码久久| 久久久久九国产精品| 国产精品久久久久久搜索 | 国产精品熟女福利久久AV| 久久棈精品久久久久久噜噜| 亚洲国产精品综合久久一线| 国产激情久久久久影院老熟女免费| 久久亚洲国产成人精品性色| 久久天天躁夜夜躁狠狠躁2022| 久久久久国产日韩精品网站| 国产精品免费久久久久电影网| 91视频国产91久久久| 狠狠88综合久久久久综合网| 久久水蜜桃亚洲av无码精品麻豆 | 久久精品国产亚洲AV电影|