• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

simple design decision when painting

 
Ranch Hand
Posts: 383
12
  • 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:

 
Sheriff
Posts: 13510
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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: 383
12
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!