Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a failure in how OOP is often taught, using examples like rectangle.draw() and dinosaur.show() that make absolutely no sense.</p> <p>You're almost answering your own question when you talk about having a user class that displays itself.</p> <p>"Later, if I need to adjust to show their name+small avatar, I can just update this one method and hey-presto, my app is updated."</p> <p>Think about just that little piece for moment. Now take a look at Stack Overflow and notice all of the places that your username appears. Does it look the same in each case? No, at the top you've just got an envelope next to your username followed by your reputation and badges. In a question thread you've got your avatar followed by your username with your reputation and badges below it. Do you think that there is a user object with methods like getUserNameWithAvatarInFrontOfItAndReputationAndBadgesUnderneath() ? Nah.</p> <p>An object is concerned with the data it represents and methods that act on that data. Your user object will probably have firstName and lastName members, and the necessary getters to retrieve those pieces. It might also have a convenience method like toString() (in Java terms) that would return the user's name in a common format, like the first name followed by a space and then the last name. Aside from that, the user object shouldn't do much else. It is up to the client to decide what it wants to do with the object.</p> <p>Take the example that you've given us with the user object, and then think about how you would do the following if you built a "UI" into it:</p> <ol> <li>Create a CSV export showing all users, ordered by last name. E.g. Lastname, Firstname.</li> <li>Provide both a heavyweight GUI and a Web-based interface to work with the user object.</li> <li>Show an avatar next to the username in one place, but only show the username in another.</li> <li>Provide an RSS list of users.</li> <li>Show the username bold in one place, italicized in another, and as a hyperlink in yet another place.</li> <li>Show the user's middle initial where appropriate.</li> </ol> <p>If you think about these requirements, they all boil down to providing a user object that is only concerned with the data that it should be concerned with. It shouldn't try to be all things to everyone, it should just provide a means to get at user data. It is up to each of the <em>many</em> views you will create to decide how it wants to display the user data.</p> <p>Your idea about updating code in one place to update your views in many places is a good one. This is still possible without mucking with things at a too low of a level. You could certainly create widget-like classes that would encapsulate your various common views of "stuff", and use them throughout your view code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload