Objective: "Realizing the class hierarchy," "functional class hierarchy" and classification
Functional class level: through inheritance to add functionality class (add a normal function)
To achieve levels of class: implement virtual functions through inheritance class
Appreciated: and bridging method same adapter mode
example:
class DisplayImpl { public: virtual void open() = 0; virtual void print() = 0; virtual void close() = 0; }; class StringDisplayImpl: public DisplayImpl //实现性扩展 { public: void open() { cout << "open()" << endl; } void print() { cout << "print()" << endl; } void close() { cout << "close()" << endl; } };
Display class { DisplayImpl * pImpl; bridged core code // public: Display (DisplayImpl pImpl *) { this-> = pImpl pImpl; } void Print () { pImpl-> Open (); pImpl-> Print (); pImpl -> Close (); } }; class multidisplay: // public Display functional extensions { public: multidisplay (DisplayImpl pImpl *): Display (pImpl) {} void Multiprint () { for (int I = 0; I <. 5 ; I ++) { Print (); } } };
int main() { Display* d = new Display(new StringDisplayImpl()); d->print(); cout << endl; MultiDisplay* md = new MultiDisplay(new StringDisplayImpl()); md->multiPrint(); return 0; }