在重载构造函数中重用代码
我的 BigBlock 类需要一些重载的构造函数。它们都需要以相同的方式初始化相同的几个字段。
执行此操作的正确方法是什么?是否要创建一个函数(例如下面示例中的Initialize
)来执行这些操作,并让所有构造函数都调用该函数?
public class BigBlock {
private Thing parentThing;
Units lengthUnit;
LabCoordinateSystem labCoordinateSystem;
private void Initialize(){
lengthUnit = parentThing.getPreferredUnits(0);
labCoordinateSystem = parentThing.getCoordinateSystem();
}
BigBlock(Thing myThing){
parentThing= myThing;
Initialize();
}
BigBlock(Thing myThing, double x, double y, double z){
parentThing= myThing;
Initialize();
// more code involving x, y, z
}
// a few more constructors
}
My BigBlock
class needs a few overloaded constructors. All of them need to initialize the same few fields in the same way.
What is the proper way to do this? Is it to make a function, e.g. Initialize
in the example below, that does these things, and have all constructors call that function?
public class BigBlock {
private Thing parentThing;
Units lengthUnit;
LabCoordinateSystem labCoordinateSystem;
private void Initialize(){
lengthUnit = parentThing.getPreferredUnits(0);
labCoordinateSystem = parentThing.getCoordinateSystem();
}
BigBlock(Thing myThing){
parentThing= myThing;
Initialize();
}
BigBlock(Thing myThing, double x, double y, double z){
parentThing= myThing;
Initialize();
// more code involving x, y, z
}
// a few more constructors
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与往常一样,“哪种方式是……的正确/最佳方式”的答案是“取决于”。
如果
Initialize
背后的逻辑专门与对象创建相关,并且Thing
在所有创建方面都很常见,那么您可以通过在最简单的构造函数上插入该行为并使用它存在于其他每一处。这会将初始化行为的使用集中在一个地方。在其他情况下,将
Initialize
作为单独的方法可能会很有用。例如,如果initialize
执行一些可重用逻辑,就像您可以在与对象创建不同的时刻调用对象的“重置”。As almost always, the answer to 'which is the proper/best way to...' is "depends".
If the logic behind
Initialize
is specifically coupled to object creation, andThing
is common in all creation aspects, you could model that by inserting that behavior on the simplest constructor, and using it in every other one. That would centralize this usage of the initialisation behavior in only one place.In other circumstances, it may be useful to have
Initialize
as a separate method. For instance, ifinitialize
does some reusable logic, llike a "reset" on the object you may call in a moment different than object creation.通常,最好将所有构造函数链接到包含最多信息的单个构造函数,例如,
如果有不同的方法来调用构造函数,而不能有效地表示相同信息的子集,那么它会变得有点奇怪- 但那时我会说无论如何都有设计的味道。
请注意,当您在单个构造函数中获得所有实际逻辑时,您就不需要
Initialize
方法(应该是initialize
来遵循 Java 命名约定) ,顺便说一句) - 这可能也您可以将字段设为最终的,而以前您无法做到这一点。Typically it's best to make all constructors chain to a single one which contains the most information, e.g.
It becomes slightly weirder if there are different ways to call the constructor which don't effectively represent subsets of the same information - but at that point I'd say there's a design smell anyway.
Note that by the time you've got all the real logic in a single constructor, you don't need your
Initialize
method (which should beinitialize
to follow Java naming conventions, btw) at all - which may also you can make fields final which previously you couldn't have done.只需重用当前的构造函数即可。让所有其他构造函数调用初始化所有必需值的构造函数。
如果
x
、y
和z
需要成为初始化的一部分,BigBlock(Thing myThing)
应调用 < code>BigBlock(Thing myThing, double x, double y, double z) 使用默认值。Just reuse your current constructor. Let every other constructor call the one that initialises all required values.
If
x
,y
andz
need to be part of the initalization,BigBlock(Thing myThing)
should callBigBlock(Thing myThing, double x, double y, double z)
with default values.否 - 您可以使用此构造函数从构造函数调用其他构造函数。
No - you can call other constructors from a constructor using this.