Advertisement
homer512

Friends with alternatives

Nov 10th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1.  
  2. namespace option_1 {
  3.   /*
  4.    * Friend class controls instantiation. Is only one with write access to members
  5.    */
  6.  
  7.   class Controller;
  8.  
  9.   class Model
  10.   {
  11.     int _a, _b;
  12.     Model() = default;
  13.   public:
  14.     int a() const
  15.     { return _a; }
  16.     int b() const
  17.     { return _b; }
  18.     friend class Controller;
  19.   };
  20.  
  21.   class Controller
  22.   {
  23.     Model _m;
  24.   public:
  25.     Controller()
  26.       : _m()
  27.     {
  28.       _m._a = 1;
  29.       _m._b = 2;
  30.     }
  31.     const Model* model() const
  32.     { return &_m; }
  33.   };
  34.  
  35.   int test()
  36.   {
  37.     Controller c;
  38.     const Model* m = c.model();
  39.     return m->a();
  40.   }
  41.  
  42. }
  43.  
  44. namespace option_2 {
  45.  
  46.   /*
  47.    * Instantiation is not controlled but pointless so who cares?
  48.    * Controller returns const pointer to Model so Controller is still only
  49.    * one with write access to the only genuine Model object.
  50.    * To avoid accidental misuse, copy construction of Model is denied.
  51.    */
  52.  
  53.   class Controller;
  54.  
  55.   class Model
  56.   {
  57.     int _a, _b;
  58.   public:
  59.     Model() = default;
  60.     Model(const Model&) = delete;
  61.     Model& operator=(const Model&) = delete;
  62.    
  63.     void a(int a)
  64.     { _a = a; }
  65.     void b(int b)
  66.     { _b = b; }
  67.  
  68.     int a() const
  69.     { return _a; }
  70.     int b() const
  71.     { return _b; }
  72.   };
  73.  
  74.   class Controller
  75.   {
  76.     Model _m;
  77.   public:
  78.     Controller()
  79.       : _m()
  80.     {
  81.       _m.a(1);
  82.       _m.b(2);
  83.     }
  84.     const Model* model() const
  85.     { return &_m; }
  86.   };
  87.  
  88.   int test()
  89.   {
  90.     Controller c;
  91.     const Model* m = c.model();
  92.     return m->a();
  93.   }
  94. }
  95.  
  96. namespace option_3 {
  97.  
  98.   /*
  99.    * A weird little trick: Abuse private inheritance to hide the model in the
  100.    * inheritance hierarchy of the controller.
  101.    * I wouldn't consider this good design but it's a neat trick for corner
  102.    * cases.
  103.    */
  104.  
  105.   class Model
  106.   {
  107.   protected:
  108.     int _a, _b;
  109.     Model() = default;
  110.     ~Model() = default;
  111.   public:
  112.     Model(const Model&) = delete;
  113.     Model& operator=(const Model&) = delete;
  114.    
  115.     int a() const
  116.     { return _a; }
  117.     int b() const
  118.     { return _b; }
  119.   };
  120.  
  121.   class Controller: private Model
  122.   {
  123.   public:
  124.     Controller()
  125.     {
  126.       _a = 1;
  127.       _b = 2;
  128.     }
  129.     const Model* model() const
  130.     { return this; }
  131.   };
  132.  
  133.   int test()
  134.   {
  135.     Controller c;
  136.     const Model* m = c.model();
  137.     return m->a();
  138.   }
  139. }
  140.  
  141.  
  142. namespace option_4 {
  143.  
  144.   /*
  145.    * Cleaner version of option_3. Modifiers for Model are restricted to a
  146.    * subclass that only Controller can access.
  147.    */
  148.  
  149.   class Model
  150.   {
  151.   protected:
  152.     int _a, _b;
  153.     Model() = default;
  154.     ~Model() = default;
  155.   public:
  156.     Model(const Model&) = delete;
  157.     Model& operator=(const Model&) = delete;
  158.    
  159.     int a() const
  160.     { return _a; }
  161.     int b() const
  162.     { return _b; }
  163.   };
  164.  
  165.   class Controller
  166.   {
  167.     class MutableModel: public Model
  168.     {
  169.     public:
  170.       void a(int a)
  171.       { _a = a; }
  172.       void b(int b)
  173.       { _b = b; }
  174.     };
  175.  
  176.     MutableModel _m;
  177.   public:
  178.     Controller()
  179.     {
  180.       _m.a(1);
  181.       _m.b(2);
  182.     }
  183.     const Model* model() const
  184.     { return &_m; }
  185.   };
  186.  
  187.   int test()
  188.   {
  189.     Controller c;
  190.     const Model* m = c.model();
  191.     return m->a();
  192.   }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement