mercredi 3 avril 2019

What design pattern should I use to avoid calling child method from base class?

I am working on my college assignment where we are asked to make a mini game sort of harvest moon like. I made a class of Game where it contains map as attribute. Map is a 2D array of pointer to class Cell. Cell is further divided into Grassland, Barn, Coop. Each has their unique method such as only grassland has the hasGrass attribute. If I have a player who would interact with the map. How do I access methods related to hasGrass while it only existed on certain derived class meanwhile map is matrix of pointer to the base class. Is there a way to elegantly do this?

I have read about making specific interfaces and let class inherited interfaces which are only relevant to certain class. I have also read about dynamic casting but it is considered as indication of bad design. Is there any design pattern to overcome this problem?

Aucun commentaire:

Enregistrer un commentaire