本文主要是介绍【C++】访问者模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
根据对象类型执行不同的功能,就是对象类型到功能之间的映射需求:
#include <iostream>
using namespace std;class Base {
public :virtual void say() = 0;virtual ~Base() {}
};class A : public Base {
public :void say() override {cout << "Class A" << endl;}
};class B : public Base {
public :void say() override {cout << "Class B" << endl;}
};class C : public Base {
public :void say() override {cout << "Class C" << endl;}
};
class D : public Base {
public :void say() override {cout << "Class D" << endl;}
};class E : public Base {
public :void say() override {cout << "Class E" << endl;}
};void func_A() {cout << "func A" << endl;
}void func_B() {cout << "func B" << endl;
}void func_C() {cout << "func C" << endl;
}void func_D() {cout << "func D" << endl;
}void func_E() {cout << "func E" << endl;
}int main() {srand(time(0));Base *p = nullptr;switch (rand() % 5) {case 0 : p = new A(); break;case 1 : p = new B(); break;case 2 : p = new C(); break;case 3 : p = new D(); break;case 4 : p = new E(); break;}p->say();//根据p对象类型执行不同的功能if (dynamic_cast<A *>(p)) {func_A();} else if (dynamic_cast<B *>(p)) {func_B();} else if (dynamic_cast<C *>(p)) {func_C();} else if (dynamic_cast<D *>(p)) {func_D();} else if (dynamic_cast<E *>(p)) {func_E();}return 0;
}
代码中存在的问题:
- 新增子类后可能会漏修改关键代码,导致隐藏严重的bug,维护成本高;
- 效率低
正确地执行到某个 func
方法的时间复杂度是 O ( n ) O(n) O(n)。
访问者模式就是为了完成对象类型到功能需求的映射。
设计的核心在于将要映射的功能封装成一个类,让编译器改代码。
用访问者模式完成上面的代码相同的功能:
#include <iostream>
using namespace std;class A;
class B;
class C;
class D;
class E;class Base {
public :class IVisitor { //访问Base类的派生类的访问者的接口类public :virtual void visit(A *) = 0;virtual void visit(B *) = 0;virtual void visit(C *) = 0;virtual void visit(D *) = 0;virtual void visit(E *) = 0;};virtual void say() = 0;virtual void Accept(IVisitor *) = 0;virtual ~Base() {}
};class A : public Base {
public :void say() override {cout << "Class A" << endl;}void Accept(IVisitor *vis) override {vis->visit(this);return ;}
};class B : public Base {
public :void say() override {cout << "Class B" << endl;}void Accept(IVisitor *vis) override {vis->visit(this);return ;}
};class C : public Base {
public :void say() override {cout << "Class C" << endl;}void Accept(IVisitor *vis) override {vis->visit(this);return ;}
};
class D : public Base {
public :void say() override {cout << "Class D" << endl;}void Accept(IVisitor *vis) override {vis->visit(this);return ;}
};class E : public Base {
public :void say() override {cout << "Class E" << endl;}void Accept(IVisitor *vis) override {vis->visit(this);return ;}
};class outputVisitor : public Base::IVisitor {
public :void visit(A *obj) {cout << "func A" << endl;}void visit(B *obj) {cout << "func B" << endl;}void visit(C *obj) {cout << "func C" << endl;}void visit(D *obj) {cout << "func D" << endl;}void visit(E *obj) {cout << "func E" << endl;}
};int main() {srand(time(0));Base *p = nullptr;switch (rand() % 5) {case 0 : p = new A(); break;case 1 : p = new B(); break;case 2 : p = new C(); break;case 3 : p = new D(); break;case 4 : p = new E(); break;}p->say();outputVisitor vis;p->Accept(&vis);return 0;
}
从 p
到具体的对象类型的时间复杂度是 O ( 1 ) O(1) O(1),而再到对象对应的visit
方法的时间复杂度也是 O ( 1 ) O(1) O(1),即是说正确指定到对象类型的功能的时间复杂度是 O ( 1 ) O(1) O(1)。
访问者模式利用虚函数的技巧,完成了两次跳转,第一次跳转定位到相关类中的Accept
方法,第二次跳转将相关类的this
指针传给visit
方法,准确定位到哪个visit
方法。即利用两次指针类型的转换定位到相关功能执行的具体过程。访问者模式不存在相关的效率问题。
根据对象类型映射到功能需求可以考虑访问者模式
对于不同的对象类型对x做不同的操作:
-
A : x -> 2x
-
B : x -> x - 3
-
C : x -> x + 6
-
D : x -> x / 3
-
E : x -> x - 2
代码实现:
#include <iostream>
using namespace std;class A;
class B;
class C;
class D;
class E;class Base {
public :class IVisitor { //访问Base类的派生类的访问者的接口类public :virtual void visit(A *) = 0;virtual void visit(B *) = 0;virtual void visit(C *) = 0;virtual void visit(D *) = 0;virtual void visit(E *) = 0;};virtual void say() = 0;virtual void Accept(IVisitor *) = 0;virtual ~Base() {}
};class A : public Base {
public :void say() override {cout << "Class A" << endl;}void Accept(IVisitor *vis) override {vis->visit(this);return ;}
};class B : public Base {
public :void say() override {cout << "Class B" << endl;}void Accept(IVisitor *vis) override {vis->visit(this);return ;}
};class C : public Base {
public :void say() override {cout << "Class C" << endl;}void Accept(IVisitor *vis) override {vis->visit(this);return ;}
};
class D : public Base {
public :void say() override {cout << "Class D" << endl;}void Accept(IVisitor *vis) override {vis->visit(this);return ;}
};class E : public Base {
public :void say() override {cout << "Class E" << endl;}void Accept(IVisitor *vis) override {vis->visit(this);return ;}
};class calcVisitor : public Base::IVisitor {
public :calcVisitor(int &x) : x(x) {}void visit(A *obj) {x *= 2;}void visit(B *obj) {x -= 3;}void visit(C *obj) {x += 6;}void visit(D *obj) {x /= 3;}void visit(E *obj) {x -= 2;}int &x;
};int main() {srand(time(0));Base *p[5] = { nullptr };for (int i = 0; i < 5; i++) {switch (rand() % 5) {case 0 : p[i] = new A(); break;case 1 : p[i] = new B(); break;case 2 : p[i] = new C(); break;case 3 : p[i] = new D(); break;case 4 : p[i] = new E(); break;}}int x = 1;//依次处理p中的对象,不同的对象类型对x做不同的操作calcVisitor vis(x);for (int i = 0, pre = x; i < 5; i++) {p[i]->Accept(&vis);p[i]->say();cout << pre << " -> " << x << endl;pre = x;}cout << x << endl;return 0;
}
运行结果:
Class B
1 -> -2
Class C
-2 -> 4
Class D
4 -> 1
Class E
1 -> -1
Class D
-1 -> 0
0
每个访问者都是一个对象,访问者内部可以拥有成员属性。
总结:访问者模式实际上是完成的类型到功能之间的映射。
这篇关于【C++】访问者模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!