File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Code sample for evil setter/getter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Code sample for evil setter/getter" Watch "Code sample for evil setter/getter" New topic

Code sample for evil setter/getter

Zee Ho
Ranch Hand

Joined: Jul 20, 2004
Posts: 128
First I feel sorry to start a new post for this topic, as I want to ask the rancher to comment my code and offer me any better design,

I have read the article <<a href="" target="_blank" rel="nofollow">Why getter and setter methods are evil</a>>
and the post in javaranch, Most of what the author said is reasonable, I wrote a sample program to follow the idea of Mr.evil. please rectify me if I misunderstand sth or do it wrong.

Principle 1 :If you don't understand all the ramifications of your actions, you're not designing at all. You're stumbling in the dark
Principle 2 : Do not ask for the information you need to do the work, ask the object that has the information to do the work for you.
Principle 3 : A fundamental precept of OO systems is that an object should not expose any of its implementation details
Principle 4 : One basic principle of OO system is abstraction.

===========Commonly But Bad desing========

In this way, we strongly couple the MainClass with Circle class, and it violates the Princile 2, 3, 4. If the further
maintainance requires to change the float to long to make our circle to be much more bigger, we will get compile
time error. further more, we have to enhance the logic in drawCircleToScreen.

===============Better design=============

The design is much more stronger now, If we modify the Circle class, the changes will not be propagated to MainClass,
however, such design mix the business logic togther with the Circle class. It is no good. Let us introduce an interface
==============Best Design?===========================

In this way, We totally seperate the Circle class and MainClass, It become more easier to apply changes into either of the two class.
for the this approach.
- It does not expose the internal implementation to the invoker.
- Circle do all the things by itself.

But wait, how about the boss want to use the Squre instead of Circle? hm~~, no good, the developer will get headache to search for all the reference of circle to apply the changes,

=========Accomplished Design==================

- The abstraction rule is also applied into this design.

does what I wrote is what the author means? or there is any other good idea to do the design, any reply will be appreciate!!!
[ September 12, 2005: Message edited by: Zee Ho ]

SCJP 1.4<br />SCWCD 1.3<br />SCJD<br />SCBCD<br />IBM Xml Cert in progress
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
What does the Drawer do? You can look in JDK how drawing is implemented.
jQuery in Action, 3rd edition
subject: Code sample for evil setter/getter
It's not a secret anymore!