My concern is that when using either shared_ptr
or unique_ptr
I stick to one ownership model - either injected objects is shared or my own. And I think this is is secondary class responsibility - to care of injected objects lifetime.
So, does it violates SRP - assuming that class already has some responsibility.
Some simple example:
class Calculator {
public:
Calculator(std::unique_ptr<Adder> adder) : adder(adder) {}
void add();
private:
std::unique_ptr<Adder> adder;
};
When design changes - so I will have many different calculators - then I need to change unique_ptr
to shared_ptr
. So even if Calculator
main responsibility (to calculate) did not change - I need to change the class.
Wouldn't be better to use simple references for injected objects - and just left the responsibility of injected objects lifetime to some other classes?