This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with object accessing  RSS feed

 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently programming a 'Bookmarks' function for an audio player.
A Bookmark object holds current data of the player like current song, elapsed time of the song etc for resuming later.

I got it almost working as it should, but here's the problem:
I need an instance of the player for accessing the current values, so I've put it in the constructor of my Bookmark class.
But - the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player (see comments in code).

How can I solve this ?

Things I thought about but not sure if / how it works:

- make player instance null after I recieved the values (seems hackish)
- make an overloaded constructor somehow which doesn't take a player instance
. other ways to (temporarily) access the player object

Thanks.



 
Paul Clapham
Sheriff
Posts: 22491
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Schroeder wrote:But - the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player (see comments in code).


I don't understand why you make this requirement. And the comments in the code didn't enlighten me in any way. So could you explain in more detail why you say that? I suspect you are misunderstanding something about object-oriented design, because it's normal and extremely common to pass object references around the way you did in that code.
 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Bookmark class should not have a getBookmark() method because it already is a Bookmark. You will need getters for the fields (except for player). You should probably not have setters for the fields. It is possible you might want a play(AudioPlayer player) method to return to a bookmark. Other than that it's a bit hard to tell without the API for AudioPlayer.
 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Dennis Schroeder wrote:But - the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player (see comments in code).


I don't understand why you make this requirement. And the comments in the code didn't enlighten me in any way. So could you explain in more detail why you say that?



Thanks for the reply.

Sorry, yes, I should have explained in more detail.
This is an audio player for a cellphone (pre-smartphone area), the bookmarks get stored in the permanent storage of the player.

So when I say "the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player. " I'm mainly concerned with efficient storage usage.
To resume the player from a bookmark it's sufficient to load the previously recieved player values (like current track etc.) back to the player, so it would be a waste of storage space (and possibly a performance decrease) to save the player instance with it also.

I hope it's more clear now.
 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Your Bookmark class should not have a getBookmark() method because it already is a Bookmark.


Sorry, should have mentioned this is simplified code from another class, not in the Bookmark class.
As said, it basically works already currently to get the bookmarks, except that the Bookmark object also contains the player which I'd like to avoid.


You will need getters for the fields (except for player). You should probably not have setters for the fields. It is possible you might want a play(AudioPlayer player) method to return to a bookmark. Other than that it's a bit hard to tell without the API for AudioPlayer.


Ah yes, sounds like I can solve it with getters for the fields, leaving the player out, I didn't think about this.
Thanks.

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when I say "the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player. " I'm mainly concerned with efficient storage usage.
To resume the player from a bookmark it's sufficient to load the previously recieved player values (like current track etc.) back to the player, so it would be a waste of storage space (and possibly a performance decrease) to save the player instance with it also.

I don't think this is correct. If you need the values of a Player object, storing its reference is the way to do it.

If you're dead set on doing it your way, you could always to this:

But ideally, I'd do this:
 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
So when I say "the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player. " I'm mainly concerned with efficient storage usage.
To resume the player from a bookmark it's sufficient to load the previously recieved player values (like current track etc.) back to the player, so it would be a waste of storage space (and possibly a performance decrease) to save the player instance with it also.

I don't think this is correct. If you need the values of a Player object, storing its reference is the way to do it.


Could of course be that I'm thinking wrongly here but I'll try to clarify:

Scenario:
AudioPlayer is running on the cellphone, playing a track from a playlist.
Before I quit the player I save a bookmark. Bookmarks contains current track, current playlist etc.

I quit the player, restart it, a new (default) player instance is created.
To restore the player in its previous state (to resume playback where it was when saving the bookmark) I'll just need to load the values saved in the bookmark like current track etc.
I don't need the saved player instance for it. at least I wouldn't think so, couldn't try in practice yet.

This is what I currently have, running the debugger:



So bookmark is created fine with the desired values, only that I think I actually don't need the PlayerCanvas instance (the player itself).

Anyway, your code suggestion seems to be the ticket (if I should be right ), will try how it goes and report back.
Thanks.

edit:
I just thought about this, maybe this comparison is totally off..
To store a bookmark say in a web browser, you just store certain data (like page URL), not the whole browser instance.
Uhm..does this make any sense
edit2:
After reading the post below, I notice I missed the difference between object copies and references.




 
Paul Clapham
Sheriff
Posts: 22491
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Schroeder wrote:So when I say "the player instance itself shouldn't be contained in the created Bookmarks object, only the actual values from the player. " I'm mainly concerned with efficient storage usage.
To resume the player from a bookmark it's sufficient to load the previously recieved player values (like current track etc.) back to the player, so it would be a waste of storage space (and possibly a performance decrease) to save the player instance with it also.


I don't know... perhaps you were assuming that in your proposed code a copy was made of the player instance, and so then you would have two copies of that instance? That isn't how it works. Java code never has direct access to instances of any object. What it does have is access to references to those objects, and it's the references which are being copied. A reference probably requires 4 bytes or maybe 8 bytes so it's pointless to worry about two or three more of those things. Even in your low-memory environment it's pointless.

What you've done to try to avoid that assumed memory waste is actually more wasteful than just keeping a reference to the Player object, because now you have several unnecessary references to various parts of the Player, hence increasing the memory usage. However as I said it doesn't hurt to keep a few references around, so a few more isn't that harmful either.

What you've done here is called "premature optimization" and it's something that you shouldn't be doing. Quite often people try to optimize the wrong thing, or they optimize based on inadequate information, which is what you're doing. Optimization should be done on a global basis, for example if you have a problem with using too much memory then you should find out what exactly is filling up your memory and deal with those things once you know what they are.
 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reminding me that it's about references, I actually know but being new to Java (and quite new to coding in general) I tend to forget about it.
So yes, in this light, I see your point, thanks.


 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
If you're dead set on doing it your way, you could always to this:



I just tried it now and that's what I was looking for.
So with this code I now get a bookmark that looks like this, as I wanted.



Actually quite simple solution but I somehow didn't think of doing it this way, guess I got confused.
If this way of doing it works in context of the whole project remains to be seen though when I get further.
Anyways, original question is solved, big thanks again.

Can / should I somehow mark this thread as resolved ?
edit: I just noticed the 'Resolved' button at the top and clicked it.


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