Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I am currently wrestling around with some GUI code where I have a table whose rows correspond to some object, let's say a Person object. When the table initializes, it fills the table with the Person objects that it knows about - say Bob, Andy, and Tom. The rest of the rows item's (row.getItem()) are null. In this particular GUI framework, one cannot highlight/select rows who's getItem() returns null - but in my particular use case I want to allow this functionality.

Therefore, upon initializing the table, I want to set the rows after Bob, Andy, and Tom to a Person object that represents a empty/null Person (not initialized per se), hence the null object pattern coming into play. The patterns combine because the Person class uses the fluent builder pattern for constructing Person instances, so I thought about combining the two like so:

for (rows in table) {
    if (row.getItem() == null) {
        row.setItem( Person.PersonBuilder().nullObject().build() );
    }
}

I am looking for feedback/advice on the merits of this choice, and am welcome to alternatives if this approach is flawed.

share|improve this question
    
lol this is not a duplicate, but might highly inform your choice: How should blank/empty value objects be instantiated/structured –  Rob Y Jan 8 at 1:11
    
shouldn't Person.PersonBuilder().build() do this? Since you didn't set anything it should return the nullobject –  L. Möller Jan 8 at 7:53

1 Answer 1

With your approach one of the problems will be to check if a row contains a null object. I'd go with a constant object such as Person.NullObject (where it's a static value), with that one advantage is you could always check if the row has a value by checking row.getItem == Person.NullObject but if this approach is used you have to make sure the fields of the Person.NullObject is not going to be modified by in-place editing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.