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.
2
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.