[ fromfile: liborg.xml id: liborg ]
A dependency between two program elements exists if one reuses the other; that is, if building, using or testing one (the reuser) requires the presence and correctness of the other one (the reused).
In the case of classes, a dependency exists if the implementation of the reuser class must change whenever the interface of the reused class changes.
Figure 7.1 shows the dependency between a reuser ClassA
and a reused ClassB
with a UML diagram.
A dependency between ClassA
and ClassB
can arise in a variety of ways.
In each of the following situations, a change in the interface of ClassB
might necessitate changes in the implementation of ClassA
.
ClassA
has a data member that is a ClassB
object or pointer.
ClassA
is derived from ClassB
.
ClassA
has a function that takes a parameter of type ClassB
.
ClassA
has a function that uses a static member of ClassB
.
ClassA
sends a message (e.g., a signal) to ClassB
.[31]
In each case, it is necessary to #include ClassB
in the implementation file for ClassA
.
Code reuse, a valuable and important goal, always produces dependencies.
When designing classes and libraries, you need to make sure that you produce as few unnecessary or unintentional dependencies as possible
Each #include
directive produces a dependency and should be carefully examined to make sure that it is really necessary.
In a class definition header file, one good rule to follow is this: Do not use an #include
if a forward declaration suffices.
For example, the header file "classa.h"
might look something like this:
#include "classb.h" #include "classd.h" // other #include directives as needed class ClassC; // forward declaration class ClassA : public ClassB { public: ClassC* f1(ClassD); // other stuff that does not involve ClassC };
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |