简体   繁体   English

我被教导不要将大多数方法放在通用的“系统”类中,而是将它们放在哪里?

[英]I've been taught not to place most methods in a general “System” class but where do they go instead?

I'm making a game. 我在做游戏。 It seems most natural to put almost all the game logic in the Game class, have it create all the objects and have mostly getters and setter methods in them. 将几乎所有游戏逻辑都放在Game类中,让它创建所有对象,并且其中大多数包含getter和setter方法,这似乎是很自然的。 Is this actually wrong? 这真的错吗? Should I try to force some of the game logic into the other classes like the process of Player taking his turn as a method of Player ? 我是否应该尝试将某些游戏逻辑强加到其他类中,例如Player轮流作为Player方法的过程?

In a typical game, if a player shooting is triggered by a mouse click, how would you go from the click event to updating the screen state to destroying the target object in terms of where the methods would be located and objects calling each other? 在典型的游戏中,如果玩家的射击是通过鼠标单击触发的,那么从方法事件的位置和对象之间的调用角度来看,您将如何从单击事件变为更新屏幕状态再破坏目标对象?

Should I even be getting rid of the Game class and somehow having the other objects all calling each other? 我是否应该摆脱Game类,以某种方式让其他对象互相调用?

How long term is this project? 这个项目多长时间? If you planning a long term project, there is a pretty decent chance that you will eventually want to jettison your hand-coded UI and integrate with one of the big Java game-graphics engine instead. 如果您计划一个长期项目,则很有可能最终要放弃您的手动编码UI并与大型Java游戏图形引擎之一集成。

If this assumption is true, you might want to use the Model View Presenter pattern , to make it easier to port your graphics library, along with the other benifits of MVP (modularity, testability, leaps over tall buildings, etc.) 如果这个假设是正确的,则您可能希望使用Model View Presenter模式 ,以便更轻松地移植图形库以及MVP的其他优点(模块化,可测试性,跨越高层建筑等)。

So, if you go down this path, you would have 因此,如果您走这条路,您将拥有

  • GameModel: The data used in your game. GameModel:游戏中使用的数据。
    • May/May not have Player etc. classes along for the ride. 可能/可能没有播放器等课程。
    • Code to save/load/track players should be in this layer. 保存/加载/跟踪播放器的代码应该在这一层。
  • GamePresenter, which contains all your UI logic GamePresenter,其中包含您的所有UI逻辑
    • It knows about the GameModel and associated classes. 它了解GameModel和关联的类。
    • It may pass events to the GameView, either using event handlers or defining a IGameView interface. 它可以使用事件处理程序或定义IGameView接口将事件传递给GameView。 It may not however, know any details of how the real GameView is implemented. 但是,可能不知道如何实现真正的GameView的任何细节。
  • GameView GameView
    • It knows all about the GamePresenter and GameModel. 它了解有关GamePresenter和GameModel的所有信息。
    • It should only have the code needed to actually display your game and get input back from the user. 它应该只具有实际显示游戏并从用户那里获取输入所需的代码。

The way you describe the problem reveals that you are overloading the class Game with logic that should belong in other objects. 描述问题的方式表明,您正在用应属于其他对象的逻辑重载Game类。

The first signal appears when you say that other objects "have mostly getters and setters". 当您说其他对象“主要具有吸气剂和吸气剂”时,将出现第一个信号。 That means that those objects are just data structures holding state but lacking interesting behavior. 这意味着那些对象仅仅是保持状态但缺乏有趣行为的数据结构。 This indicates that you still have to think deeper on how you distribute responsibilities among objects. 这表明您仍然必须更深入地思考如何在对象之间分配职责。

The second evidence is in the second paragraph of your question. 第二个证据在您的问题的第二段中。 It suggests poor separation between model and GUI. 这表明模型和GUI之间的分隔差。 A good test to verify whether there is adequate separation (or not) is to prove that you could play the Game (at least non-trivial parts of it) from a script, ie, without exercising any GUI code. 验证是否存在足够的分离性的一个很好的测试是证明您可以从脚本中玩Game (至少是其中的重要部分),即无需执行任何GUI代码。 The game model will only be properly designed if it is full functional in "headless" mode. 仅在“无头”模式下具有完整功能时,才可以正确设计游戏模型。

Finally, getting rid of the Game class would be a bad decision because your model would not have any representation of an entity that exists in the "reality" you are trying to model. 最后,摆脱Game类将是一个错误的决定,因为您的模型将不具有您要建模的“现实”中存在的实体的任何表示形式。

I think that the Game -centric design you describe reflects your way of looking at the domain. 我认为您描述的以Game中心的设计反映了您查看领域的方式。 If you only see "the game" you will model just that. 如果您仅看到“游戏”,那么您将对此建模。 However, games are usually rich simulations full of interesting objects whose behaviors go far beyond their interactions with the player. 但是,游戏通常是充满有趣对象的丰富模拟,其行为远远超出了与玩家的互动。 I would therefore recommend more thinking on the other objects, what they know, what tasks could they carry on, how would they collaborate with others, etc. In other words, do not program the game, program its actors, elements, rules, stages, strategies, characters, etc. Do not underestimate any entities either; 因此,我建议您对其他对象进行更多的思考,他们应该知道什么,他们可以执行什么任务,如何与他人合作等等。换句话说,不要对游戏进行编程,对游戏的参与者,元素,规则,阶段进行编程,策略,角色等。也不要低估任何实体; all of them have something interesting to add to the whole, even a single tile in the chess board has something to contribute to the small universe you are creating. 所有这些元素都可以添加到整体中,这很有趣,即使棋盘中的单个图块也可以为您创建的小宇宙做出贡献。 Not something that anybody else could provide, something that no other object could provide with economy of code and elegance. 没有其他人可以提供的东西,没有其他对象可以提供的代码经济和优雅的东西。

暂无
暂无

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

相关问题 我被告知,并发条件并不一定需要按照Oracle文档所说的那样循环编写 - I've been taught that conditions in concurrency do not necessarily need to be written in a loop, against what the oracle doc says 在不使用Java中的数组列表的情况下为类创建动态数组,并且尚未正确地教过如何做 - Making a Dynamic Array for a class without using Arraylists in Java and have not been properly taught how to do it 方法在哪里片段化? - Where do methods go in a fragment? 类实例中的方法是否在内存中占有一席之地? - Do methods in class instances take a place in memory? 我应该如何创建 Java 应用程序,我应该将 class 放在哪里? - How should I create a Java application, where do I place the class? 在Java中,在使用Java创建对象期间,静态方法在哪里? - In Java where do the static methods go during the creation of an object in java? 在 JAVA 中,我应该将 function 的退货放在哪里? - Where do I place the return of a function in JAVA? 我应该在哪里放置意图代码? - Where do I place the intent code? Android应用程式当机。 我已经尝试了显而易见的解决方案,不确定从何而来 - Android app is crashing. I've tried the obvious solutions not sure where to go from here 我在 IntelliJ 上遇到一个错误,提示无法创建带有奇怪错误消息的类 - I've been getting an error on IntelliJ saying Cannot Create Class with an odd Error message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM