fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <ostream>
  4. #include <array>
  5. using namespace std;
  6.  
  7. class Logger{
  8. private:
  9. ostream &m_oss;
  10. public:
  11. Logger(ostream &oss) : m_oss(oss){}
  12. void log(const string &str){
  13. m_oss << str << endl;
  14. }
  15. };
  16.  
  17. struct IDrawable{
  18. virtual void draw() = 0;
  19. };
  20.  
  21. struct ILogable{
  22. virtual Logger *logger() const = 0;
  23. virtual void logger(Logger *) = 0;
  24. };
  25.  
  26. class Shape : IDrawable{
  27. protected:
  28. virtual string info() = 0;
  29. };
  30.  
  31. class RegisteredShape : public Shape, ILogable{
  32. protected:
  33. Logger *m_logger;
  34. public:
  35. Logger *logger() const{ return m_logger; }
  36. void logger(Logger *l){ m_logger = l; }
  37. void draw(){
  38. m_logger->log("drawing " + info());
  39. }
  40. };
  41.  
  42. class RegRectangle : public RegisteredShape{
  43. protected:
  44. string info() { return "Rectangle"; }
  45. };
  46.  
  47. class RegCircle : public RegisteredShape{
  48. protected:
  49. string info() { return "Circle"; }
  50. };
  51.  
  52. int main(){
  53. Logger l(cout);
  54. array<RegisteredShape *, 2> shapes = {
  55. new RegRectangle(),
  56. new RegCircle()
  57. };
  58.  
  59. for(auto *shape : shapes){
  60. shape->logger(&l);
  61. shape->draw();
  62. }
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
drawing Rectangle
drawing Circle