Both are used to reduce compilation dependency between files.
1. Compilation dependency
# Include " File1.h "
# Include " File2.h "
Class Class_name
{
Member1 m_m1;
Member2 m_m2;
Public:
Member1 get_member_1 ()Const{};
Member2 get_member_2 ()Const{};
} ;
Assume that the header file is file. h. When file1.h or file2.h changes, or the implementation of class_name in file changes, all include file. H files have to be re-compiled when file. when H is contained in many files, it takes a lot of compilation time even if you make a small change to class_name.
2. Handle class (handle class)
Handle classs only improves all interfaces and contains a pointer to the real implementation class. The real implementation class is included in another file. to modify this class, only file. H will cause re-compilation, while other files containing file. H will not cause re-compilation.
# Include " File1.h "// Contain member1
# Include " File2.h "// contain member2
# Include "Implement. H"
Class Class_name
{
Class_impl * Implement; // Generally, shared pointer is used.
Public :
Member1 get_member_1 () Const
{
ReturnImplement->Get_member_1 ();
}
Member2 get_member_2 () Const
{
ReturnImplement->Get_member_2 ();
}
} ;
The following is the implementation of implement. h.
Class Class_impl
{
Member1 M_1;
Member2 M_2;
Public ;
Member1 get_member_1 () Const {}
Member2 get_member_2 () Const {}
} ;
2. Interface Class
This is another way to create a handle class.
First, the declared class is an abstract class. Generally, the interfaces in the class are pure virtual functions, just like Java interfaces.
Then raise a static create function (that is, the factory method). This function returns the object of a specific subclass of the abstract class, and the return value in the function declaration is still the pointer or reference of the abstract class.
The specific subclass is declared in another file.