Both are used to reduce compilation dependency between files.
1. Compilation dependency
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 is changed or the implementation of class_name in file is changed, all files are included. 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 only make small changes to class_name.
2 handler class
Handle class only improves all interfaces and contains a pointer to the real implementation class. The real implementation class is included in another file. When the class is modified, only file. H will cause re-compilation, and include file. Other files of H will not be re-compiled
# Include "file1.h" // contain member1
# Include "file2.h" // contain member2
# Include "Implement. H"
Class class_name
{
Class_impl * implement; // shared pointer is usually used.
Public:
Member1 get_member_1 () const
{
Return implement-> get_member_1 ();
}
Member2 get_member_2 () const
{
Return implement-> 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, declare that the class is an abstract base class. Generally, the interfaces in it are pure virtual functions, and then provide a static create function. This function returns the object of a specific subclass of this abstract class, the return value in the function declaration is still a pointer or reference to this abstract class. The specific subclass is declared in another file.
Class base
{
Public:
Virtual ~ Base (){}
Void dosomething ()
{
Stepone ();
Steptwo ();
}
PRIVATE:
Virtual void stepone () = 0;
Virtual void steptwo () = 0;
};
Class derived: public Base
{
PRIVATE:
Virtual void stepone ()
{
Cout <"derived stepone: Do something." <Endl;
}
Virtual void steptwo ()
{
Cout <"derived steptwo: Do something." <Endl;
}
};
Handle Class & Interface Class