适配器模式.cpp
# include <iostream>
# include <vector>
# include <string>
# include <fstream>
# include <memory>
using namespace std;
namespace ns1
{ class LogToFile { public : void initfile ( ) { } void writetofile ( const char * pcontent) { } void readfromfile ( ) { } void closefile ( ) { } } ;
} namespace ns2
{ class LogToDatabase { public : void initdb ( ) { } void writetodb ( const char * pcontent) { } void readfromdb ( ) { } void closedb ( ) { } } ;
} namespace ns3
{ class LogToFile { public : void initfile ( ) { } void writetofile ( const char * pcontent) { } void readfromfile ( ) { } void closefile ( ) { } } ; class LogToDatabase { public : virtual ~ LogToDatabase ( ) { } virtual void initdb ( ) = 0 ; virtual void writetodb ( const char * pcontent) = 0 ; virtual void readfromdb ( ) = 0 ; virtual void closedb ( ) = 0 ; } ; class LogAdapter : public LogToDatabase { shared_ptr< LogToFile> m_pfile; public : LogAdapter ( const shared_ptr< LogToFile> & pfile = make_shared < LogToFile> ( ) ) : m_pfile ( pfile) { } void initdb ( ) override { cout << "LogAdapter::initdb() Adapter LogToFile::initfile()" << endl; m_pfile-> initfile ( ) ; } void writetodb ( const char * pcontent) override { cout << "LogAdapter::writetodb() Adapter LogToFile::writetofile()" << endl; m_pfile-> writetofile ( pcontent) ; } void readfromdb ( ) override { cout << "LogAdapter::readfromdb() Adapter LogToFile::readfromdb()" << endl; m_pfile-> readfromfile ( ) ; } void closedb ( ) override { cout << "LogAdapter::closedb() Adapter LogToFile::closedb()" << endl; m_pfile-> closefile ( ) ; } } ;
} namespace ns4
{ class LogToFile { public : void initfile ( ) { } void writetofile ( const char * pcontent) { } void readfromfile ( ) { } void closefile ( ) { } } ; class LogToDatabase { public : virtual ~ LogToDatabase ( ) { } virtual void initdb ( ) = 0 ; virtual void writetodb ( const char * pcontent) = 0 ; virtual void readfromdb ( ) = 0 ; virtual void closedb ( ) = 0 ; } ; class LogAdapter : public LogToDatabase , private LogToFile { public : void initdb ( ) override { cout << "LogAdapter::initdb() Adapter LogToFile::initfile()" << endl; initfile ( ) ; } void writetodb ( const char * pcontent) override { cout << "LogAdapter::writetodb() Adapter LogToFile::writetofile()" << endl; writetofile ( pcontent) ; } void readfromdb ( ) override { cout << "LogAdapter::readfromdb() Adapter LogToFile::readfromdb()" << endl; readfromfile ( ) ; } void closedb ( ) override { cout << "LogAdapter::closedb() Adapter LogToFile::closedb()" << endl; closefile ( ) ; } } ;
} int main ( )
{
# if 0 using namespace ns1; shared_ptr< LogToFile> plog ( new LogToFile ( ) ) ; plog-> initfile ( ) ; plog-> writetofile ( "Write a log to the log file" ) ; plog-> readfromfile ( ) ; plog-> closefile ( ) ;
# endif # if 0 using namespace ns2; shared_ptr< LogToDatabase> plogdb ( new LogToDatabase ( ) ) ; plogdb-> initdb ( ) ; plogdb-> writetodb ( "Write a log to the database" ) ; plogdb-> closedb ( ) ; plogdb-> readfromdb ( ) ;
# endif # if 0 using namespace ns3; shared_ptr< LogToDatabase> plogdb2 ( new LogAdapter ( ) ) ; plogdb2-> initdb ( ) ; plogdb2-> writetodb ( "Writing a log to the database is actually writing a log to the log file" ) ; plogdb2-> readfromdb ( ) ; plogdb2-> closedb ( ) ;
# endif # if 1 using namespace ns4; shared_ptr< LogToDatabase> plogdb3 ( new LogAdapter ( ) ) ; plogdb3-> initdb ( ) ; plogdb3-> writetodb ( "Writing a log to the database is actually writing a log to the log file" ) ; plogdb3-> readfromdb ( ) ; plogdb3-> closedb ( ) ;
# endif cout << "Over!\n" ; return 0 ;
}