gpt4 book ai didi

java - 如何将 setter 方法传递给 Java 方法的参数?

转载 作者:行者123 更新时间:2023-12-05 09:36:56 26 4
gpt4 key购买 nike

关于重构方法的快速问题,以及如何在 Java 中将 setter 方法作为方法传递。

我曾经有一个越来越大的遗留方法,遗留方法实际上只是更新客户ID,通过什么方式(电子邮件,电话,传真,推特等)我们联系了客户,比如:

 final Map<Integer, Customer> customerID_and_customer_Map = getCustomerIDandCustomerContactedByXXXMap();

for (final Integer customerId : getListOfCustomersContactedByTelephone()) {
if (customerID_and_customer_Map.containsKey(customerId)) {
final Customer customer = customerID_and_customer_Map.get(customerId);
customer.setCustomerContactedByTelephone(true);
customer.setCustomerHasBeenContactedAtLeastBySomethingOnce(true);
customerID_and_customer_Map.put(customerId, customer);
}
}
for (final Integer customerId : getListOfCustomersContactedByTwitter()) {
if (customerID_and_customer_Map.containsKey(customerId)) {
final Customer customer = customerID_and_customer_Map.get(customerId);
customer.setPartnerContactedByTwitter(true);
customer.setCustomerHasBeenContactedAtLeastBySomethingOnce(true);
customerID_and_customer_Map.put(customerId, customer);
}
}

随着时间的推移,我们只是为与合作伙伴的每种新联系方式添加 for 循环。而这个从两个 for 循环开始的方法,现在已经有 50 多条了。

final Map<Integer, Customer> customerID_and_customer_Map = getCustomerIDandCustomerContactedByXXXMap();
for (final Integer customerId : getListOfCustomersContactedByTelephone()) {
if (customerID_and_customer_Map.containsKey(customerId)) {
final Customer customer = customerID_and_customer_Map.get(customerId);
customer.setCustomerContactedByTelephone(true);
customer.setCustomerHasBeenContactedAtLeastBySomethingOnce(true);
customerID_and_customer_Map.put(customerId, customer);
}
}
for (final Integer customerId : getListOfCustomersContactedByTwitter()) {
if (customerID_and_customer_Map.containsKey(customerId)) {
final Customer customer = customerID_and_customer_Map.get(customerId);
customer.setPartnerContactedByTwitter(true);
customer.setCustomerHasBeenContactedAtLeastBySomethingOnce(true);
customerID_and_customer_Map.put(customerId, customer);
}
}
for (final Integer customerId : getListOfCustomersContactedByEmail()) {
if (customerID_and_customer_Map.containsKey(customerId)) {
final Customer customer = customerID_and_customer_Map.get(customerId);
customer.setPartnerContactedByEmail(true);
customer.setCustomerHasBeenContactedAtLeastBySomethingOnce(true);
customerID_and_customer_Map.put(customerId, customer);
}
}
//and lot more for loops for other ways, like fax, LinkedIn, pager, advertisement, etc etc etc

到今天为止,这个方法已经有一页长了!

我想重构这个 block ,让代码更干净,然后想到了这个。

    private static void magicMethod(Map<Integer, Customer> map, List<Integer> customersContactedByXXX) {
for (final Integer customerId : customersContactedByXXX) {
if (map.containsKey(customerId)) {
final Customer customer = map.get(customerId);
// customer.setPartnerContactedByXXX(true); <---- How to do this part?
customer.setCustomerHasBeenContactedAtLeastBySomethingOnce(true);
map.put(customerId, customer);
}
}
}

但是,for 循环中的 setter 是有问题的部分。

如何最有效地做到这一点?或者也许有一种方法可以将 setter 作为方法的参数传递?

我明白首先应该有更好的方法来完成整个事情。我的目标实际上是询问如何将所有这些 for 循环重构为一个 for 循环。请不要重构整个事情。

非常感谢您的帮助。

最佳答案

在 Java 8 中,也许是这样的?

magicMethod(getCustomerIDandCustomerContactedByXXXMap(),  getListOfCustomersContactedByTwitter(), MyClass::contactedCustomerByTwitter);

private static void magicMethod(Map<Integer, Customer> map, List<Integer> customersContactedByXXX, Consumer<Customer> contactedConsumer) {
for (final Integer customerId : customersContactedByXXX) {
if (map.containsKey(customerId)) {
final Customer customer = map.get(customerId);
contactedConsumer.accept(customer);
customer.setCustomerHasBeenContactedAtLeastBySomethingOnce(true);
map.put(customerId, customer);
}
}
}

private static void contactedCustomerByTwitter(Customer customer) {
customer.setPartnerContactedByTwitter(true);
}

关于java - 如何将 setter 方法传递给 Java 方法的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64747550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com