How to organise classes, packages [closed]

拥有回忆 提交于 2019-12-03 06:34:36
Josh Pordon

Classes should do one thing (Single Responsibility Principle).

Classes that do related things should go in the same package. If you find you can more closely relate some of the classes in a package, make them a subpackage!

For example, if I had a project with these classes:

  • GreetingInputWindow
  • GreetingDatabaseObject
  • GreetingDatabaseConnector

I might just put them all in the greeting package. If I wanted to, I might put GreetingInputWindow in the greeting.ui package, and the other 2 into the greeting.db package.

I strongly discourage from organizing packages from an implementational point of view, like controllers, data, etc. I prefer grouping them by functionality, that is, feature1, feature2, etc. If a feature is reasonably complex and requires a large number of classes, then (and only then) I create subpackages like above, that is, feature1.controllers, feature1.data, etc.

I don't believe there are any hard and fast rules on packaging convention (though I could be wrong). Normally I break it up into

com.mycompanyname and then:

  • api
  • controllers
  • data (for models)
  • jobs (for cron jobs)
  • reporting
  • servlet
  • utils

If I find I have a class which does not fit into any of those, then I create a new package.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!