I was taught that an object should know how to do everything with itself. So I've built an application trying to keep fields private and have a lot of methods like these:
- Edit() (shows a dialog box to edit it)
Now I want to save the data to another file format whose structure doesn't closely correspond to my object structure. It would feel easy if all the data was public. Then a single method could collect all the various bits of data from the objects and reorganize it to suit the file format. But this means exposing private fields (through getters). On the other hand, that's what an API would provide and surely doing this job through an API would be quite clean.
To sum up, which is better in what cases?
- Fields are private. Objects have lots of methods to deal with many different uses.
- Fields have public getters and setters which maintain correct internal state. New functionality that involves many objects can be encapsulated in its own class and doesn't require any modification of the existing code.