如何最好地将大型应用程序划分为模块?

How to best divide big app into modules?

构建我正在开发的应用程序需要花费大量时间。这是我工作过的最大的一个。我尝试调整 gradle 设置,这很有帮助,但构建速度仍然很慢。

由于应用程序是在没有考虑模块的情况下构建的,它只是一大堆包,现在我想知道我如何 "extract" 其中一些并将它们放入单独的模块中。据我所知,模块不应该依赖于应用程序模块,所以我想知道是否有一种工具或技术可以让我分析代码并帮助我找到要提取的正确包,因为它有很多代码。

你会如何处理我的问题?

这主要是一个设计问题。正如您所说,项目中已经有大量代码,一种方法是分析整个项目结构的 UML 图。目标是识别体系结构的区域,在这些区域中,少数 类 之间的交互紧密耦合,也可以根据 类 具有相同的外部依赖性来形成组。

通过这种方法,您可以降低大型项目的复杂性,将 类 与大型项目中不使用的外部依赖项分离。您将项目拆分成的各个模块将具有更快的构建时间。然后,您可以将项目拆分成的模块作为依赖项在主项目中引用。额外的好处是每次更改时只会重建主项目中修改的模块。

This Stack Overflow post discusses many UML diagram generator plugins for Android Studio. Code Iris is a good option that you can install via the Android Studio plugin menu. As an example, here is the output from Code Iris on a sample FaceTracker Android application(点击图表放大):

此处的图表显示了包和项目的分组。您可以看到不同的项目被分成单独的绿色框,在这些框内,是包的框,最后是 类 和交互。通过分析 UML,您可以首先确定如何最好地分组您的 类 并创建单独的项目。将主项目拆分为模块后,您可以再次使用 Code Iris 在对结构进行更改后可视化交互。

您的问题是软件工程中的源代码模块化。它是软件中的新课题,关于它的参考资料很少。源代码模块化是对源代码上 聚类 概念的重铸。

此参考来自 (see reference 1)

The aim of the software modularization process is to partition a software system into subsystems to provide an abstract view of the architecture of the software system, where a subsystem is made up of a set of software artifacts which collaborate with each other to implement a high-level attribute or provide a high-level service for the rest of the software system.

However, for large and complex software systems, the software modularization cannot be done manually, owing to the large number of interactions between different artifacts, and the large size of the source code. Hence, a fully automated or semiautomated tool is needed to perform software modularization.

源代码模块化 (see reference 1) 有 多种技术(算法)

  1. 分层技术:

    • 单联动、完全联动、平均联动
    • 沃德法、中位数法、质心法
    • 组合和加权组合方法
  2. 基于搜索的技术:

    • 爬山,多点爬山 (HC)
    • 模拟退火 (SA)
    • 遗传算法(GA)

请注意, 您也可以使用此名称找到其他 聚类 技术。但是模块化有点不同。它们被重铸为源代码模块化。

整体源码模块化流程如下图:


您可以使用许多工具。您可以在模块化过程中使用它们:

  1. 静态源代码分析工具(获取 ADG 格式等)参见参考资料here -(如 Understand、NDepend 等)
  2. 可视化工具 -(图形可视化)见列表here (like Tom Sawyer Visualization)

以小项目为例,如果你的项目结构(使用静态分析工具从源代码生成)是这样的:

结果可能是这样的(应用模块化过程后):

我会将我的应用程序分为四层:

  1. 对象层:在这一层中,您可以使用 get 和 set 方法启动您需要的所有对象{示例:

class person{ region private private int _PersonID; endregion region public public int PersonID{get{return _PersonID;}set{_PersonID=value;}} endregion }}

  1. 数据访问层:该层将处理连接数据库的贡献,并执行与过程、触发器和函数相关的所有事情。{此部分必须真正受到保护} {不要在代码中实现任何 sql 查询,将所有查询构建到数据库中,并通过在代码中调用它们的名称来连接这些过程} {示例://

    class personDAO { private List _GetPersons(){//codes here} ; public List GetPersons(){ _GetPersons();} public delegate void del_GetPersons(); private del_GetPersons _del_GetPersons; public del_GetPersons Del_GetPersons { get{return _del_GetPersons;} set {_del_GetPersons=value;} } public personDAO() {//constructor del_GetPersons=GetPersons; } } }

  2. 业务对象层,该层将委托数据访问库的实例,然后修改它并添加多个异常处理程序。 "we use delegates to hide our method names that are used in by equalizing the method to it's delegate into the constructor function of the DataAccessLibrary "。 例子 class personBO { //create instance of personDAO //create an other delegate for personBO //create private method _GetPerson(){//call personDAO.del_GetPersons()} //create public method GetPerson() {// call _GetPerson()} create public constructor function personBO{//set public method = delegates of bo} }

4.Finally最后一层或用户有权与之交互的层,它是通过前端处理程序和隐藏的后端处理程序处理的多个连接表单(他们也被称为使用代表)。

  • 此结构构建应用程序的时间可能比 其他

  • 但速度很快(因为委托使速度更快)

  • 它是受保护的(因为它被设计成许多层,并且您正在处理调用对象实例而不是对象本身的隐藏方法)。