Usually, the providers of a program want to hide the module’s concrete technique implementation. In this post, two ways for this goal will be introduced. One is called pImpl, another is pure abstract class.
pImpl
pImpl means “pointer to Implementation” which is a C++ programming technique. It can remove the implementation details of a class from its object representation by placing them in a separate class. pImpl can break the compilation dependency, changes to the implementation will not cause recompilation. So if a library uses pImpl as its interface, new version of the library may change implementation while remaining the interface with older versions.
As the object of the interface type controls the lifetime of object of the implementation type, the pointer to the implementation type is usually std::unique_ptr
.
|
|
|
|
|
|
|
|
Pure abstract class
Pure abstract class is the class which includes pure virtual functions as its member functions. This type of class should not be instantiated but used as a base class. Usually, it plays a role as an interface class in programming.
|
|
|
|
|
|
To set constructor, copy constructor and assignment operator as private member, the object of implementation class can only be instantiated by factory function. Meanwhile, the factory function should be included as a protect member of implementation class for accessing its private constructor.