- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
假设我有一个 Foo 类,它有很多内部变量,只有一个构造函数和非修改方法。内部变量值的计算涉及状态变量和复杂函数,因此不应成为 Foo 的一部分。因此,我创建了一个执行微积分的类 FooCreator,并最终创建了 Foo。
首先我在 FooCreator 中单独实现了创建 Foo 所必需的所有成员变量,并添加了一个 createFoo() 函数,但最后 FooCreator 几乎变成了 Foo 的拷贝加上一些状态变量。
接下来,我决定让 FooCreator 继承自 Foo,这样可以节省大量的输入,但是这感觉不像是一个干净的解决方案,因为要获得某物。我必须使 Foo 的成员变量受到保护而不是私有(private)的,因此向其他用户公开的内容比我想要的要多。除了创建之外,不应将 Foo 用作基类。
我看过工厂模式,但这似乎和构建器一样大材小用。这个问题肯定会很普遍。那么处理这个问题的正确方法是什么?
一些代码示例如下所示:
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();
};
最佳答案
It can be good to keep complex logic out of the constructor .但是,如果您不想使用Factory 或Builder 模式,那么您可以提取一个ParameterObject。 .
如果您需要使用 Foo
中的方法来创建实例,那么也许您应该尝试将 Foo
拆分为两个类。
关于c++ - 使用什么模式将对象创建与其类分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8231955/
我像那样遍历数组。 NSArray *array = [[currentRaum raumattribute] allObjects]; NSString *compositeString =
我想找到所有引用这种模式的子字符串:一些字符+一些字符+第一个字符。现在我在 Python 2.7 中有了这个: T = "i was here" m = re.findall(r"([a-z])[a
我想使用与 tidyr 分开将一列字符串(例如 [1, 58, 10] )分成几列。我的问题是有时列较短(永远不会更长)。我在同一个数据框中有很多列有这个问题。 加载包 require(tidyr)
我正在开发一个具有图形用户界面的网络测试工具。我现在面临的问题是,我无法将基础数据与 GUI 类分开。该应用程序由一个 QMainWindow 组成,它随后生成多个其他 QDialogs 并具有一些
我经常听到“策略与机制分离”的口头禅,尤其是在 Unix 哲学的背景下。这是什么意思,有哪些具体的例子?什么时候/为什么是/不是一件好事? 最佳答案 它基本上是将需求或业务功能与技术实现分离。机制是技
我正在使用 writeToFile:atomically: 方法将一些加密数据写入文本文件。问题是,需要保存的文件必须是用户加密的文件,并带有我选择的扩展名。这是我到目前为止所拥有的: [encryp
我有这串 abcdef x y z 或这个 "ab cd ef" x y z 我正试图将其解析为 s1 = "abcdef" arr = ["x","y","z"] 或者 s1 = "ab cd e
这个问题已经有答案了: One big javascript file or multiple smaller files? [duplicate] (7 个回答) 已关闭 6 年前。 我有 4 种类
我有这样的事情 - function DetailCtrl($scope) { $scope.persons = [{ id: 1, name: "Mark"
在操作(复制/移动)包含合并单元格的范围时,我总是收到错误消息“您的粘贴与合并单元格重叠。请取消合并单元格,然后重试”。但是,当尝试使用 Range#breakApart 取消合并范围内的单元格时,我
我有一个包含一些 TextFields 的 TableView。所述 TextFields 的值链接到二维数组(NSMutableArrays 的 NSArray)中的某些位置。 一个初始的干净数组定
我定义了一个标签,其中一半需要在左侧,另一半文本需要在右侧。我怎样才能解决这个问题,让另一半拉对? 我添加了 margin-right 以使文本向右拉,但它与其他 div 不一致。
我正在尝试创建一个正则表达式来将 JavaScript 中的每个单词与 .(点)分开。 function myFunction() { var url = "in.k1.k2.k3.k4.com"
如何使用 CSS 将网站的正文/内容区域与背景分开。为了向您展示我的意思,请看附图。因此,两侧的背景将扩展到拥有超大显示器的人,但内容将始终保持相同大小。 谢谢,阿马尔 http://i.imgur.
有可能用 CSS 将两个背景图像对 Angular 分开吗? 我知道如何只用一张图片制作它,但我不能用两张图片制作它。 这是一个例子: |-------------| | /|
这是一个JSFiddle我创建了展示代码的外观。我将如何给予这些 它们之间是否存在间隙,没有一个元素低于另一个元素? .main-content { width: 50%; float: le
我正在处理具有这样数据的项目(我使用带有 python 的 pandas 框架): days rain 0 1 2 0 3 1 1
我正在尝试编写一个宏来获取信息并将该信息发送到另一个函数,方法是将原始 va_list 拆分为字符串,然后从原始 va_list 生成另一个 va_list。 下面是我的代码。 调用宏 /* Usag
我需要来自 SharedToDomains 和 SharedFromDomains 的键和值数据。我想打印这些值。 var LogResponse = DeserializeFromJson(sLog
我现在正在使用 Alamofire 来发出发布请求。我首先在 ViewController 中构建它并开始工作。但后来我试图通过在另一个 class 中构建它来分离它。我使用 singleton 并且
我是一名优秀的程序员,十分优秀!