Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <time.h>
  4.  
  5. class BaseClass;
  6.  
  7. int randomInt(int max = 0) {
  8.     srand(time(NULL) + rand());
  9.  
  10.     if(max == 0) {
  11.         return  rand();
  12.     } else {
  13.         return rand() % max;
  14.     }
  15. }
  16.  
  17. class Factory {
  18. public:
  19.     virtual BaseClass* create() = 0;
  20. };
  21.  
  22.  
  23. class BaseClass {
  24. protected:
  25.     class BaseClassFactory;
  26. public:
  27.     virtual void method() = 0;
  28.  
  29.     static Factory *getFactory() {
  30.         return new BaseClassFactory;
  31.     }
  32.  
  33. protected:
  34.     class BaseClassFactory : public Factory {
  35.     public:
  36.         BaseClass* create() {
  37.             return NULL;
  38.         }
  39.     };
  40.  
  41. };
  42.  
  43. class DerrivedClass : public BaseClass {
  44. protected:
  45.     class DerrivedClassFactory;
  46.  
  47. public:
  48.     void method() {
  49.         std::cout << "Derrived Class!" << std::endl;
  50.     }
  51.  
  52.     static Factory *getFactory() {
  53.         return new DerrivedClassFactory();
  54.     }
  55. protected:
  56.     class DerrivedClassFactory : public Factory {
  57.     public:
  58.         BaseClass* create() {
  59.             return new DerrivedClass();
  60.         }
  61.     };
  62. };
  63.  
  64. class SecondDerrivedClass : public BaseClass {
  65. protected:
  66.     class SecondDerrivedClassFactory;
  67. public:
  68.     void method() {
  69.         std::cout << "SecondDerrived Class!" << std::endl;
  70.     }
  71.  
  72.     static SecondDerrivedClassFactory* getFactory() {
  73.         return new SecondDerrivedClassFactory();
  74.     }
  75.  
  76. protected:
  77.     class SecondDerrivedClassFactory : public Factory {
  78.     public:
  79.         BaseClass *create() {
  80.             return new SecondDerrivedClass();
  81.         }
  82.     };
  83. };
  84.  
  85.  
  86. class ClassCreator {
  87. public:
  88.    
  89.     void subscribeFactory(Factory *f) {
  90.         mClassFactoryList.push_back(f);
  91.     }
  92.  
  93.     void clearSubscriptions() {
  94.         std::vector<Factory*>::iterator itr;
  95.         for(itr = mClassFactoryList.begin(); itr != mClassFactoryList.end();) {
  96.             delete (*itr);
  97.             itr = mClassFactoryList.erase(itr);
  98.         }
  99.     }
  100.  
  101.     BaseClass *createRandomClass() {
  102.         return mClassFactoryList.at(randomInt(mClassFactoryList.size()))->create();
  103.     }
  104.  
  105. private:
  106.     std::vector<Factory*> mClassFactoryList;
  107. };
  108.  
  109. int main() {
  110.     ClassCreator *c = new ClassCreator();
  111.  
  112.     c->subscribeFactory(DerrivedClass::getFactory());
  113.     c->subscribeFactory(SecondDerrivedClass::getFactory());
  114.  
  115.     BaseClass *b = c->createRandomClass();
  116.     b->method();
  117.     delete b;
  118.  
  119.     b = c->createRandomClass();
  120.     b->method();
  121.     delete b;
  122.  
  123.     b = c->createRandomClass();
  124.     b->method();
  125.     delete b;
  126.  
  127.     b = c->createRandomClass();
  128.     b->method();
  129.     delete b;
  130.  
  131.     b = c->createRandomClass();
  132.     b->method();
  133.     delete b;
  134.  
  135.     b = c->createRandomClass();
  136.     b->method();
  137.     delete b;
  138.  
  139.     b = c->createRandomClass();
  140.     b->method();
  141.     delete b;
  142.  
  143.     b = c->createRandomClass();
  144.     b->method();
  145.     delete b;
  146.  
  147.     b = c->createRandomClass();
  148.     b->method();
  149.     delete b;
  150.  
  151.     c->clearSubscriptions();
  152.     delete c;
  153.  
  154.     return 0;
  155. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement