English 中文(简体)
标 题
原标题:Calendar Null Object
public class Test {

    //required fields
    private String str1;
    private String str2;

    //optional
    private Calendar cal;

    public Test(String a, String b){
        str1 = a;
        str2 = b;
    }

    //getter for str1 and str2

    public void setCal(Calendar cal){
        this.cal = cal;
    }

    public Calendar getCal() {
        return cal;
    }
}

如果日历标的是任择性的,我就有一个这样的类别。 如果不要求设立Cal(Cal())办法,则没有任何选择,而不是退回。 如果被送回,我就不得不在每一个地方进行无制检查。 是否有任何等级可延长日历,而没有做像“纽尔反对模式”这样的任何事情。

什么是最佳选择?

问题回答

would something like this fix your problem ?

public Calendar getCal() {
    if (cal != null)
        return cal;
    return Calendar.getInstance(); // to be returned when cal is null
}

回归无效和无效检查最好在这里,其他明智之处是,你可以做些什么。

Initialize the Calendar to today, if that fits your scenario.

只是一个例外,要求制定日历。

仅增加财产违约价值:

private Calendar cal = Calendar.getInstance();

If setCal is not called and so if cal is not set, why do you want to send other thing than null ? null is the best thing to return in this case. Null is also a value, which give an indication to the developper. And, because developpers don t know that your getCal can t return null, they will check for null even if you never return null (I assume that for getter and setter, developpers don t read javadoc).

也许你可以发出新的违约日历。 但缺省值是目前的日期,因此我认为它不是一个好主意,因为它并不代表日历尚未确定的事实。

Or you can throw an exception... but do not abuse of Exception. Exception are designed to defined error or bad behavior, not to simplify functionnal checks. However, to protect the setCal method of null value, here you can throw an IllegalArgumentException if user set null value.

如果你们反对对你们的法典作一劳永逸的检查,那么你就可以执行这样的措施:

    public boolean hasCal(){
        if(this.cal == null){
            return false;
        }
        return true;
    }

这将使你的代码更加可读,但即使有<条码>,你仍须有<条码>(>)。 测试物体无一,返回<代码>null<>/code>似乎是本案的最佳选择。

你的法典内容不明确,根据具体情况,回归无效可能有利。 但总的来说,当你再次试图说“我没有信息回来”时,退却显然表明了这一点。 此外,使其他人更容易看到这一点。

is there any option rather than returning null.

事实上,还有其他许多办法,取决于具体情况。 另一个例子是Option patterns。 其想法依靠的是总结一种选择价值;要么没有,要么是原始数据。 这样,你就可以恢复有意义的价值(在Haskell和在一定程度上在Schala大量使用)。 尽管应当谨慎地把显示空洞的资本退回,除非仔细记录并商定为公约(例如与你的团队合作)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签