How would you do this?

Suppose you were making a card game with other people and want to make it as bug free as possible. One consequence of this is making sure that object data cannot be corrupted.

Suppose you have a Game and it holds a Deck. Game obviously modifies Deck and so Deck has methods for that manipulation. However, using the methods incorrectly could corrupt the data. Usually to avoid this, you would encapsulate the object. However, for debugging reasons, you need outside access to Deck’s cards has. But exposing Deck would expose those manipulation methods and allow outsiders to corrupt the data. So, how do you expose Deck’s cards, and allow ONLY the Game to manipulate the Deck?

[quote=“tariqbroadnax,post:1,topic:58573”]

  1. I don’t think that a deck should have public methods that corrupt its state. Every method should either get a part of the (copied) state oder modify the deck in a way that leads to a healthy deck and not a corrupt one.
  2. For debugging - do you mean for testing? Usually you place tests in the same package and make things package-protected, but not public.

[quote=“tariqbroadnax,post:1,topic:58573”]
Could you create getter()s for debugging purposes? That way the cards can be examined but not changed.

  1. The public methods only corrupt the data when they are used outside of their original scope. Whenever the Game uses the methods, Deck is guaranteed to remain healthy. However, when other code uses the those methods, Deck becomes corrupted. At least, for those methods that change Deck’s state.

  2. I just used debugging/testing as an example. There are plenty of cases where some other code might need to know what cards Deck has.

How do you create accessors without allowing the data to be changed?

Only return simple data types, ie. toString() for debugging purposes.

I don’t know if you could really ever prevent corruption of the deck. As long as the user has access to any object referencing the deck, you could just use reflection to access any field you wanted. The closest you could come is returning copies of the cards/deck, but that has its downsides as well (including memory usage).

If you can manage to restrict access to the actual values via private modifiers, getters, setters, and that sort (Though reflection may bypass this), then you could check for legal moves in the Deck.

Perhaps store a pre-modification copy of Deck, and check to make sure only the thing that was supposed to happen, happened. Dunno how you’d make sure that you can’t get at the copy though.

I.E. if you play 1 card that doesn’t do anything beyond subtracting that card from the deck, if anything else changes, you know something’s up, and you can replace the deck with the correct one and, I dunno, I guess you’d first inform the player that an illegal move was made, and if it keeps happening, penalize them more and more, until the other person just wins by forfeit?

Again, there are probably ways around this (there are ALWAYS ways around security, no matter how obscure). I just figured I’d toss in my two cents.