Whether it is modular design, object-oriented design or layered design, it is the key internal requirement to realize the external hiding of information in subsystem. With my simple experience, the information hiding according to the extent of the different divided into (1) not available (2) visible unavailable (3) visible is available.
1 not visible and unavailable
This means that the variables, structs, and class definitions inside the module are completely hidden from the outside, and the outside is ignorant. The common way to implement this is to use an opaque pointer, see my blog C language Development function library, using opaque pointers to hide the details of the structure.
This approach is also applicable to C + + language, a possible implementation of interface-oriented programming.
Header file IMyClass.h
Class IMyClass
{public
:
virtual ~imyclass ();
Public:
virtual void Public_function1 ();
virtual void Public_function2 ();
imyclass* Createmyclassobject ();
Implementing File MyClass.cpp
#include "IMyClass.h"
class myclass:imyclass.h
{
private:
int x;
int y;
int Z;
Public:
virtual void Public_function1 ();
virtual void Public_function2 ();
imyclass* Createmyclassobject ()
{return
new MyClass ();
}
This implementation can be applied both at the source level and at the library level.
In theory, complete realization is the most perfect design. However, this is very demanding for program design, and will bring more code and design logic levels as well as some limitations (such as the inability to implement inheritance of existing types).
Therefore, many C + + libraries are designed to use the following invisible way, such as MFC.
2 visible is not available
This method refers specifically to C + +, referring to a non-public type member in a C + + class. For example, in the header file Myclass.h:
Class MyClass
{
private:
int x;
int y;
int Z;
Protected:
float F;
Public:
int M;
void Member_method1 ();
For call callers, when you include a secondary header file, you can see member variables such as X,Y,Z, but you can't use them. As long as clients need to use new to generate instances or inherit classes, they must know the full definition of the class.
This is not the case for C, because any variable in the structure is public.
3 Visible Available
That is, there is no hidden, the design must be avoided.
The above mentioned is aimed at the implementation of the three types of information hiding in C + + program development, hope to help all above!