简体   繁体   English

如何减少多个采用不同类型1个参数的方法?

[英]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.

相关问题 如何将一种方法的结果作为另一种方法的参数 - how can I take one methods result as another methods' parameter 如何使方法将任何类型的数组作为参数? - How can I make a method take an array of any type as a parameter? 如何简化只有 Parameter 不同的 2 种方法? - How can I simplify 2 methods with only Parameter different? 如何创建带有任何类型参数的通用方法? - How to create generic methods that take any type parameter? 如何在不同的类型ArrayList中使用相同的方法? - How can I use same methods in different classes type ArrayList? 我可以为多个具有不同签名和返回类型的JPA存储库方法提供一个@Query吗? - Can I have one @Query for multiple JPA repository methods with different signature and return type? 如何调用作为参数类型传递的不同类的方法? - How to call methods of different classes passed as parameter type? 我如何才能使侦听器方法超出Java的范围? - How can I take listener methods out of their scope in Java? 如何返回作为参数给出的相同Collection类型,但具有不同的元素类型? - How can I return the same Collection type given as a parameter, but with a different element type? 在Eclipse中,是否可以在项目中找到采用某种参数类型的所有方法? - In Eclipse, is it possible to find all methods in project that take a certain parameter type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM