[英]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 因此,如果您走这条路,您将拥有
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.