Let's assume we've a class A. In one of its methods it sets up environment for another class B and creates the object of B. In the name of encapsulation it seems reasonable to hide B's logic inside it and make it use the environment A has set up by accepting this pointer of A and calling A's needed public method through it. And here comes the issue: all this construction starts to look like callbacks (we've a sandwich of A's, B's and A's again in the stack), what I find improper and kind of 'dirty' in OOP designs. Even worse, in my code I have to implement this pattern several times in a nested manner - B is going to set up environment for C and pass its this to it and so on. So could you please help me to design this piece of code in a somehow cleaner way?
Thank you.
Here's the code sample:
class Bar {
Foo *m_foo;
public:
Bar(Foo *foo) : m_foo(foo)
{
}
bar()
{
...
m_foo->foo0();
...
}
};
class Foo {
public:
foo0()
{
// method needed by Bar to implement its logic
}
foo1()
{
// prepare the environment for Bar
...
m_bar = new Bar(this);
m_bar->bar();
...
}
};
Aucun commentaire:
Enregistrer un commentaire