Caminhoneiro

Dependency Injection / Decorator pattern (SOLID)

Jul 11th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. // Interface Segregation Principle
  2.  
  3. // No client should be forced to depend on methods it does not use
  4. #include "stdafx.h"
  5. #include <vector>
  6.  
  7. struct Document;
  8.  
  9. //struct IMachine
  10. //{
  11. //  virtual void print(std::vector<Document*> docs) = 0;
  12. //  virtual void scan(std::vector<Document*> docs) = 0;
  13. //  virtual void fax(std::vector<Document*> docs) = 0;
  14. //};
  15. //
  16. //struct MFP : IMachine
  17. //{
  18. //  void print(std::vector<Document*> docs) override;
  19. //  void scan(std::vector<Document*> docs) override;
  20. //  void fax(std::vector<Document*> docs) override;
  21. //};
  22.  
  23. struct IPrinter
  24. {
  25.   virtual void print(std::vector<Document*> docs) = 0;
  26. };
  27.  
  28. struct IScanner
  29. {
  30.   virtual void scan(std::vector<Document*> docs) = 0;
  31. };
  32.  
  33. struct IFax
  34. {
  35.     virtual void fax(std::vector<Document*> docs) = 0;
  36. };
  37.  
  38. struct Printer : IPrinter
  39. {
  40.   void print(std::vector<Document*> docs) override;
  41. };
  42.  
  43. struct Scanner : IScanner
  44. {
  45.   void scan(std::vector<Document*> docs) override;
  46. };
  47.  
  48. struct Fax:IFax
  49. {
  50.     void fax(std::vector<Document*> docs) override;
  51. };
  52.  
  53. //Dependecy injection
  54. struct IMachine : IPrinter, IScanner {};
  55.  
  56. //Decorator pattern
  57. struct Machine : IMachine
  58. {
  59.   IPrinter& printer;
  60.   IScanner& scanner;
  61.  
  62.   Machine(IPrinter& printer, IScanner& scanner)
  63.     : printer{printer},
  64.       scanner{scanner}
  65.   {
  66.   }
  67.  
  68.   void print(std::vector<Document*> docs) override {
  69.     printer.print(docs);
  70.   }
  71.   void scan(std::vector<Document*> docs) override {
  72.     scanner.scan(docs);
  73.   }
  74. };
Advertisement
Add Comment
Please, Sign In to add comment