如何组织类,包 [英] How to organise classes, packages

查看:198
本文介绍了如何组织类,包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何确定一个包名应该是什么以及哪个类应该进入哪个包?

How do you decide what a package name should be and what class should go into what package ?

我正在开展一个项目,我不断添加/删除类并且不确定我是否需要新包,或者应该将其添加到我当前不知道的现有包中。

I'm working on a project where I am constantly adding/removing classes and not really sure if I need a new package, or should add it to an existing one that im not currently aware of.

您是否遵循一套规则在创建新包时?

Do you follow a set of rules when creating a new package ?

您如何知道您是否没有复制包功能?这只是为了熟悉这个项目。

How do you know if you are not duplicating package functionality ? Is this just down to familiarity with the project.

任何指针都赞赏。

推荐答案

课程应该做一件事(单一责任原则)。

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

  • GreetingInputWindow
  • GreetingDatabaseObject
  • GreetingDatabaseConnector

我可能只是将它们全部放在 greeting 包中。如果我想,我可以在 greeting.ui 包中加入 GreetingInputWindow ,将另外2包放入 greeting.db package。

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.

这篇关于如何组织类,包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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