Guest User

Untitled

a guest
Jun 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. class Software
  2. {
  3. friend class SoftwareProducer;
  4.  
  5. SoftwareProducer* m_producer;
  6. int m_key;
  7. // Only producers can produce software
  8. Software(SoftwareProducer* producer) : m_producer(producer) { }
  9.  
  10. public:
  11. void buy()
  12. {
  13. m_key = m_producer->next_key();
  14. }
  15. };
  16.  
  17. class SoftwareProducer
  18. {
  19. friend class Software;
  20.  
  21. public:
  22. Software* produce()
  23. {
  24. return new Software(this);
  25. }
  26.  
  27. private:
  28. // Only software from this producer can get a valid key for registration
  29. int next_key()
  30. {
  31. return ...;
  32. }
  33. };
  34.  
  35. class Software {
  36. private:
  37. Software() : m_key(0) { /* whatever */ }
  38. public:
  39. void buy() { m_key = new_key(); }
  40. public:
  41. static Software *create() { return new Software; }
  42. private:
  43. static int new_key() { static int example_id = 1; return example_id++; }
  44. private:
  45. int m_key;
  46. };
  47.  
  48. Software *soft = Software::create();
  49. soft->buy();
Add Comment
Please, Sign In to add comment