zoukankan      html  css  js  c++  java
  • 设计模式复习-访问者模式

    VisitorMode.h
    
    #pragma once
    #include<list>
    #include<string>
    #include<iostream>
    using namespace std;
    
    /*
    设计模式-访问者模式(Visitor)
    表示一个作用于某对象机构中各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于
    这些元素的新操作。
    */
    
    class CConcreteElementA;
    class CConcreteElementB;
    
    class CVisitor {//为该对象结构中ConcreteElement的每个类声明一个Visit操作。
    public:
    	virtual void VisitConcreteElementA(CConcreteElementA * pConcreteElementA) = 0;
    	virtual void VisitConcreteElementB(CConcreteElementB * pConcreteElementB) = 0;
    };
    
    class ConcreteVisitor1 :public CVisitor {//具体访问者类
    public:
    	void VisitConcreteElementA(CConcreteElementA * pConcreteElementA);
    	void VisitConcreteElementB(CConcreteElementB * pConcreteElementB);
    };
    
    class ConcreteVisitor2 :public CVisitor {//具体访问者类
    public:
    	void VisitConcreteElementA(CConcreteElementA * pConcreteElementA);
    	void VisitConcreteElementB(CConcreteElementB * pConcreteElementB);
    };
    
    class CElement {//定义一个Accept操作,它以一个访问者为参数
    public:
    	virtual void Accept(CVisitor *pVisitor) = 0;
    };
    
    class CConcreteElementA :public CElement {
    private:
    	string mstrName;
    public:
    	CConcreteElementA(const string &strName);
    	void Accept(CVisitor *pVisitor);
    	void OperationA() {}//A的单独方法
    	string GetName();
    };
    
    class CConcreteElementB :public CElement {
    private:
    	string mstrName;
    public:
    	CConcreteElementB(const string &strName);
    	void Accept(CVisitor *pVisitor);
    	void OperationB() {}//B的单独方法
    	string GetName();
    };
    
    class CobjuctStructure {//定义一个高层接口,以允许访问者访问它的元素。
    private:
    	list<CElement*>mlElement;
    public:
    	CobjuctStructure();
    	~CobjuctStructure();
    	void Attach(CElement *pElement);
    	void Eetach(CElement *pElement);
    	void Accept(CVisitor *pVisitor);
    };
    
    VisitorMode.cpp
    #include "stdafx.h"
    #include "VisitorMode.h"
    
    
    
    void ConcreteVisitor1::VisitConcreteElementA(CConcreteElementA * pConcreteElementA) {
    	cout << "1-A:" << pConcreteElementA->GetName() << endl;
    }
    void ConcreteVisitor1::VisitConcreteElementB(CConcreteElementB * pConcreteElementB) {
    	cout << "1-B:" << pConcreteElementB->GetName() << endl;
    }
    
    
    void ConcreteVisitor2::VisitConcreteElementA(CConcreteElementA * pConcreteElementA) {
    	cout << "2-A:" << pConcreteElementA->GetName() << endl;
    }
    void ConcreteVisitor2::VisitConcreteElementB(CConcreteElementB * pConcreteElementB) {
    	cout << "2-B:" << pConcreteElementB->GetName() << endl;
    }
    
    CConcreteElementA::CConcreteElementA(const string &strName) {
    	mstrName = strName;
    
    }
    void CConcreteElementA::Accept(CVisitor *pVisitor) {
    	pVisitor->VisitConcreteElementA(this);
    }
    string CConcreteElementA::GetName() {
    	return mstrName;
    }
    
    CConcreteElementB::CConcreteElementB(const string &strName) {
    	mstrName = strName;
    
    }
    void CConcreteElementB::Accept(CVisitor *pVisitor) {
    	pVisitor->VisitConcreteElementB(this);
    }
    string CConcreteElementB::GetName() {
    	return mstrName;
    }
    
    CobjuctStructure::CobjuctStructure() {
    	mlElement.clear();
    }
    CobjuctStructure::~CobjuctStructure() {
    	for each(CElement* index in mlElement) {
    		delete index;
    	}
    }
    void CobjuctStructure::Attach(CElement *pElement) {
    	mlElement.push_back(pElement);
    }
    void CobjuctStructure::Eetach(CElement *pElement) {
    	mlElement.remove(pElement);
    }
    void CobjuctStructure::Accept(CVisitor *pVisitor) {
    	for each (CElement * index in mlElement) {
    		index->Accept(pVisitor);
    	}
    }
    
    客户端调用
    
    #include<string>
    #include<iostream>
    #include "VisitorMode.h"
    using namespace std;
    
    int main() {
    
    	CobjuctStructure *pO = new CobjuctStructure();
    	pO->Attach(new CConcreteElementA("man"));
    	pO->Attach(new CConcreteElementB("woman"));
    
    	ConcreteVisitor1 *pV1 = new ConcreteVisitor1();
    	ConcreteVisitor2 *pV2 = new ConcreteVisitor2();
    
    	pO->Accept(pV1);
    	pO->Accept(pV2);
    
    	delete pO, delete pV1, delete pV2;
    
    	getchar();
    	return 0;
    }

  • 相关阅读:
    SDNU 1311.Binomial Coeffcients
    SDNU 1306.兑数
    SDNU 1272.SL的秘密
    SDNU 1270.超超的难题
    XCode 自动化打包总结
    Xrun 将 app 转化为 IPA
    mac终端下运行shell脚本
    ios 检测应用程序升级问题
    在iis6.0公布asp.net mvc3网站
    IOS 中 NSArray
  • 原文地址:https://www.cnblogs.com/csnd/p/12061903.html
Copyright © 2011-2022 走看看