Assume I have a Class Foo which has many internal variables, only one constructor and non modifying methods.
The calculation of the values for the internal variables involves state variables and complex functions and thus should not be part of Foo. Thus I created a class FooCreator that performs the calculus and in the end creates Foo.
First I implemented all member variables necessary for the creation of Foo separately in FooCreator and added a createFoo() function, but in the end FooCreator became almost a copy of Foo plus some more state variables.
Next I decided to let FooCreator inherit from Foo which saves a lot of typing but
this just does not feel like a clean solution, since to gain sth. I have to make Foo’s member variables protected instead of private, thus exposing more than I want for other users. Foo should not be used as base class except for the creation.
I had a look at the factory pattern but that seems overkill as well as the builder.
Surely this problem will be very common. So what would be the proper way to deal with this problem?
Some code example would look like this:
class Foo{
private: //protected for second case
int mVal;
State mState;
ComplexStuff mStuff;
//...
public:
Foo():mVal(),mState(),mStuff(){}
Foo(int val, State const& state, ComplexStuff const& stuff):
mVal(val),mState(state),mStuff(stuff){}
bool loadFromFile();
bool evalStateTransition(State const& formerState) const{/*....*/}
bool someTest(int) const{/*....*/};
GraphicItem* visualize() const{/* ....*/};
//...
};
class FooCreator{
private:
int mVal;
State mState;
ComplexStuff mStuff;
StuffVisualizer mStuffVis;
Paramset mParams;
//...
public:
FooCreator(Paramset const& set):
mVal(),mState(),mStuff(),mStuffVis(),mParams(set){}
constructState(int, int, int);
gatherStuff1(File file1);
Foo createFoo();
int evalStateTransition(State const& formerState) const{
/*same long code as in Foo*/
}
bool someTest(int) const{ /*same long code as in Foo*/}
GraphicItem* visualize() const{ /*same long code as in Foo*/}
//...
};
class FooCreator2 : public Foo{
private:
StuffVisualizer mStuffVis;
Paramset mParams;
//...
public:
FooCreator(Paramset const& set):Foo(),mParams(set){}
constructState(int, int, int);
gatherStuff1(File file1);
Foo createFoo();
};
just write
friend class FooCreator;in Foo, and now you can access all the private fields of Foo from FooCreator. Then just create a field of type Foo in FooCreator… Nice, and easy.