Advertisement
Caminhoneiro

Specification pattern

Jul 11th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <vector>
  4. #include <iostream>
  5. //SOLID
  6.  
  7. //O
  8.  
  9. //OPEN - CLOSED PRINCIPLE
  10. //The class must be open for extension and closed to modification
  11.  
  12. enum class Color {Red, Green, Blue};
  13. enum class Size {Small, Medium, Large};
  14.  
  15. struct Product
  16. {
  17.   std::string name;
  18.   Color color;
  19.   Size size;
  20. };
  21.  
  22. //Check on the list of products if the color  is the one with I searched for and return the product for me
  23. struct ProductFilter
  24. {
  25.   typedef std::vector<Product*> Items;
  26.   static Items by_color(Items items, Color color)
  27.   {
  28.     Items result;
  29.     for (auto& i : items)
  30.       if (i->color == color)
  31.         result.push_back(i);
  32.     return result;
  33.   }
  34.  
  35.   //Check on the list of products if the size  is the one with I searched for and return the product for me
  36.   static Items by_size(Items items, Size Size)
  37.   {
  38.     Items result;
  39.     for (auto& i : items)
  40.       if (i->size == Size)
  41.         result.push_back(i);
  42.     return result;
  43.   }
  44.  
  45.   //Check on the list of products if the size and colors  is the one with I searched for and return the product for me
  46.   static Items by_color_and_size(Items items, Color color, Size size)
  47.   {
  48.     Items result;
  49.     for (auto& i : items)
  50.       if (i->color == color && i->size == size)
  51.         result.push_back(i);
  52.     return result;
  53.   }
  54. };
  55.  
  56. //Specification pattern
  57. template <typename T> struct ISpecification
  58. {
  59.   virtual bool is_satisfied(T* item) = 0;
  60. };
  61.  
  62. template <typename T> struct IFilter
  63. {
  64.   virtual std::vector<T*> filter(std::vector<T*> items, ISpecification<T>& spec) = 0;
  65. };
  66.  
  67. struct BetterFilter : IFilter<Product>
  68. {
  69.   typedef std::vector<Product*> Items;
  70.   std::vector<Product*> filter(std::vector<Product*> items, ISpecification<Product>& spec) override
  71.   {
  72.     Items result;
  73.     for (auto& p : items)
  74.       if (spec.is_satisfied(p))
  75.         result.push_back(p);
  76.     return result;
  77.   }
  78. };
  79.  
  80. struct ColorSpecification: ISpecification<Product>
  81. {
  82.   Color color;
  83.  
  84.   explicit ColorSpecification(const Color color)
  85.     : color{color}
  86.   {
  87.   }
  88.  
  89.  
  90.   bool is_satisfied(Product* item) override {
  91.     return item->color == color;
  92.   }
  93. };
  94.  
  95. struct SizeSpecification: ISpecification<Product>
  96. {
  97.   Size size;
  98.  
  99.   explicit SizeSpecification(const Size size)
  100.     : size{size}
  101.   {
  102.   }
  103.  
  104.  
  105.   bool is_satisfied(Product* item) override {
  106.     return item->size == size;
  107.   }
  108. };
  109.  
  110. template <typename T> struct AndSpecification : ISpecification<T>
  111. {
  112.   ISpecification<T>& first;
  113.   ISpecification<T>& second;
  114.  
  115.  
  116.   AndSpecification(ISpecification<T>& first, ISpecification<T>& second)
  117.     : first{first},
  118.       second{second}
  119.   {
  120.   }
  121.  
  122.  
  123.   bool is_satisfied(T* item) override {
  124.     return first.is_satisfied(item) && second.is_satisfied(item);
  125.   }
  126. };
  127.  
  128. int main()
  129. {
  130.   Product apple{ "Apple", Color::Green, Size::Small };
  131.   Product tree{ "Tree", Color::Green, Size::Large };
  132.   Product house{ "House", Color::Blue, Size::Large };
  133.  
  134.   std::vector<Product*> all{ &apple, &tree, &house };
  135.  
  136.   BetterFilter bf;
  137.   ColorSpecification green(Color::Green);
  138.  
  139.   auto green_things = bf.filter(all, green);
  140.   for (auto& x : green_things)
  141.     std::cout << x->name << " is green" << std::endl;
  142.  
  143.   SizeSpecification big(Size::Large);
  144.   AndSpecification<Product> green_and_big{ big,green };
  145.  
  146.   auto green_big_things = bf.filter(all, green_and_big);
  147.   for (auto& x : green_big_things)
  148.     std::cout << x->name << " is green and big" << std::endl;
  149.  
  150.   getchar();
  151.   return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement