I have a client class A
which has some data structure (m_data
). Now I need a function which needs to perform some operations on the data. Is it a good practice to use the dependency injection, so the given service will access m_data
of the client class?
Note
: There are more service, they all operate on m_data
, I would just like to avoid inheritance. The reason for that is that class A is also a derived class, so there might be other classes delivering the foo()
functionality, but not the m_data
data structure.
class B()
{
public:
// constructor, destructor, ...
void foo();
}
class Service()
{
public:
// constructor, destructor, ...
void foo(Data);
}
class A() : public Base
{
public:
A(Service &_serv) :
m_serv(_serv)
{
init_data(); // initializes m_data
}
void foo()
{
m_serv.foo(m_data) // is this a good practice?
}
private:
Service m_serv;
Data m_data;
}
I guess this is a bad practice, so could someone provide some good design patterns, or any other solutions? Thanks!
Aucun commentaire:
Enregistrer un commentaire