Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- // Virtuality
- // Why do we make virtual functions public, protected or private?
- // public: rarely, protected: sometimes and private: by default
- /*
- Traditionally, many programmers were used to writing base classes
- using public virtual functions to directly and simultaneously specify both the interface and the customizable behavior.
- The problem is that "simultaneously" part,
- because each virtual function is doing two jobs: It's specifying interface because it's public and therefore directly part of the interface Widget
- presents to the rest of the world; and it's specifying implementation detail, namely the internally customizable behavior,
- because it's virtual and therefore provides a hook for derived classes to replace the base implementation of that function (if any).
- That a public virtual function inherently has two significantly different jobs is a sign that it's not separating concerns well
- and that we should consider a different approach.
- What if we want to separate the specification of interface from the specification of the implementation's customizable behavior?
- Then we end up with something that should remind us strongly of the Template Method pattern:
- **Prefer to use Template Method to make the interface stable and nonvirtual,
- while delegating customizable work to nonpublic virtual functions that are responsible for implementing the customizable behavior.
- After all, virtual functions are designed to let derived classes customize behavior;
- it's better to not let publicly derived classes also customize the inherited interface, which is supposed to be consistent.
- */
- void print_output(std::string value);
- /*
- A more modern base class, using
- Template Method to separate interface from internals.
- */
- class Widget
- {
- public:
- // Stable, nonvirtual interface.
- int Process( Gadget& ); // uses DoProcess...()
- bool IsDone(); // uses DoIsDone()
- // ...
- private:
- // Customization is an implementation detail that may
- // or may not directly correspond to the interface.
- // Each of these functions might optionally be
- // pure virtual, and if so might or might not have
- // an implementation in Widget.
- virtual int DoProcessPhase1( Gadget& );
- virtual int DoProcessPhase2( Gadget& );
- virtual bool DoIsDone();
- // ...
- };
- void print_output(std::string value = "")
- {
- std::cout << "Output: " << value << std::endl;
- }
- // Main function for the program
- int main() {
- std::cout << std::endl;
- print_output("end");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement