VasilM

oop_singleton

May 13th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. class Singleton {
  7.     static Singleton s;
  8.     int i;
  9.     Singleton(int x):i(x) { cout<<"Singleton ready!!!\n"; }
  10.     Singleton(const Singleton&);
  11.     Singleton& operator=(Singleton&);
  12. public:
  13.     static Singleton& instance() {
  14.         cout <<"Singleton used:\n";
  15.         return s;
  16.     }
  17.     int getValue() { return i; }
  18.     void setValue(int x) { i=x; }
  19. };
  20.  
  21. Singleton Singleton::s(7777);
  22.  
  23. class SingletonM {
  24.     int i;
  25.     SingletonM(int x):i(x) { cout<<"Mayers Singleton ready!!!\n"; }
  26.     SingletonM(const SingletonM&);
  27.     SingletonM& operator=(SingletonM&);
  28. public:
  29.     static SingletonM& instace() {
  30.         static SingletonM s(5555);
  31.         return s;
  32.     }
  33.     int getValue() { return i; }
  34.     void setValue(int x) { i=x; }
  35.     ostream& ins(ostream& os) const {
  36.         return os<<i;
  37.     }
  38. };
  39.  
  40. ostream& operator<<(ostream& os, SingletonM& obj) {
  41.     return obj.ins( os );
  42. }
  43.  
  44. template <class T> class SingT {
  45. private:
  46.     SingT(const SingT&);
  47.     SingT& operator=(SingT&);
  48. public:
  49.     static T& instance() {
  50.         static T t;
  51.         return t;
  52.     }
  53. protected:
  54.     SingT() {}
  55.     virtual ~SingT() {}
  56. };
  57.  
  58. class SClass: public SingT<SClass> {
  59. protected:
  60.     friend class SingT<SClass>;
  61.     SClass() {cout <<"Singletonized instance ready!!!\n";}
  62. public:
  63.     double fun1(double x) { return x*x; }
  64.     double fun2(double x) { return sqrt(x); }
  65. };
  66.  
  67. class Dependant {
  68. public:
  69.     Dependant() {cout <<"Dependant instance ready!!!\n";}
  70.     double x2(double x) {
  71.         return SClass::instance().fun1(x);
  72.     }
  73.     double sqrt(double x) {
  74.         return SClass::instance().fun2(x);
  75.     }
  76. };
  77.  
  78. int main() {
  79.     {
  80.         Singleton& s = Singleton::instance();
  81.         cout << s.getValue() << endl;
  82.         Singleton& s2 = Singleton::instance();
  83.         s2.setValue(9999);
  84.         cout << s2.getValue() << endl;
  85.     }
  86.     {
  87.         SingletonM& s = SingletonM::instace();
  88.         cout << s << endl;
  89.         SingletonM& s2 = SingletonM::instace();
  90.         s2.setValue(9999);
  91.         cout << s2 << endl;
  92.     }
  93.     {
  94.         Dependant d;
  95.         double r = d.x2(5);
  96.         cout <<"5 * 5 = "<< r << endl;
  97.         double q = d.sqrt(r);
  98.         cout <<"sqrt(25) = "<< q << endl;
  99.     }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment