• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code sample for evil setter/getter  RSS feed

Zee Ho
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html" 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 ]
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the Drawer do? You can look in JDK how drawing is implemented.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!