Tuesday 1 October 2013

Design issue in C++

Design issue in C++

This is rather question on a design issue than a problem on c++. I'll try
to describe my thoughts in an abstract way:
I'm the "owner" of some service object A0, dynamically created by a
framework. To do my job I created dependent aggregated classes A1, A2, A3,
... to handle different specific requests.
Within A1, A2, A3... I typically use resources like a database connection,
a property tree etc..., which are available from the framework, which
calls A.
What is the best way, to make these resources "available" from A1...An ?
Typically I have setter-Methods like
a0->setFramework(myFramework);
a1->setFramework(a0->getFramework());
a2->setFramework(a0->getFramework());
a3->setFramework(a0->getFramework());
Then I use other dependent classes, to complete a specific request, e.g. a
ResponseWriterA for A, which needs also some of the mentioned services.
Therefore, I have to implement a setter
ResponseWriterInstanceA->setFramework(a1->getFramework())
and so on.
Although this works, I don't regard it as a good solution, since the same
set of resources is "propagated" through along the whole set of classes.
Moreover, I typically end up with all my subclasses depending somehow on
the whole framework, which makes it hard, to isolate classes ("each
include File is everywhere afterwards").
I would prefer a more centralized access to the set of resources, without
the need to pass data through and to keep the worker classes independent
of other nasty things. But I dont know, how to do that in a "proper"
way...
On the one hand I dont't want to use static (class) methods, but for all
other approaches, I need an object. How to get this instance, if it is not
passed as a dependent object to all of my worker-instances?
What is the typical "golden way" (maybe some kind of design pattern) to
solve such issues?
Many Thanks !

No comments:

Post a Comment