Though getter/setter methods are commonplace in Java, they are not particularly object oriented (OO). In fact, they can damage your code's maintainability. Moreover, the presence of numerous getter and setter methods is a red flag that the program isn't necessarily well designed from an OO perspective.
This article explains why you shouldn't use getters and setters (and when you can use them) and suggests a design methodology that will help you break out of the getter/setter mentality.
Before I launch into another design-related column (with a provocative title, no less), I want to clarify a few things.
I was flabbergasted by some reader comments that resulted from last month's column, "Why extends Is Evil" (see Talkback on the article's last page). Some people believed I argued that object orientation is bad simply because extends has problems, as if the two concepts are equivalent. That's certainly not what I thought I said, so let me clarify some meta-issues.
This column and last month's article are about design. Design, by nature, is a series of trade-offs. Every choice has a good and bad side, and you make your choice in the context of overall criteria defined by necessity. Good and bad are not absolutes, however. A good decision in one context might be bad in another.
If you don't understand both sides of an issue, you cannot make an intelligent choice; in fact, if you don't understand all the ramifications of your actions, you're not designing at all. You're stumbling in the dark. It's not an accident that every chapter in the Gang of Four's Design Patterns book includes a "Consequences" section that describes when and why using a pattern is inappropriate.
Stating that some language feature or common programming idiom (like accessors) has problems is not the same thing as saying you should never use them under any circumstances. And just because a feature or idiom is commonly used does not mean you should use it either. Uninformed programmers write many programs and simply being employed by Sun Microsystems or Microsoft does not magically improve someone's programming or design abilities. The Java packages contain a lot of great code. But there are also parts of that code I'm sure the authors are embarrassed to admit they wrote.
By the same token, marketing or political incentives often push design idioms. Sometimes programmers make bad decisions, but companies want to promote what the technology can do, so they de-emphasize that the way in which you do it is less than ideal. They make the best of a bad situation. Consequently, you act irresponsibly when you adopt any programming practice simply because "that's the way you're supposed to do things." Many failed Enterprise JavaBeans (EJB) projects prove this principle. EJB-based technology is great technology when used appropriately, but can literally bring down a company if used inappropriately.
| Subject |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
( 1 2 all )
|
|
|
|
|
|
|
|
|
Very good articleBy Anonymous on August 20, 2010, 11:46 amVery good article, it would be nice to show us a concrete code example of this design.
Reply | Read entire comment
I was not mislead...By Anonymous on August 4, 2010, 11:08 am... but I see your point. I made the same point in a separate comment to this article.
Reply | Read entire comment
Setters are even more dangerous than getters. By Anonymous on August 4, 2010, 11:06 amWhen you are able to set a variable, you may need to do a lot of work to ensure that the object's variables remain in a consistent state. For instance, let's suppose...
Reply | Read entire comment
That what accessors do How well they do it is up to you...By Anonymous on August 3, 2010, 11:18 amThe way to tell the object what to do is via getters and setters ( well setters.) Any object which models the world ( or a part thereof ) will need to take input....
Reply | Read entire comment
You got UI code in by data object By Anonymous on August 3, 2010, 10:48 amAgreed this is a kind of wishes are fishes argument. Its not bound to the UI code its bound the the interface of the UI code. Its better but still an issue. Now...
Reply | Read entire comment
View all comments