Visitor ---------- mode

zhaozj2021-02-16  66

example:

Class modem {public: Virtual void Dial (CString PNO) = 0; Virtual Void Handup () = 0; Virtual Void Send (CHAR C) = 0; Virtual Void Recv () = 0; Virtual Accept (ModeMvisitor V) = 0; }; Class Hayesmodem: public modem {

PUBLIC: VOID DIAL () {} void handup () {} void res () {} virtual accept (modemvisitor v) {v.visit (this);}}; class zoommodem: Public ModeM {public: Void Dial (CSTRING PNO) {} void send () {} void recv () {} Virtual accept (modemvisitor v) {v.visit (this);}}; class ModemVisitor {public: void visit (HayModem modem) = 0; void visit (ZoomModem modem) = 0;}; class ModemCfg: public ModemVisitor {public: ( "! I am HayerModem") void visit (HayerModem modem) {printf; } Void Visit (Zoommodem Modem) {Printf ("I am Zoommodem!");}};

Void main () {modemcfg * pm = new modemcfg (); hayermodem * ph = new hayermodem (); zoommodem * pz = new zoommodem (); vector

Vectmodem;

VECTMODEM.PUSH_BACK (PH);

VECTMODEM.PUSH_BACK (PZ);

For (int i = 0; i

{

((MODEM *) VECTMODEM [I]). ACCEPT (PM);

}

VectModem.clear ();

转载请注明原文地址:https://www.9cbs.com/read-16833.html

New Post(0)