The object adapter has the following characteristics:
- Sometimes, you will find that it is not easy to construct an object of the adaptee type;
- When adding a new abstract method to the Adaptee, the adapter class does not need to make any adjustments and can perform the action correctly;
- You can invoke methods of the Adaptee class subclass in the adapter class using a polypeptide.
Code implementation:
1 //Adapter.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <string>7 using namespacestd;8 9 Ten classTarget One { A Public: - Target () {} - Virtual~Target () {} the Virtual voidRequest () - { -cout <<"target::request"<<Endl; - } + }; - classAdaptee + { A Public: at voidspecificrequest () - { -cout <<"adaptee::specificrequest"<<Endl; - } - }; - in classAdapter: PublicTarget - { to Public: +Adapter (): M_adaptee (Newadaptee) {} -~Adapter () the { * if(M_adaptee! =NULL) $ {Panax Notoginseng Deletem_adaptee; -M_adaptee =NULL; the } + } A voidRequest () the { +M_adaptee->specificrequest (); - } $ $ Private: -Adaptee *m_adaptee; - }; the int_tmain (intARGC, _tchar*argv[]) - {Wuyicout<<"Adapter Mode:"<<Endl; theTarget *targetobj =NewAdapter (); -Targetobj->Request (); Wu Deletetargetobj; -Targetobj =NULL; AboutSystem"Pause"); $ return 0; -}
Adapter mode for C + + design mode