This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

simple design decision when painting  RSS feed

 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RE: the oracle tutorial here,

They discuss a 'red square' object (added to a JPanel) painting itself by accepting a graphics object, making the code cleaner - and I get that.

I just find myself wondering why I don't also have the red square class deal with the 'move' logic too? Personally I would've thought that the square would deal with its own movement, but can see the reasoning for a panel to move an object around. Is it just a design decision, or is there a good reason for this particular set up that i haven't quite spotted? Apologies if this is a silly question, pretty new to all this swing stuff

Thanks,

Nick

full code:

 
Junilu Lacar
Sheriff
Posts: 10948
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you actually have a point in calling out that design because the code looks like it violates the Tell, Don't Ask Principle of object-oriented design. It's really more interested in getting values from the red square and making calculations. Flipping that around would push the calculations back into the square, which keeps the information and the calculations closer together within the same class. That's what encapsulation is about. The current design is arguably less well-encapsulated.
 
nick woodward
Ranch Hand
Posts: 372
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like i said i'm no expert, so I'll pretend that's what I meant..... but eitherway it didn't quite seem right - sort of like a bad attempt to loosen the coupling between the two - which it isn't doing well anyway.

anyway, enjoying reading about tell don't ask (here, if anyone is interested).

thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!