Anthony Robertson
Anthony Robertson

Reputation: 63

What is the correct structure of classes in java?

I have been coding in java for about a year and a half, and have made some small games and programs that I think turned out very well. The problem is that I am mostly self taught and I most of my code and class structure is not organized or "correctly structured". This didn't matter to me for the smaller things I made, but if I were to take on a larger project I would want to structure it correctly and make it organized.

In a previous mini-RPG game I had

My Main class contained an instance of Player and of Engine, and Engine had an instance of Map. The problem is that Player then could't tell the Engine where it was, and the Engine couldn't adjust Player's position and stats when something happened on the Map. So I ended up having some static variables and methods in both Player and Engine to get them to communicate.

I guess my overall question is is this the correct structure of classes, and is it correct to use static methods and variables here? If not, how would you structure these classes, or would there need to be more or less classes?

My overall objective is to understand how to structure classes in this previous game so I can better structure classes in a bigger project I want to take on.

Upvotes: 0

Views: 1558

Answers (4)

Sanjeev
Sanjeev

Reputation: 1575

From what you described there a few possible ways to handle this. One would be to use a messaging system. I would look into Java Messaging Service (JMS). Another would be to make your app event drive. Here is a neat little tutorial on how to do this using spring : https://spring.io/guides/gs/messaging-reactor/. Having said that, if your intent is get a better understanding of problem solving using Java, I would first try and mimic these two approaches on your own, without any bulky frameworks.

Upvotes: 0

biziclop
biziclop

Reputation: 49804

It is a rather broad question, but the general answer is no.

As a rule you shouldn't use static fields to connect instances. (There are a couple of possible exceptions, but as a rule of thumb it's a useful one.) The basic idea of OOP is that everybody has a reference to whoever they want to send messages to. So if a Player needs to tell the Engine something, it should have a reference to whichever Engine instance it belongs to. Or you can redesign your architecture so only Engine sends messages to Map and Player, it's difficult to tell without more detail about your setup whether that would be appropriate in this case.

Another piece of general advice is to try to sit down with a piece of paper, write down the name of all three of your classes and in a separate column write down all the things the system has to do. And then you should try to figure out who's responsible for what. In a good design this decision is simple, if you find yourself shoehorning different things into one class, that's a sign that you should maybe need a more detailed model with more classes.

I would also suggest you take a look at the observer pattern and the publish-subscribe pattern, as it might be what you need.

Upvotes: 3

creulcat
creulcat

Reputation: 286

Try take take a look at some design-patterns.
Which design pattern you want to use depends on what you prefer. Some can be found here on Wikipedia.

I also take it that you are familiar with OOP? Some more general info can be found here on Wikipedia.

Looking at your specific story, I think a MVC-design would be a nice solution.

MVC meaning Model View Controller.
Here you have your Model, classes holding different forms of data.
Your Controller, controls your Model, contains all the real logic.
And your View, this is the graphic end of your application.

Upvotes: 1

Mr. DROP TABLE
Mr. DROP TABLE

Reputation: 332

You'd probably want to put and instance of your player in your engine as well. That way your engine will control everything (the player and the map). Hope that helps!

Upvotes: 0

Related Questions