验证来自不同根的对象的方法会重复大多数检查

问题描述

请提供一种使代码更简洁的方法- 为了避免重复类似/相同的动作- 我有一个进行对象验证的方法,但是大多数参数 相似/相同,例如为此使用某种lambda表达式, 但是对象不是来自同一棵树。

我的用例是这样的:

validate.( car );

我在其他地方做

validate.( person );

现在我正在像这样进行验证:

public boolean validate( Object obj ) {
  if ( obj instanceof Car ) {

    Car car = (Car) obj;
    if ( car.getAge() <= 0 ) return false;
    // many other checks which are repeated below

  } else if ( obj instanceof Person ) {

    Person person = (Person) obj;
    if ( person.getAge() <= 0 ) return false;
    // many other check which are repeating those above

  }
  // here I would like to do checks for both objects,but objects are from different roots

return true;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)