0

I consider JSF have a namespace pollution in which I have to expose private members to the world that I shouldn't do.

Here's an example:

Suppose I have a form with From data and To date input forms, which represented in the backing beans as:

private Date fromDate;
private Date toDate;
// then getters and setters only so JSF XHTML page can see the fields and work with 'em

Then I have this private method, that is being called on post construct:

private List<? extends StatisticsModel> _getDataModel() {
    List<? extends StatisticsModel> dataModel = null;
    if (isSingleDate()) {
        dataModel = getDataModel(getFromDate(), getFromDate(),
                getSelected());
    } else {
        dataModel = getDataModel(getFromDate(), getToDate(),
                getSelected());
    }
    return dataModel;
}

which calls this abstract method that every subclass have to implement:

protected abstract List<? extends StatisticsModel> getDataModel(Date from, Date to, String[] selected);

If you look carefully, you will find that the abstract method from date and to date parameters are too critical and differ from the private fields.

But because of JSF, I have to make the private fields of date, to be public, hence the developer -who will work on subclasses - will be confuse ed between the dates sent to the method as args and the dates inherited as public properties from the super class.

For such case, I did a workaround by appending an under score on the front the private fields names (C trick), but I think this is not good enough.

EDIT The problem is, In ideal world, the fields fromDate and toDate should kept private to the superclass, and he only knows about then, then he would convert then and pass them down to the subclasses (as done in _getDataModel method). But in JSF, this date fields should have Getters and Setters (to be accessible from the XHTML pages) and hence will be accessible too to the subclasses of this Controller, which lead to confusion when working on the subclasses, because the subclass will able to see 2 from date vars (1 field + 1 arg), and 2 to date vars as well. (which each pair might have different values). I hope I am clear now!

Do you think this is a real problem? And How can we solve it?

Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
  • I don't really understand the problem here. You can mark the fields as `protected` if that's what you need. Or even better: provide the relevant code that's being executed that also needs the improvement. – Luiggi Mendoza Jul 01 '15 at 21:00
  • Even protected still be visible by subclasses. – Muhammad Hewedy Jul 01 '15 at 21:01
  • Then what's your problem with the current design? Your variables can be `private` and then access to them using the getter/setter. If you want to disallow the access to the fields to the subclasses then don't provide any getter for them. But you also state that the user may/have to fill these values from a ``. And your question is how to fulfill all these requirements, right? – Luiggi Mendoza Jul 01 '15 at 21:03
  • @LuiggiMendoza, I put some edit and repeated myself so it could be clear – Muhammad Hewedy Jul 01 '15 at 21:06
  • @LuiggiMendoza, Yep this is what I want try to say! – Muhammad Hewedy Jul 01 '15 at 21:07
  • 1
    Well. Your problem is completely unrelated to JSF. Instead, I may use a different class where I will store these `Date` values and inject it to the relevant managed bean it needs this data. – Luiggi Mendoza Jul 01 '15 at 21:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82134/discussion-between-muhammad-hewedy-and-luiggi-mendoza). – Muhammad Hewedy Jul 01 '15 at 21:11

0 Answers0