Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OO game design question

I am programming a simple game in Java but I am trying to do it 'right' with a nice clean design and no hacks.

I have two classes GamePanel that receives clicks and keypresses and Model which contains all the Entities and is responsible for their updates. The Model needs to know where the user's mouse is but I can't decide on the 'right' way to do it.

Should the Model and every Entity within keep a reference to the GamePanel or just cache the last known mouse position and receive updates from the GamePanel regularly. With the first option when the model is created it will need to be given a reference to the GamePanel and with the second the last mouse position will be sent as a parameter to the World.update() method.

Neither of these solutions seem elegant so I was wondering if there is a 'right' way to do this that I have missed.

Thanks, Ben.

like image 389
Ben Page Avatar asked Jul 17 '09 20:07

Ben Page


People also ask

What is object oriented design question?

The object-oriented design (OOD) question is centered around writing out a program to model a real-world system. For example, one OOD question I've been asked was to design a restaurant. The solution would involve specifying the important classes and methods for managing a restaurant.

How do you prepare object oriented design questions?

Preparation Before the InterviewsYou should have a good command of one object-oriented programming language such as Java/C++/Python etc. Have some experience in it and learn how the OOPs concepts work in these languages. 2. Understand the various object-oriented design principles such as SOLID/DRY principles etc.

How do you approach an object oriented design interview?

To simplify things, you can take the following approach for any OOD question you encounter: Clarify the requirements: Make sure you understand the expectations of the interviewer. Ask clarifying questions if at all necessary — the interviewer will not mind, and will likely appreciate it.


1 Answers

In my opinion, it would depend on how your classes are interacting. Is a change in the mouse position triggering the entities in the Model class? or Is the Model class independent of the GamePanel and only works on the current values of Mouse position?

If the later and in that case I agree with what jeff has said before me. Pass a handle on the GamePanel to the Model class when creating it and let every entity use the handle to access the mouse position whenever needed. That way the updated mouse position is always used.

If the former I would suggest use Observers to let the Model class know when the mouse position value has changed. Then Model class could use the same design (i.e let the Model class have an handle on the GamePanel class always) and access the current values in the GamePanel.

To sum up my answer to your question, I think it is only logical and conforming to OO concepts to let the GamePanel hold the values of the mouse position and let other classes access that information using a GamePanel instance.

Thanks, Rohan.

like image 70
Rohan Grover Avatar answered Oct 17 '22 14:10

Rohan Grover