0

I hope this a correct stack.

I am developing a nethack-like game, and I would like an advice how to approach the design part. For now I got class like Location, Npc, Item etc. But I ve got a problem how to easily access parts of location.

Lets say I have an object Door inside (not inheritated). If player is inside location it is easy to check whether door are opened. But on the other hand (I got this solution with my previous non-object implementation) I had a script that at 0600 opened all shops. But now I need to iterate thru all lcoations, check whethere are doors inside, and open them if location is a shop. Is it really optimized way to do it?

I could also do a globals (like singelton) with door states and fastly run thru those - but it would be hardly an OOP.

What are the possibilites here?

If this problem is somewhere covered please share link with me and that would surely be enough :)

Thanks!

Zaqqen

1 Answers1

0

In my opinion, there is a difference between theoretical and practical OOP. If you want to learn the basis or make a thesis on OOP, theoretical could be fine. In most other cases you would prefer a practical one.

Why and how is it related to your question?

When I was a young developper, I had so much pain to rationalize my code. Shall I code the selling method in the class Product, Store or Consumer?

Then I discovered SOA, and I set my selling method in the class SaleHandler.

This is what you call a singleton. I prefer to implement it as a service with a framework helping me to make some dynamic dependency injection. From here, I had my data classes (Product, Store, ...) and my service classes (SaleHandler) to be fast. All my logic was coded in these services. This was not pure theoretical OOP but it helped me so much to handle the increasing complexity of growing applications.

I don't know how you can use this in your case, but I can give you some slope:

  • A service class DoorsRegistry containing all your doors (pattern registry).
  • A service class DoorOpener handling the opening of the doors.

If you do not use any framework helping you to do that you can implement your services as singletons but be aware that the pattern singleton is certainly an anti-pattern.

Hope this is the kind of answer you are waiting for.

Community
  • 1
  • 1
Gnucki
  • 5,043
  • 2
  • 29
  • 44