简体   繁体   English

从对象关系验证空参数的更好方法是什么

[英]What's the better way to verify null parameters from objects relationships

well i've a good question about verification of relationships. 好吧,我有一个关于关系验证的好问题。

Follows below the three classes to suppose the question: 遵循以下三个类来假设问题:

public class Son {

    private Integer idSon;
    private String name;
    private Father father;
}

public class Father {

    private Integer idFather;
    private String name;
    private GrandFather grandFather;
}

public class GrandFather {

    private Integer idGrandFather;
    private String name;
}

Following these principies, let's suppose that we've to do relationships verifications, instead every time we do something like this "in differents parts of our code": 遵循这些原则,让我们假设我们必须进行关系验证,而不是每次“在代码的不同部分”执行类似的操作时:

Son son = new SonBusiness().getById(idSon); //Get a Son from database
if(son.getFather == null){
    throw new Throwable("Father's Son does not exist");
}

wouldn't be better create some method like: 创建如下方法会更好:

public void verifySonsDatas(Son son, Bollean verifyFather, Boolean verifyGrandFather) throws Throwable{
    if(son == null){
        throw new Throwable("Son does not exist");
    }
    if(verifyFather){
        if(son.getFather == null){
            throw new Throwable("Father's Son does not exist");
        }
    }

and so on for any son's ralationships.. 对任何儿子的亲属关系等等。

Somebody else agree that with this approach we avoid many redundant codes and we can concentrate the verification inside an unique method...? 有人同意使用这种方法可以避免许多冗余代码,并且可以将验证集中在一种独特的方法中……?

If we think about 10 relations, imagine writting if(object == null) it does not appear be a good thing for me. 如果我们考虑10个关系,想象写if(object == null)对我来说不是一件好事。

Thanks! 谢谢!

I would rather suggest to find the corresponding part of your code, where you have to check this only once or twice. 我宁愿建议您找到代码的相应部分,在此只需检查一次或两次。 Why should you call this check more than twice? 为什么您应该多次致电此支票? Shouldnt it be enough to check this if 1. you save a son or 2. load the son from database? 如果1.您保存了一个儿子或2.从数据库中加载了儿子,是否足以检查这一点?

I think if you have to place the above checks at so many places, with throwing some Exception with predefined messages then this approach at least provide a reason (ie all the exception will be managed at a central place) and thus it is valid. 我认为,如果您必须将上述检查放在很多地方,并在预定义消息中抛出一些Exception ,则此方法至少可以提供一个原因(即,所有异常都将在中央位置进行管理),因此它是有效的。
I will suggest you should have different function each to check individually Son , Father and GrandFather and so on and frame a method with those boolean conditions. 我建议您分别使用不同的功能分别检查SonFatherGrandFather等,并使用这些boolean条件构造一个方法。

But if this exceptions, are just thrown to break without any meanings then you should consider placing the obj == null checks wher you need to check them. 但是,如果将此异常抛出而没有任何意义,那么您应该考虑在需要检查它们时放置obj == null检查。

You can also read Avoiding != null statements for information. 您也可以阅读避免!= null语句以获取信息。

How about implementing some sort of interface to specify the family's inheritance (heh heh). 如何实现某种接口来指定家族的继承关系(呵呵)。

interface IChild {
    abstract int GetParent();
}

In Son: 在儿子中:

Son implements IChild 

@override 
int GetParent() { return this.father.idFather; }

In Father: 在父亲中:

Father implements IChild 

@override 
int GetParent() { return this.grandFather.idGrandFather; }

If null is part of the contract then I suppose yes creating a helper method is better than repeating the same code all over the place. 如果null是合同的一部分,那么我想是的,创建一个助手方法比在各处重复相同的代码更好。 However the much better alternative is to just avoid this in some way. 但是,更好的替代方法是以某种方式避免这种情况。 For example consider the following: 例如,考虑以下内容:

public Son(int idSon, String name, Father father) {
    if (father == null) {
        this.father = new Father(0, "Unknown", null);
    }
}

Or if nulls are illegal then make the constructor the delegate for the exception: 或者,如果null是非法的,则使构造函数成为异常的委托:

public Son(int idSon, String name, Father father) {
    if (father == null) {
        throw new IllegalArgumentException(
            "Children must have parents to be born."
        );
    }
}

If an object is invalid within its own specifications it makes more sense not to construct it at all. 如果一个对象在其自己的规范内无效,则完全不构造它是更有意义的。

Finally, you are checking for a null and throwing an exception: 最后,您要检查是否为null并引发异常:

if(son.getFather == null){
    throw new Throwable("Father's Son does not exist");
}

So why bother? 那为什么要麻烦呢? If nulls are illegal, why not get a NullPointerException? 如果null是非法的,为什么不获取NullPointerException?

Indeed exists many ways to implement it. 确实存在许多实现它的方法。 But in my case i can't create a rule like "can't exist any relations == null" or any other implementation because in differents actions, i've differents approaches, sometimes i've to verify the son's father for example. 但就我而言,我无法创建诸如“不能存在任何关系== null”之类的规则或任何其他实现,因为在不同的动作中,我有不同的方法,例如,有时我必须验证儿子的父亲。 Another i've to verify the father's and grandfather's son, My method need top be flexible and generic. 另一个我要验证父亲和祖父的儿子,我的方法需要灵活,通用。 I've analised what kjhf said me, implementing it by another way, without be mandatory the parameters of the implementations... Some cases i've three relationships, another i've five, another only one. 我已经分析了kjhf所说的内容,以另一种方式实现了该实现,而不必强制实现这些参数……有些情况下,我有3个关系,另一个则有5个关系,另一个只有一个。 Anyway, i'm trying to discover some way to do it only one time and all of the objects use the same method. 无论如何,我试图发现某种方式只能一次完成,并且所有对象都使用相同的方法。 nowadays i'm creating a new method inside each business class that references one entity. 如今,我正在每个业务类中创建一个引用一个实体的新方法。 Then if i've 10 entities, logically i've 10 methods of verification. 然后,如果我有10个实体,则从逻辑上讲,我有10个验证方法。 If someone else have some ideia, please, i'm waiting. 如果有人有任何想法,请等待。

Thanks everybody. 谢谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将元素从Stream添加到现有List的更好方法是什么? - What's the better way to add elements from a Stream to an existing List? 比较两个对象的类的更好方法是什么 - What is better way to compare class of two objects 编写此函数的更好方法是什么? - What's the better way to write this function? 干燥 Java 代码的最佳方法是什么? 为参数创建具有不同对象的私有方法? - What's the best way to DRY Java code ? Creating private method with different Objects for parameters? 检查参数的更好方法? - Better way to check Parameters? 为互斥请求参数设计API的更好方法是什么? - What is a better way to design an API for mutually exclusive request parameters? GAE JDO-建立这些关系的正确方法是什么? - GAE JDO - What's the correct way to model these relationships? 对我来说,做这些空检查并改善此代码的性能的更好方法是什么? - What is a better way for me to do these null checks and improve the performance of this code 持久化 Java 对象的最简单方法是什么? - What's the easiest way to persist java objects? 对列表对象进行分组的有效方法是什么? - What's the efficient way to group this list objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM