Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. // Virtuality
  5. // Why do we make virtual functions public, protected or private?
  6. // public: rarely, protected: sometimes and private: by default
  7.  
  8. /*
  9. Traditionally, many programmers were used to writing base classes
  10. using public virtual functions to directly and simultaneously specify both the interface and the customizable behavior.
  11. The problem is that "simultaneously" part,
  12. because each virtual function is doing two jobs: It's specifying interface because it's public and therefore directly part of the interface Widget
  13. presents to the rest of the world; and it's specifying implementation detail, namely the internally customizable behavior,
  14. because it's virtual and therefore provides a hook for derived classes to replace the base implementation of that function (if any).
  15. That a public virtual function inherently has two significantly different jobs is a sign that it's not separating concerns well
  16. and that we should consider a different approach.
  17. What if we want to separate the specification of interface from the specification of the implementation's customizable behavior?
  18. Then we end up with something that should remind us strongly of the Template Method pattern:
  19.  
  20. **Prefer to use Template Method to make the interface stable and nonvirtual,
  21. while delegating customizable work to nonpublic virtual functions that are responsible for implementing the customizable behavior.
  22. After all, virtual functions are designed to let derived classes customize behavior;
  23. it's better to not let publicly derived classes also customize the inherited interface, which is supposed to be consistent.
  24. */
  25.  
  26. void print_output(std::string value);
  27.  
  28. /*
  29. A more modern base class, using
  30. Template Method to separate interface from internals.
  31. */
  32. class Widget
  33. {
  34. public:
  35. // Stable, nonvirtual interface.
  36. int Process( Gadget& ); // uses DoProcess...()
  37. bool IsDone(); // uses DoIsDone()
  38. // ...
  39. private:
  40. // Customization is an implementation detail that may
  41. // or may not directly correspond to the interface.
  42. // Each of these functions might optionally be
  43. // pure virtual, and if so might or might not have
  44. // an implementation in Widget.
  45.  
  46. virtual int DoProcessPhase1( Gadget& );
  47. virtual int DoProcessPhase2( Gadget& );
  48. virtual bool DoIsDone();
  49. // ...
  50. };
  51.  
  52. void print_output(std::string value = "")
  53. {
  54. std::cout << "Output: " << value << std::endl;
  55. }
  56.  
  57. // Main function for the program
  58. int main() {
  59.  
  60.  
  61.  
  62. std::cout << std::endl;
  63. print_output("end");
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement