好或坏 - 使用构造函数从远程 Web 服务初始化模型
我想知道通过对 Web 服务的请求来初始化我的模型是好是坏,还是使用在构造函数之后调用的另一个公共方法更好
例如:
class Model {
ModelData data;
Model(Integer model_id) {
data = Request.getDataFromWebServices(model_id);
}
}
I would like know if it good or bad to initialize my model by a request to a webservices or is it better to use an another public method called after the constructor
For example:
class Model {
ModelData data;
Model(Integer model_id) {
data = Request.getDataFromWebServices(model_id);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通常,使用构造函数 args 参数是一个很好的方法,这些参数是类正常运行所必需的(而不是使用 setter)。
因此,在您的情况下,如果
model_id
是Model
工作所必需的,那么您在那里拥有它是正确的。现在您可以使用
model_id
进行远程方法调用。远程方法调用可能需要更多时间来执行,从而使
Model
需要更多时间来初始化,并且可能会因网络原因而失败。如果 API 涵盖了来自网络层或实际处理的任何异常,并返回一个好的值来初始化模型,那么恕我直言,它应该没问题。
只需将类记录为由于网络访问而花费更多时间来初始化
It is generally a good aproach to use as constructor args parameters that are required for the class to be functional (instead of using setters).
So in your case if
model_id
is mandatory forModel
to work, it is correct you have it there.Now you use
model_id
to do a remote method call.Remote method calls can take more time to execute, making
Model
taking more time to initialize and could fail e.g. due to network reasons.If the api covers any exception either coming from network layer or from the actual processing and returning a good value to initialize the
Model
then IMHO it should be ok as it is.Just document the class as taking more time to initialize due to network access
问题不在于构造函数或其他任何地方的使用,问题在于使用全局变量。最好通过实例传递
request
以显示模型 API 中的依赖关系:或者通过更具描述性的工厂方法创建:
我使用原始
int
而不是Integer
因为我想表明null
不是model_id
的有效值。The problem is not the use in constructor or anywhere else, the problem is using a global variable. It would be much better to pass
request
by instance to show the dependency in the API of Model:or create by a more descriptive factory method:
I used the primitive
int
instead ofInteger
because I wanted to shaw thatnull
is not a valid value formodel_id
.看来这很大程度上取决于您的个人情况。
我个人可能会将构造函数设为私有,并公开一个名为“CreateModelFromService”之类的公共静态方法,以使使用我的类的其他人清楚地知道,由于网络等原因,该方法可能会失败的可能性大于正常情况连接性等。在实例化一个看似与网络无关的类时,我通常不会想到会出现这样的错误。
只是我的 0.02 美元
Seems like this is highly dependent on your individual circumstance.
I personally would probably make the constructor private, and expose a public static method called something like "CreateModelFromService" to make it clear to anyone else using my class that there is a larger-than-normal chance the method might fail due to things like network connectivity, etc. I typically wouldn't expect such an error when instantiating a seemingly non-network-related class.
Just my $0.02
取决于该组件将在哪里使用。大多数程序员希望构造函数能够相当快地完成,因此进行网络操作可能不是最好的主意。
Depends where this component will be used. Most programmers expect constructors to complete fairly quickly so doing network operations may not be the best idea.
实例化时和之后的初始化都是有效的。真正的问题是您正在处理哪些限制,什么在某些时候可能会失败,您何时需要访问状态以及您是否能够在需要访问状态来计算状态之前处理潜在的延迟。两者都没有对错之分,除非您的情况需要另一个。
Both initializing on instantiation and later are valid. The real questions are what restrictions are you dealing with, what might fail at some point, when will you need to access the state and will you be able to work with a potential delay waiting until you need to access the state to compute the state. Neither is right or wrong unless your situation calls for the other.
假设您想要对需要模型类之一的实例的内容进行单元测试。如果没有 Web 服务,您将如何创建此实例?
可能值得考虑使用 工厂 来使用 Web 服务创建模型实例。这将您的模型与特定的组装机制分开:
Let's say you want to unit test something that requires an instance of one of your model classes. How will you create this instance without the webservice?
It might be worth considering a factory to create model instances using a webservice instead. This separates your model from the specific assembly mechanism: