1
\$\begingroup\$

I'm designing a system to handle the keyboard input for my game. To get it more simple, I have a class that has two methods, KeyUp and KeyDown which tell me which key has been pressed, this is already implemented by the library I'm using, now I want to have a way to send that information to the classes that are handling input (I'm using an MCV pattern, those would be the controllers). I've thought of two ways of doing that, both seem valid to me, and I don't know which is better, so I'm asking which would be the best way, in terms of object-oriented design, and if it has any significant impact, also efficiency.

The first way I thought of is having the class which receives the input hold references to all the controllers, and then call some methods of those controller classes in an event-like fashion when a key event is triggered in this class.

The other method is the other way round, the controllers hold a refference to the input handler class, which in this design has some kind of data structure (I was thinking of an EnumMap) that holds the status of all the relevant game keys, and then the controller classes might query the input handler for a key with some getters (just to encapsulate the data structure).

So, that's it, I'd like some help on how to do this. Any comments on which of both is better, or a new design that might be better than this are more than welcome.

Thanks in advance.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Asking "Which is best" is pretty much asking for opinions/discussion. You're just suffering a little design indecision. Just pick one and go with it. If you find that the strategy you picked is inefficient, try something else. \$\endgroup\$
    – House
    Jul 30, 2013 at 22:46
  • 1
    \$\begingroup\$ Why is efficiency given as a concern and not flexibility, maintainability, etc.? Are you having efficiency problems? Making "the best OOP" solution is also not super valuable, especially as you've yet to determine if OOP is even the right solution for the problem. In general, try to get things working, then reapproach them only if you run into real, identified problems. \$\endgroup\$ Jul 31, 2013 at 7:01
  • \$\begingroup\$ @Sean Well, as a newbie I often find myself asking the wrong questions (surprisingly, to get right answers, but I'm not talking about that right now). So yes, I was more concerned about flexibility, maintainability and making things easy to use and work with than anything else, although I try to make everything as efficient as I can, so I was concerned about efficiency as well. So I mainly want to know how to do things well, and not that much about object oriented (although I value and like this way of designing software, and try to stay with it as much as I can). \$\endgroup\$
    – Setzer22
    Jul 31, 2013 at 20:40

1 Answer 1

1
\$\begingroup\$

Possibly duplicated with this post

Essentially you are asking the differences between polling and interrupt/event-driven. You can Google it and there are a lot of answers out there. They both have advantages and disadvantages and which one to use depends on the scale and architecture of your application.

Here is another discussion you can look at.

\$\endgroup\$
2
  • \$\begingroup\$ This should probably be a comment. It's basically a link only answer. \$\endgroup\$
    – House
    Jul 31, 2013 at 0:15
  • \$\begingroup\$ Sometimes it's just about knowing how your problem is called, so you can go and look for it. You really helped a lot, and by what I've been able to read with some searching I think I'll stay with the even-driven approach. I mark this as answered although Byte56 pointed out that this might have to be a comment, but it's the only answer, and certainly gave me what I was looking for. \$\endgroup\$
    – Setzer22
    Jul 31, 2013 at 20:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .