Flyweight.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <stdio.h>
  2. /*
  3. * C++ Design Patterns: FLYWEIGHT
  4. * https://www.plantuml.com/plantuml/uml/bL3DZjCm4BxxAKRYq1O85TTLLAr0Mq12N7OHueGSPzeQEUDePWn4m0F1Q-oBuLgXgOWgiLOEyNm_y_cx5hMigGjclO3jsuyVRjwyUdq7YxN9nmKh0jimV4M_F-ZMEeqym7S3-LIGA7ABZEsdplPbzWJsg2V-SZKEWvTfAiiLc9_6U5BadNKuBHYT7q7iaQtwI4jFWD-KFObtjseLLrax1diPF9AURf4Se-A_RXLuEZSOC_tVYlSa1ylONZQt8Jp4zk1R1Enw7Q33ha4MOsO5FwwcJYOKwXzLDNlcZ70nydnFdUbZYJFsENZHyfnaA2g25NZtl7HHDvlQnH3vYkDWmukGS7CIrki2RUfri_dBlXjwl3eEu5OvEKriGy5kY77lHHaXe41iFgCAj9O44Y78o4PeynU9xd-XF2rAPtlRX098wll8YfmrWBiTElKx-695Cxmedj0cwEv_TrYqckCyCqWj71zchlDVwi8V
  5. *
  6. */
  7. #include <iostream>
  8. #include <string>
  9. #include <unordered_map>
  10. class Flyweight {
  11. public:
  12. virtual ~Flyweight() {}
  13. virtual void operation(const std::string& extrinsicState) = 0;
  14. };
  15. class ConcreteFlyweight : public Flyweight {
  16. public:
  17. ConcreteFlyweight(const std::string& intrinsicState) :
  18. intrinsicState(intrinsicState) {
  19. std::cout << "ConcreteFlyweight intrinsicState: " << intrinsicState << std::endl;
  20. }
  21. ~ConcreteFlyweight() {}
  22. void operation(const std::string& extrinsicState) {
  23. std::cout << "ConcreteFlyweight operation extrinsicState: " << extrinsicState << std::endl;
  24. }
  25. private:
  26. std::string intrinsicState;
  27. };
  28. class UnsharedConcreteFlyweight : public Flyweight {
  29. public:
  30. UnsharedConcreteFlyweight(const std::string& allState) : private_allState(allState) {
  31. std::cout << "UnsharedConcreteFlyweight allState: " << private_allState << std::endl;
  32. }
  33. void operation(const std::string& extrinsicState) {
  34. std::cout << "UnsharedConcreteFlyweight operation extrinsicState: " << extrinsicState << std::endl;
  35. }
  36. private:
  37. std::string private_allState;
  38. };
  39. class FlyweightFactory {
  40. public:
  41. ~FlyweightFactory() {
  42. for (auto it = mapflys.begin(); it != mapflys.end(); ++it) {
  43. delete it->second;
  44. }
  45. mapflys.clear();
  46. }
  47. Flyweight *getFlyweight(const std::string& key) {
  48. if (mapflys.find(key) != mapflys.end()) {
  49. std::cout << "Key : " << key << ", already exist" << std::endl;
  50. return mapflys[key];
  51. }
  52. Flyweight *fly = new ConcreteFlyweight(key);
  53. mapflys.insert(std::make_pair(key, fly));
  54. std::cout << "New entry for key: " << key<< std::endl;
  55. return fly;
  56. }
  57. void listFlyweights() const
  58. {
  59. size_t count = this->mapflys.size();
  60. std::cout << "\nFlyweightFactory: I have " << count << " flyweights:\n";
  61. for (auto& it: mapflys) {
  62. // Do stuff
  63. std::cout << it.first << ";";
  64. }
  65. std::cout << std::endl;
  66. }
  67. private:
  68. std::unordered_map<std::string, Flyweight*> mapflys;
  69. };
  70. int main() {
  71. FlyweightFactory *factory = new FlyweightFactory();
  72. factory->getFlyweight("Hello Greeting")->operation("Greeting the world");
  73. factory->getFlyweight("Hello")->operation("Greeting group");
  74. factory->getFlyweight("Hello")->operation("Greeting classroom");
  75. factory->listFlyweights();
  76. Flyweight* unsharedfly = new UnsharedConcreteFlyweight("Unshared");
  77. unsharedfly->operation("Greeting");
  78. }