[英]How can I reduce multiple methods that take 1 parameter of different type?
I have the following code: 我有以下代码:
public final boolean doesExistById(Long id) {
return dataAccessObject.findById(id) != null;
}
public final boolean doesExistByName(String name) {
return dataAccessObject.findByName(name) != null;
}
public final boolean doesExistByDisplayName(String displayName) {
return dataAccessObject.findByDisplayName(displayName) != null;
}
public final boolean doesExistByWebId(String webId) {
return dataAccessObject.findByWebId(webId) != null;
}
My Product
class has properties id, name, displayName, wedId
. 我的
Product
类具有属性id, name, displayName, wedId
。
dataAccessObject.findBy____()
returns an object of type Product
, if it can be found in the data store, or null
if it cannot. dataAccessObject.findBy____()
返回Product
类型的对象(如果可以在数据存储中找到),如果不能,则返回null
。
I would like to reduce this chunk of code, if possible, because I have many objects that require the doesExist()
pattern as above. 如果可能的话,我想减少这段代码,因为我有很多对象需要上面的
doesExist()
模式。 The client code will only know one of these properties. 客户端代码只会知道其中一个属性。
A possible solution I thought of would be to do this: 我想到的一个可能的解决方案是:
public final boolean doesExist(Long id, String name, String displayName, String webId) {..}
and then call it with null
for unknown fields while using if
statements to determine which field has a value. 然后在使用
if
语句确定哪个字段具有值时使用null
为未知字段调用它。 But is there another way that is more elegant? 但还有另一种更优雅的方式吗?
You are recognizing that the "does exist" part of all of these methods is exactly the same, and that makes you want to avoid repeating it, but the "ByXxx" part of these methods is completely different. 您认识到所有这些方法中“确实存在”的部分完全相同,这使您希望避免重复它,但这些方法的“ByXxx”部分完全不同。
What you've got is far better than what you're thinking of doing. 你得到的东西比你想做的要好得多。 Please don't change your method signature to require callers to provide null values for all but one argument.
请不要更改方法签名,以要求调用者为除一个参数之外的所有参数提供空值。 That is highly error-prone, as it provides no compile-time errors for a variety of different ways that people might use the method signature incorrectly.
这非常容易出错,因为它不会为人们可能错误地使用方法签名的各种不同方式提供编译时错误。
One thing you might want to consider doing is separating the "does exist" piece of this into its own mechanism: 您可能想要考虑做的一件事是将“确实存在”的部分分成它自己的机制:
public final Optional<MyObj> byId(Long id) {
return Optional.ofNullable(dataAccessObject.findById(id));
}
So instead of saying service.doesExistById(123)
, you'd say service.byId(123).isPresent()
. 因此,不是说
service.doesExistById(123)
,而是说service.byId(123).isPresent()
。 This represents the same meaning semantically, but it's broken into separate parts, which means you can reuse byId()
, byName()
, etc., for other purposes where you need the actual object, and not just to know whether it exists. 这在语义上表示相同的含义,但它被分解为单独的部分,这意味着您可以重用
byId()
, byName()
等,以用于需要实际对象的其他目的,而不仅仅是知道它是否存在。
You can write a method that takes object type I recommend you check out this page. 您可以编写一个采用对象类型的方法我建议您查看此页面。 What is reflection and why is it useful?
什么是反思,为什么它有用?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.