I don't know if to have domain models with properties that not always are set, it's a some kind of code smell or design error. I put an example:

I have an app that lets read internet magazines using Rss. For domain models, I use cases of use. Briefly I have:

  • There is a magazines library.
  • You can consult the profile of the magazine.
  • You can explore news magazine.

For that, I extracted this entities: library, magazine, magazine profile, magazine news.

My classes are:

class Magazine
    private int id; // BD id row
    private MagazineProfile magazineProfile;
    private List<MagazineNews> news;

class MagazineProfile
    private String title;
    private String webUrl;

class MagazineNews
    private String title;
    private String body;
    private Date pubDate;

In some points of my app, I will be exploring only the magazines using their magazine profile (displaying a list of all magazines), in other points I will be exploring only the news and in other I will be exploring news and the magazine profile at the same time.

Depending on the case, sometimes, my object magazine will have the properties magazineProfile news set or empty.

So my question is, if this design is an error approach to have properties that will be established according to the window in which the user is browsing. Should I work only with the objects that interest me, in my case, magazineProfile and news?


I don't believe that your design is an issue but the implementation can give you some problems. Use the Null Object Pattern to fill in the fields that otherwise would be missing.

In your case, a "missing" MagazineProfile would be marked by a singleton that returned an empty title, webUrl, etc.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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