Imagine you have following class
1 | class WebBrowser { |
We want to add another function clearEverything
which calls clearCache
, clearHistory
and removeCookies
. Which way is better?
1 | // first approach is add clearEverything as member function |
Second approach is better, because it provides better encapsulation. The more public methods which can access private data, the worse encapsulation the class has. In the second approach, it uses existing method of WebBrowser
without add public interface to it, which yields better encapsulation.
Above example, clearBrowser
is a utility funtion which client can call. Defining it in a namespace related to WebBrowser
class is a nature way of providing the functionality. If you have other utility function which may not be needed by all clients, you can even distribute them into different header files with same namespace to provide better isolation of functionalities. This is how STL organize it’s classes and functioinalities. Rather than having a single monolithic <C++StandardLibrary>
header containing everything in the std namespace, there are dozens of headers (e.g., <vector>
, <algorithm>
, <memory>
, etc.), each declaring some of the functionality in std.
Practically speaking, you can implement WebBrowser
related functionality as following
1 | // header "webbrowser.h" — header for class WebBrowser itself |