简体   繁体   English

Java中类的正确结构是什么?

[英]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. 我已经用Java编写了大约一年半的代码,并做了一些我认为效果很好的小型游戏和程序。 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 在之前的迷你RPG游戏中

  • Main Class (Main loop + Main method) 主类(主循环+主方法)
  • Player Class (containing player position and player stats) 玩家等级(包含玩家位置和玩家统计信息)
  • Engine Class (containing map and collision detection between player and map 引擎类(包含地图以及玩家与地图之间的碰撞检测
  • Map Class (containing map data) 地图类(包含地图数据)

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. 因此,我最终在Player和Engine中都包含一些静态变量和方法,以使它们进行通信。

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. 我的总体目标是了解如何在之前的游戏中构造类,以便在想要进行的更大项目中更好地构造类。

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. (有几种可能的例外,但是根据经验,这是一个有用的例外。)OOP的基本思想是,每个人都可以参考想要向其发送消息的对象。 So if a Player needs to tell the Engine something, it should have a reference to whichever Engine instance it belongs to. 因此,如果Player需要告诉Engine一些信息,则应该引用它所属的Engine实例。 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. 或者,您可以重新设计架构,以便仅Engine将消息发送到MapPlayer ,而在没有更多详细设置的情况下很难确定在这种情况下是否合适。

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. 我还建议您看一下观察者模式发布-订阅模式 ,因为这可能是您所需要的。

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 . 一些可以在Wikipedia上找到。

I also take it that you are familiar with OOP? 我还认为您熟悉OOP? Some more general info can be found here on Wikipedia . 一些更一般的信息可以在Wikipedia上找到。

Looking at your specific story, I think a MVC-design would be a nice solution. 查看您的特定故事,我认为MVC设计将是一个不错的解决方案。

MVC meaning Model View Controller. MVC表示模型视图控制器。
Here you have your Model, classes holding different forms of data. 在这里,您拥有Model,这些类保存着不同形式的数据。
Your Controller, controls your Model, contains all the real logic. 您的控制器控制模型,包含所有实际逻辑。
And your View, this is the graphic end of your application. 而您的视图,这就是您应用程序的图形端。

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! 希望有帮助!

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). 我将研究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/ . 这是一个关于如何使用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. 话虽如此,如果您的目的是更好地理解使用Java解决问题的方法,那么我将首先尝试自行模拟这两种方法,而无需使用任何笨重的框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM