Think of it as a subcomponent like java SubComponent extends ParentComponent
where everything is protected
. It will have access to the same members as the parent and can just use all of the parents things.
A component dependency on the other hand is like a delegate object, where you only have access to the public methods that the class exposes.
Lets just assume we have 2 classes that Dagger can create and provide. Whatever this might be. It does not matter if you use constructor injection or create the objects from a module...We just assume we have those 2 classes that can be provided by some BaseComponent
, which I will show next.
// some module can create either object
class ComponentPrivateObject {}
class ExposedObject {}
It does not matter where they come from. The next part is the interesting bit. We declare one of those classes as a return type on a method on our component interface. This is what Dagger calls a Provision Method. It says that this component can provide that class.
@Component(modules=/* some modules */)
class BaseComponent {
// BaseComponent can provide ExposedObject!
ExposedObject exposedObject();
}
The difference between subcomponent and dependent component is that, as with the java sample in the beginning, Subcomponent
will have access to both ComponentPrivateObject
and ExposedObject
because it just extends the parents graph.
// can use either class, it just adds to the parent
@Subcomponent
class Subcomponent {}
On the other hand, DependentComponent
only has access to ExposedObject
. It does not extend the graph of BaseComponent
, but it just binds BaseComponent
as a provider for some objects. Like with the java sample above, its like using its public API, and if you try to use ComponentPrivateObject
Dagger will tell you that it cannot be provided...
// binds component dependency as provider
@Component(dependency=BaseComponent.class)
class DependentComponent{}
One extends the parent, the other one uses its public API.