c++之NVI手法
non-virtual interface(NVI)手法:令用户通过public non-virtual成员函数间接调用private virtual函数,将这个non-virtual函数称为virtual函数的wrapper.wrapper确保得以在一个virtual函数被调用之前设定好适当场景,并在调用结束之后清理场景.注意此时用指针或引用调用该non-virtual成员函数时,该non-virtual成员函数调用的virtual函数取决于指针或引用的动态类型,即存在多态
enum ShapeColor { Red, Green, Blue }; class Shape { public: void draw(ShapeColor color = Red) const { doDraw(color); } private: virtual void doDraw(ShapeColor color) const = 0; }; class Rectangle :public Shape { public: private: virtual void doDraw(ShapeColor color) const; }; void Rectangle::doDraw(ShapeColor color) const { cout << "Rectangle's doDraw" << "color= " << color << endl; } void main() { Shape*p = new Rectangle; ////输出:Rectangle's doDrawcolor= 0 p->draw(); system("pause"); }
enum ShapeColor { Red, Green, Blue }; class Shape { public: void draw(ShapeColor color = Red)const { doDraw(color); } private: virtual void doDraw(ShapeColor color) const { cout << "Shape's doDraw" << "color= " << color << endl; } }; class Rectangle :public Shape { public: private: virtual void doDraw(ShapeColor color) const; }; void Rectangle::doDraw(ShapeColor color) const { cout << "Rectangle's doDraw" << "color= " << color << endl; } /* 输出: Shape's doDrawcolor= 0 Rectangle's doDrawcolor= 0 */ void main() { Shape*p = new Shape; p->draw(); delete p; p = NULL; Shape*q = new Rectangle; q->draw(); delete q; q = NULL; system("pause"); }
enum ShapeColor { Red, Green, Blue }; class Shape { public: void draw(ShapeColor color = Red)const { doDraw(color); } private: void doDraw(ShapeColor color) const { cout << "Shape's doDraw" << "color= " << color << endl; } }; class Rectangle :public Shape { public: private: void doDraw(ShapeColor color) const; }; void Rectangle::doDraw(ShapeColor color) const { cout << "Rectangle's doDraw" << "color= " << color << endl; } /* 输出: Shape's doDrawcolor= 0 Shape's doDrawcolor= 0 */ int main() { Shape*p = new Shape; p->draw(); delete p; p = NULL; Shape*q = new Rectangle; q->draw(); delete q; q = NULL; system("pause"); return 0; }