Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Result{};
  4.  
  5. class A {};
  6.  
  7. class B {
  8. public:
  9. void specific() const
  10. {
  11. std::cout << "B::specific()" << std::endl;
  12. };
  13. };
  14.  
  15. class C : public B {};
  16.  
  17. class D {};
  18.  
  19. template<class Type>
  20. class RelationshipWithA
  21. {
  22. const A& a_;
  23.  
  24. const Type& t_;
  25.  
  26. public:
  27. typedef Type target_type;
  28.  
  29. RelationshipWithA (const A& a, const Type& t)
  30. :
  31. a_(a),
  32. t_(t)
  33.  
  34. {
  35. std::cout << "RelationshipWithA::ctor" << std::endl;
  36. };
  37.  
  38. const A& a() const
  39. {
  40. return a_;
  41. }
  42.  
  43. const Type& type() const
  44. {
  45. return t_;
  46. }
  47. };
  48.  
  49. class DefaultAlgorithm
  50. {
  51. public:
  52. template <class Relationship>
  53. void calculate (Result& res, const Relationship& r)
  54. {
  55. std::cout << "DefaultAlgorithm::calculate" << std::endl;
  56. const A& a = r.a();
  57. const typename Relationship::target_type& t = r.type();
  58. // Default iterator based calculation on a, target_type and r
  59. };
  60. };
  61.  
  62. class AlternativeAlgorithm
  63. :
  64. public DefaultAlgorithm
  65. {
  66. public:
  67. template <class Relationship>
  68. void calculate (Result& res, const Relationship& r)
  69. {
  70. std::cout << "AlternativeAlgorithm::calculate" << std::endl;
  71. // Optimized iterator based calculation on a, target_type and r
  72. }
  73. };
  74.  
  75. class ImplementationBasedAlgorithm
  76. :
  77. public DefaultAlgorithm
  78. {
  79. public:
  80. // No specialization: Relationships store
  81. // a const reference to any class that inherits from B
  82. template <class Relationship>
  83. void calculate (Result& res, const Relationship& r)
  84. {
  85. // Use B implementation and the Relationship With A to compute the result
  86. std::cout << "ImplementationBasedAlgorithm::calculate" << std::endl;
  87. const A& a = r.a();
  88. const B& b = r.type();
  89. b.specific();
  90. // Implementation based on B implementation
  91. }
  92. };
  93.  
  94. int main(int argc, const char *argv[])
  95. {
  96. Result res;
  97.  
  98. A a;
  99. C c;
  100.  
  101. RelationshipWithA<C> relationshipAC (a, c);
  102.  
  103. DefaultAlgorithm defaultAlg;
  104. AlternativeAlgorithm alternativeAlg;
  105. ImplementationBasedAlgorithm implementationAlg;
  106.  
  107. defaultAlg.calculate(res, relationshipAC);
  108. alternativeAlg.calculate(res, relationshipAC);
  109. implementationAlg.calculate(res,relationshipAC);
  110.  
  111. D d;
  112. RelationshipWithA<D> relationshipAD (a, d);
  113.  
  114. defaultAlg.calculate(res, relationshipAD);
  115. alternativeAlg.calculate(res, relationshipAD);
  116. // This fails, as expected
  117. //implementationAlg.calculate(res,relationshipAD);
  118.  
  119. return 0;
  120. }
  121.  
  122. DefaultAlgorithm algo = Factory.CreateImplementationBasedAlgorithm();
  123. RelationshipWithA<D> relationshipAD (a, d);
  124. algo.calculate(res, relationshipAD); //won't fail because the base class methods are used (because it isn't virtual)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement