问题描述
假设我有一个类Foo有很多内部变量,只有一个构造函数和非修改方法。内部变量的值的计算涉及状态变量和复杂函数,因此不应该是Foo的一部分。因此,我创建了一个类FooCreator来执行演算,最终创建Foo。
首先我在FooCreator中实现了创建Foo所需的所有成员变量, createFoo()函数,但最终FooCreator几乎成为Foo的一个副本加上一些更多的状态变量。
接下来我决定让FooCreator继承Foo,的输入但
这只是不觉得像一个干净的解决方案,因为获得sth。我必须使Foo的成员变量受保护而不是私有,从而暴露比我想要的其他用户。 Foo不应该用作基类除了创建。
我看了工厂模式,但似乎过度杀伤以及生成器。
当然这个问题会很常见。那么,什么是正确的方法来处理这个问题?
一些代码示例如下所示:
class Foo {
private:// protected for second case
int mVal;
状态mState;
ComplexStuff mStuff;
// ...
public:
Foo():mVal(),mState(),mStuff(){}
Foo 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 {
/ *与Foo中相同的长代码* /
}
bool someTest(int)const {/ *与Foo中的相同的长代码* /}
GraphicItem * visualize()const {/ *与Foo中的相同的长代码* /}
// ...
};
class FooCreator2:public Foo {
private:
StuffVisualizer mStuffVis;
Paramset mParams;
// ...
public:
FooCreator(Paramset const& set):Foo(),mParams(set){}
constructState ,int);
gatherStuff1(File file1);
Foo createFoo();
};
只要写好友类FooCreator ; 在Foo中,现在你可以从FooCreator访问Foo的所有私有字段。然后在FooCreator中创建一个类型为Foo的字段...很好,很容易。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.
这篇关于使用什么模式将对象创建与其类分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!