Guest User

Untitled

a guest
Dec 7th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. // Logger.h - Абстракция
  2. #include <string>
  3.  
  4. // Опережающее объявление, позволяющее не включать файл LoggerImpl.h
  5. class LoggerImpl;
  6.  
  7. class Logger
  8. {
  9. public:
  10. Logger( LoggerImpl* p );
  11. virtual ~Logger( );
  12. virtual void log( string & str ) = 0;
  13. protected:
  14. LoggerImpl * pimpl;
  15. };
  16.  
  17. class ConsoleLogger : public Logger
  18. {
  19. public:
  20. ConsoleLogger();
  21. void log( string & str );
  22. };
  23.  
  24. class FileLogger : public Logger
  25. {
  26. public:
  27. FileLogger( string & file_name );
  28. void log( string & str );
  29. private:
  30. string file;
  31. };
  32.  
  33. class SocketLogger : public Logger
  34. {
  35. public:
  36. SocketLogger( string & remote_host, int remote_port );
  37. void log( string & str );
  38. private:
  39. string host;
  40. int port;
  41. };
  42.  
  43.  
  44. // Logger.cpp - Абстракция
  45. #include "Logger.h"
  46. #include "LoggerImpl.h"
  47.  
  48. Logger::Logger( LoggerImpl* p ) : pimpl(p)
  49. { }
  50.  
  51. Logger::~Logger( )
  52. {
  53. delete pimpl;
  54. }
  55.  
  56. ConsoleLogger::ConsoleLogger() : Logger(
  57. #ifdef MT
  58. new MT_LoggerImpl()
  59. #else
  60. new ST_LoggerImpl()
  61. #endif
  62. )
  63. { }
  64.  
  65. void ConsoleLogger::log( string & str )
  66. {
  67. pimpl->console_log( str);
  68. }
  69.  
  70. FileLogger::FileLogger( string & file_name ) : Logger(
  71. #ifdef MT
  72. new MT_LoggerImpl()
  73. #else
  74. new ST_LoggerImpl()
  75. #endif
  76. ), file(file_name)
  77. { }
  78.  
  79. void FileLogger::log( string & str )
  80. {
  81. pimpl->file_log( file, str);
  82. }
  83.  
  84. SocketLogger::SocketLogger( string & remote_host, int remote_port ) : Logger(
  85. #ifdef MT
  86. new MT_LoggerImpl()
  87. #else
  88. new ST_LoggerImpl()
  89. #endif
  90. ), host(remote_host), port(remote_port)
  91. { }
  92.  
  93. void SocketLogger::log( string & str )
  94. {
  95. pimpl->socket_log( host, port, str);
  96. }
  97.  
  98.  
  99. // LoggerImpl.h - Реализация
  100. #include <string>
  101.  
  102. class LoggerImpl
  103. {
  104. public:
  105. virtual ~LoggerImpl( ) {}
  106. virtual void console_log( string & str ) = 0;
  107. virtual void file_log( string & file, string & str ) = 0;
  108. virtual void socket_log( string & host, int port, string & str ) = 0;
  109. };
  110.  
  111. class ST_LoggerImpl : public LoggerImpl
  112. {
  113. public:
  114. void console_log( string & str );
  115. void file_log ( string & file, string & str );
  116. void socket_log ( string & host, int port, string & str );
  117. };
  118.  
  119. class MT_LoggerImpl : public LoggerImpl
  120. {
  121. public:
  122. void console_log( string & str );
  123. void file_log ( string & file, string & str );
  124. void socket_log ( string & host, int port, string & str );
  125. };
  126.  
  127.  
  128. // LoggerImpl.cpp - Реализация
  129. #include <iostream>
  130. #include "LoggerImpl.h"
  131.  
  132.  
  133. void ST_LoggerImpl::console_log( string & str )
  134. {
  135. cout << "Single-threaded console logger" << endl;
  136. }
  137.  
  138. void ST_LoggerImpl::file_log( string & file, string & str )
  139. {
  140. cout << "Single-threaded file logger" << endl;
  141. }
  142.  
  143. void ST_LoggerImpl::socket_log( string & host, int port, string & str )
  144. {
  145. cout << "Single-threaded socket logger" << endl;
  146. };
  147.  
  148. void MT_LoggerImpl::console_log( string & str )
  149. {
  150. cout << "Multithreaded console logger" << endl;
  151. }
  152.  
  153. void MT_LoggerImpl::file_log( string & file, string & str )
  154. {
  155. cout << "Multithreaded file logger" << endl;
  156. }
  157.  
  158. void MT_LoggerImpl::socket_log( string & host, int port, string & str )
  159. {
  160. cout << "Multithreaded socket logger" << endl;
  161. }
  162.  
  163.  
  164. // Main.cpp
  165. #include <string>
  166. #include "Logger.h"
  167.  
  168. int main()
  169. {
  170. Logger * p = new FileLogger( string("log.txt"));
  171. p->log( string("message"));
  172. delete p;
  173. return 0;
  174. }
Add Comment
Please, Sign In to add comment