fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3. using namespace std;
  4.  
  5. struct Android{};
  6. struct IOS{};
  7.  
  8. struct Printer{virtual ~Printer(){}};
  9. struct Writer{virtual ~Writer(){}};
  10. struct Connector{virtual ~Connector(){}};
  11.  
  12. struct AndroidPrinter : Printer{};
  13. struct AndroidWriter : Writer{};
  14. struct AndroidConnector : Connector{};
  15.  
  16. struct IOSPrinter : Printer{};
  17. struct IOSWriter : Writer{};
  18. struct IOSConnector : Connector{};
  19.  
  20. template <typename T>
  21. struct ConcreteComponentFactory;
  22.  
  23. template <>
  24. struct ConcreteComponentFactory<Android>
  25. {
  26. static Printer *CreatePrinter()
  27. { return new AndroidPrinter(); }
  28. static Writer *CreateWriter()
  29. { return new AndroidWriter(); }
  30. static Connector *CreateConnector()
  31. { return new AndroidConnector(); }
  32. };
  33.  
  34. template <>
  35. struct ConcreteComponentFactory<IOS>
  36. {
  37. static Printer *CreatePrinter()
  38. { return new IOSPrinter(); }
  39. static Writer *CreateWriter()
  40. { return new IOSWriter(); }
  41. static Connector *CreateConnector()
  42. { return new IOSConnector(); }
  43. };
  44.  
  45. #define PLATFORM 0
  46. #if PLATFORM==0
  47. using Platform = Android;
  48. #else
  49. using Platform = IOS;
  50. #endif
  51.  
  52. using ComponentFactory = ConcreteComponentFactory<Platform>;
  53.  
  54. int main() {
  55. std::cout << typeid(*ComponentFactory::CreateWriter()).name();
  56. return 0;
  57. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
13AndroidWriter