This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Very basic instance addressing question

 
Bill Parsh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have a "MainWindow" instance that creates a "Scorekeeper" instance and many "Player" instances. If I want the Player instances to be able to address the Scorekeeper instance, the way I do it now is instantiate the Scorekeeper in the MainWindow, then when I create a Player I pass the EDIT: MainWindow /EDIT as a parameter to the Player constructor. Then I address the Scorekeeper from the Player as like "myMainWindow.myScorekeeper"

This works fine but seems a little wonky to me. Any suggestions?
[ November 20, 2008: Message edited by: Bill Parsh ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

Welcome to JavaRanch!

Sounds like you're passing the window to the player, not the scorekeeper; otherwise in player you'd just use "myscorekeeper" or something, right?

But in any case, that's basically the right way to do things: either provide player with what it needs as constructor arguments, and let the player store a reference for its lifetime, or alternatively, make sure all the player methods that need a scorekeeper accept one as an argument, so that the caller has to provide the scorekeeper.

A third alternative -- and one that's strongly discouraged -- is to store things in public static variables, so that the player objects can just say, for example, MainWindow.theScoreKeeper to access that static variable. The problem with this is that your code becomes terribly messy, complex, interconnected and impossible to change after a while.
 
Bill Parsh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Sounds like you're passing the window to the player, not the scorekeeper; otherwise in player you'd just use "myscorekeeper" or something, right?


Right, sorry about that.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
. . . basically the right way to do things


No need to be sorry; EFH said you are basically right, just needing a bit of refinement
 
J. Noah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on the app, another thing you could do is create a singleton for the scorekeeper or mainwindow if that's applicable in this case. That way you'd be able to access it elsewhere while still preserving some of the ideal loose coupling properties.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic