samedi 3 février 2018

Lifecycle design

I'm writing a card game (there is a related topic I opened a couple of days ago unique_ptr vs shared_ptr - design). I think that I've got a problem with managing lifecycle. As you can see in the mentioned topic I wanted to use unique_ptr to manage Card's lifecycle. This approach comes from observing a real world and trying to mimic it: If I have a deck then there is only one instance of a card in the real world and if I want to give a card to a player then I have to take it from the deck. Not copy a card. Not give a "reference".

But maybe it's not so good idea. Maybe a better solution would be: Let's assume that the Deck is the owner of cards. If you want a card you can get only a reference/pointer. But it the Deck who manages cards' lifecycle.

I thought that using a unique_ptr would help me to avoid some mistakes too. Like - if you pass a unique_ptr then you are sure that a card is not held by two players the same time.

I appreciate any comments:)

P.S. I've read this related topic Why should I use a pointer rather than the object itself? but I think my question is more about design.

Aucun commentaire:

Enregistrer un commentaire