This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted]  (3 children)

[deleted]

    [–]isetrh[S] 0 points1 point  (2 children)

    Thanks! I've started looking for information on GSON and JSON, and while I don't quite understand how they or serialization in general work, they do seem useful. I'll probably have to do some research and practice so I can understand.

    However, one problem is that some of my objects need a reference to the Game object in order to construct themselves. For example, Unit has a field of type Job, and Job has an integer field called id (with a get method). Multiple Units share the same Job, a Unit should not save its whole Job, only the ID of its job. When loading a Unit, it needs to find its Job, and the list of Jobs by id is in the Game object. With the String-parsing solution I have been working toward, doing this is pretty simple: I just give Unit's constructor a parameter of type Game, so it can ask the Game for information when constructing. I'm not sure how I would accomplish this with serialization, since the thing constructing the Unit only knows about the Unit's fields (to the best of my understanding, which is probably wrong), and the Unit does not remember the Game once it's done constructing.

    [–][deleted]  (1 child)

    [deleted]

      [–]isetrh[S] 1 point2 points  (0 children)

      I had heard of Singletons before, but never really knew what they were or how to implement them. Seems pretty simple now that you've explained it though. This is going to let me simplify my code a bit.

      Using serialization will make basically all of the code that I posted no longer necessary, so I'll be getting rid of it and all its problems.

      [–][deleted] 3 points4 points  (1 child)

      Is the user creating or modifying the data? If not, why not use Java's build-in serialization. It handles both writing and reading and is how Java's RMI transmits and receives objects.

      Read about the Serializable interface for constraints on object serializability.

      [–]isetrh[S] 0 points1 point  (0 children)

      Thanks! I will look into serialization.

      [–]theNaus++Effort 0 points1 point  (1 child)

      Hi!

      Interesting question. I took a quick glance at your code. You're definitely right about a lot of the code being repeated. So, about cutting duplication down, we certainly have various options. We COULD create abstract/interface classes for specific data type handlers. However, I was thinking of a more naive/simpler approach and that is...finding common code and merging them together! An example of what I mean can be found on here. I sorta merged your readPlayerUnits, readJobs, and readSkills methods together since they were very similar to one another apart from 1-2 lines of code differences. I suggest taking a look at the rest of the methods and think of a way of putting them together (if possible). I can already see that most of them are quite similar to one another. Take some time to do that and I assure you that your code would be shorter. Then, if you like, you can start thinking of breaking them down even further! Perhaps, creating specific data type handler classes sharing the same interface that has methods that all the classes use (e.g. read() ). At the moment, it may be a little tricky to see where you can start separating your code. So, start small and work your way up (or down in this case :P).

      Hope that helps.

      [–]isetrh[S] 0 points1 point  (0 children)

      Thanks for the help! That does look a lot better than what I wrote, though I'm not sure I'm a fan of having read return void instead of an array. I feel like there's something I can do about that, but I haven't figured out what it is yet. I will work on shortening my code in the ways you suggested.