拥有更多的Java类,还是更少的类来完成更多的工作更好? [英] Is it better to have more Java classes, or have fewer classes doing more work?

查看:86
本文介绍了拥有更多的Java类,还是更少的类来完成更多的工作更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发游戏。目前,我有一个类(游戏环境)负责保存游戏对象(敌人,武器等)的集合并进行碰撞检查,调用对象的创建例程等。在项目中,我开始怀疑我是否应该采用更分层的方法-最好是让WeaponsManager,EnemiesManager和Environment将所有内容放在一起,还是让我的Environment类在我处理每个对象时更好?我现在在做什么?

I'm currently working on a game. At the moment, I have one class (the game's environment) responsible for holding collections of game objects (enemies, weapons, etc.) and doing collision-checks, calling the objects' creation routines, etc. As I've progressed with the project, I've started to wonder if I should have a more layered approach - is it better to have, say, a WeaponsManager, an EnemiesManager, and an Environment holding everything together, or to have my Environment class manipulating each object as I'm doing right now?

值得一提的是,游戏对象已经相当重了:

It might be worth mentioning that the game objects are already fairly heavily layered:

BaseClass
-----EnemyClass
----------Enemy1Subclass
----------Enemy2Subclass
-----WeaponClass
----------Weapon1Subclass
----------Weapon2Subclass

BaseClass定义了基本的对象属性,例如位置。 EnemyClass和WeaponClass是相当通用的类,它们定义了更多类特定的方法和属性,例如EnemyClass的速度或WeaponClass的损坏。它们大部分都存在,所以我可以拥有相当通用的集合,而不是针对每个单独的敌人/武器类型单独的集合。 Enemy1Subclass / Enemy2Subclass和Weapon1Subclass / Weapon2Subclass是实际创建和使用的实际敌人/武器类。

BaseClass has basic object properties defined, such as position. EnemyClass and WeaponClass are fairly generic classes that define more class-specific methods and properties, such as speed for EnemyClass or damage for WeaponClass. They mostly exist so I can have fairly generic collections instead of a separate collection for each individual enemy/weapon type. Enemy1Subclass/Enemy2Subclass and Weapon1Subclass/Weapon2Subclass are the actual enemy/weapon classes, which are actually created and used.

我的环境类存储EnemyClass / WeaponClass的集合并执行必要的操作通过调用游戏对象的方法进行操作。

My Environment class stores collections of EnemyClass/WeaponClass and does the necessary manipulation by calling the game objects' methods.

Environment (manipulates EnemyClass and WeaponClass objects by iterating over their respective arrays and calling their respective methods; doesn't do any subclass-specific stuff)
-----EnemyClass array
----------Enemy1Subclass entry
----------Enemy2Subclass entry
----------Enemy1Subclass entry
-----WeaponClass array
----------Weapon1Subclass entry
----------Weapon2Subclass entry
----------Weapon2Subclass entry

但是现在我想知道是否还有另一层分离是一个好主意,环境持有EnemyManager / WeaponManager类,各自的经理持有和操作调整其集合:

But now I'm wondering if yet another layer of separation would be a good idea, with Environment holding EnemyManager/WeaponManager classes and the respective managers holding and manipulating their collections:

Environment (calls generic instantiation, destruction, moving, etc. methods in EnemyManager and WeaponManager; doesn't ever directly interact with an EnemyClass or WeaponClass object)
-----EnemyManager (gets instructions from Environment and manipulates EnemyClass objects to carry out those instructions)
----------EnemyClass array
---------------Enemy1Subclass entry
---------------Enemy2Subclass entry
---------------Enemy1Subclass entry
-----WeaponManager (gets instructions from Environment and manipulates WeaponClass objects to carry out those instructions)
----------WeaponClass array
---------------Weapon1Subclass entry
---------------Weapon2Subclass entry
---------------Weapon2Subclass entry

有什么想法吗?有什么建议吗?我无法找到有关此类更多班级或勤奋工作的班级惯例的任何信息,因此任何答案都是公平的,直到并包括您的模型是垃圾,重新开始。尽管希望它不会实现。 ;)

Thoughts? Suggestions? I've not been able to find anything about this kind of "more classes or harder-working classes" convention, so any answer is fair game, right up to and including "your model is trash, start over." Although hopefully it won't come to that. ;)

推荐答案

一般而言(非常普遍),面向对象语言的主要优点是封装-封装是最好将相关的功能/方法和数据组合在一起(具有适当的权限)。在这里应用,这意味着最好将代码分开是有意义的。在您的示例中,工作难度更大的类表示更大,整体上更难理解且处理大量数据的类。有两个不错的指导原则,涵盖了连续性的任何方面:

Generally speaking (very generally), a major advantage of object-oriented languages is Encapsulation - the idea that it is better to group related functions/methods and data together (with appropriate permissions). Applied here, that means that it is nicer to separate code as far as "it makes sense". In your example, "harder working classes" implies classes that are larger, more difficult to understand as a whole, and dealing with large amounts of data. Two nice guidelines that cover either side of the continuum:


  • 如果您需要流程图来跟踪一个类中发生的所有事情,则可以应该可能会创建另一个类

  • 如果您注意到类A仅包含仅包含方法C的类B,也许这都可以在一个函数之内

  • If you need a flowchart to keep track of everything that happens inside one class, you should probably make another class
  • If you notice Class A only contains Class B which only contains Method C, perhaps this can all be within one function

总而言之,请尽最大努力确保将代码分成合理的部分,而不是多余的部分。

In summary, just do your best to ensure that your code is divided up in reasonable, NOT superfluous sections.

这篇关于拥有更多的Java类,还是更少的类来完成更多的工作更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆