如何让代码模块化?

发布于 2024-08-05 07:26:57 字数 112 浏览 6 评论 0 原文

我有一些Java程序,现在我想知道它是否是模块化的,如果是模块化的,那么模块化到什么程度,因为模块化永远不可能是二进制术语,即0或1。 我如何确定特定代码在很大程度上是模块化的。我想知道如何使代码更加模块化?

I have some Java programs, now I want to find out whether it is modular or not, if it is modular then up to what extent, because modularity can never be binary term i.e. 0 or 1.
How do I decide that particular code is modular upto this much extent. I want to know how to make code much more modular?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

装纯掩盖桑 2024-08-12 07:26:57

模块化的一些基准:

  1. 重写类似代码多少次 用于执行特定任务?
  2. 当您更改某些部分时,您需要重构代码多少你的程序?
  3. 文件是否小且易于浏览?
  4. 应用程序模块是否在需要时充分且独立地执行?
  5. 您的代码灾难性程度最低吗?当你只删除一个函数或变量时,一切都会失败吗? 重命名类时是否会遇到 20 多个错误?(要检查这一点,您可以实现堆栈机制来跟踪应用程序中的所有跃点)
  6. 代码与自然语言用法有多接近?(即模块及其子组件代表更多现实世界的对象,而无需过多关注净源文件大小)。

有关更多想法,请查看 软件质量上的这篇

简介至于您对让你的代码更加模块化首先你应该问自己上述问题,获得具体的答案,然后看看这个

基本理念是将应用程序分解为尽可能小的代码片段,并整齐地排列在多个易于理解和访问的目录布局中。

应用程序中的每种方法执行的处理量不得超过所需的最小处理量。将这些方法组合成越来越多的宏观方法应该会引导您回到您的应用程序。

Some Benchmarks for modularity:

  1. How many times are you rewriting similar code for doing a particular task?
  2. How much do you have to refactor your code when you change some part of your program?
  3. Are the files small and easy to navigate through?
  4. Are the application modules performing adequately and independently as and when required?
  5. Is your code minimally disastrous? Does all hell break lose when you delete just one function or variable? Do you get 20-odd errors upon re-naming a class? (To examine this, you can implement a stacking mechanism to keep trace of all the hops in your application)
  6. How near is the code to natural language usage? (i.e. modules and their subcomponents represent more real world objects without giving much concern to net source file size).

For more ideas check out this blurb about modularity and this one on software quality

As for your concern on making your code more modular first you should ask yourself the above questions, obtain specific answers for them and then have a look at this.

The basic philosophy is to break down your application into as small of code fragments as possible, arranged neatly across a multitude of easily understandable and accessible directory layouts.

Each method in your application must do no more than the minimum quanta of processing needed. Combining these methods into more and more macro level methods should lead you back to your application.

与君绝 2024-08-12 07:26:57

关键点是

  • 关注点
  • 分离内聚封装
  • (通过接口进行通信)
  • 可替代性
  • 可重用性

此类模块系统的一个很好的例子是标准汽车零件,如盘式制动器和汽车音响。
当您制造汽车时,您不想从头开始制造汽车音响。您宁愿购买它并将其插入。您也不希望制动系统影响汽车音响 - 或者更糟糕的汽车音响影响制动系统。

为了回答您的问题“我如何确定特定代码在很大程度上是模块化的”,我们可以提出问题来测试模块化性。 您能否轻松地用其他模块替换您的模块而不影响应用程序的其他部分?

XML 解析器可能是另一个例子。一旦获得 DOM 接口,您实际上并不关心底层使用的是哪种 XML 解析器实现(例如 Apache Xerces 或 JAXP)。

在 Java 中,另一个问题可能是:所有功能都可以通过接口访问吗?接口几乎可以解决低耦合问题。

另外,您能用一句话描述一下您系统中的每个模块吗?例如,汽车音响播放音乐和收音机。盘式制动器使车辆安全减速。


(这是我写给 什么是组件驱动开发?

的内容 )在 Wikipedia 中,基于组件的开发是基于组件的软件工程 (CBSE) 的别名。

[它]是软件的一个分支
工程,其优先事项是
方面的关注点分离
广泛的功能
在给定的软件中可用
系统。

这有点模糊,所以让我们看看更多细节。

单个组件是一个软件
包或模块,
封装了一组相关的
函数(或数据)。

所有系统进程都被放入
独立的组件,以便所有的
每个内部的数据和功能
组件在语义上相关
(就像内容一样
类)。因为这个原则,
人们常说组件是
模块化内聚

因此,根据这个定义,组件可以是任何东西,只要它真正做好一件事并且只做一件事。

关于全系统
协调,组件通信
通过接口相互联系。 [...]
这一原则导致组件被称为“封装”。

所以这听起来越来越像我们认为好的 API 或 SOA 应该是什么样子。

提供的接口由棒棒糖表示,必需接口由附加到 UML 中组件外边缘的开放套接字符号表示。

替代文本

另一个重要属性
组件是它们
可替代,因此组件
可以被另一个替换(在
设计时或运行时),如果
初始组件的要求
(通过接口表达)满足
由后继组件。

可重用性很重要
高品质的特征
软件组件。一个软件
组件应该被设计和
实施以便可以重用
在许多不同的程序中。

可替换性和可重用性是组件之所以成为组件的原因。
那么这和面向对象编程有什么区别呢?

面向对象的思想
编程(OOP)是这样的软件
应该根据a写成
实际或想象的心理模型
它代表的对象。 [...]

基于组件的软件工程,
相比之下,没有这样的
假设,并指出
软件应该通过粘合来开发
预制构件多在一起
就像在电子领域或
力学。

Key points are

  • Separation of concerns
  • Cohesion
  • Encapsulation (communicates via interface)
  • Substitutability
  • Reusability

A good example of such module system is standard car parts like disk brakes and car stereo.
You don't want to build car stereo from scratch when you are building cars. You'd rather buy it and plug it in. You also don't want the braking system affecting the car stereo — or worse car stereo affecting the brake system.

To answer your question, "How do I decide that particular code is modular up to this much extent," we can form questions to test the modularity. Can you easily substitute your modules with something else without affecting other parts of your application?

XML parsers could be another example. Once you obtain the DOM interface, you really don't care which implementation of XML parser is used underneath (e.g. Apache Xerces or JAXP).

In Java, another question may be: Are all functionality accessible via interfaces? Interface pretty much takes care of the low coupling.

Also, can you describe each module in your system with one sentence? For example, a car stereo plays music and radio. Disk brakes decelerate the vehicle safely.


(Here's what I wrote to What is component driven development?)

According to Wikipedia, Component-Based Development is an alias for Component-based software engineering (CBSE).

[It] is a branch of software
engineering, the priority of which is
the separation of concerns in respect
of the wide-ranging functionality
available throughout a given software
system.

This is somewhat vague, so let's look at more details.

An individual component is a software
package, or a module, that
encapsulates a set of related
functions (or data).

All system processes are placed into
separate components so that all of the
data and functions inside each
component are semantically related
(just as with the contents of
classes). Because of this principle,
it is often said that components are
modular and cohesive.

So, according to this definition, a component can be anything as long as it does one thing really well and only one thing.

With regards to system-wide
co-ordination, components communicate
with each other via interfaces. [...]
This principle results in components referred to as encapsulated.

So this is sounding more and more like what we think of good API or SOA should look like.

The provided interfaces are represented by a lollipop and required interfaces are represented by an open socket symbol attached to the outer edge of the component in UML.

alt text

Another important attribute of
components is that they are
substitutable, so that a component
could be replaced by another (at
design time or run-time), if the
requirements of the initial component
(expressed via the interfaces) are met
by the successor component.

Reusability is an important
characteristic of a high quality
software component. A software
component should be designed and
implemented so that it can be reused
in many different programs.

Substitutability and reusability is what makes a component a component.
So what's the difference between this and Object-Oriented Programming?

The idea in object-oriented
programming (OOP) is that software
should be written according to a
mental model of the actual or imagined
objects it represents. [...]

Component-based software engineering,
by contrast, makes no such
assumptions, and instead states that
software should be developed by gluing
prefabricated components together much
like in the field of electronics or
mechanics.

独夜无伴 2024-08-12 07:26:57

要回答如何使代码更加模块化的具体问题,有以下几种方法:

  • 模块化的最佳工具之一是发现代码重用。如果您发现您的代码在多个地方执行完全相同(或非常相似)的操作,那么它是模块化的良好候选者。

  • 确定哪些逻辑可以独立,从某种意义上说,其他逻辑可以使用它们,而无需知道它们是如何构建的。这有点类似于 OO 设计中的内容,尽管模块/组件不一定需要与 OO 中的建模对象相对应。

To answer your specific question of how to make the code more modular, a couple of approaches are:

  • One of best tool for modularization is spotting code re-use. If you find that your code does the same exact (or very similar) thing in more than once place, it's a good candidate for modularizing away.

  • Determine which pieces of logic can be made independent, in a sense that other logic would use them without needing to know how they are built. This is somewhat similar to what you to in OO design, although module/component does not necessarily need to correspond to a modeled object as in OO.

懒的傷心 2024-08-12 07:26:57

Hej,

请参阅“如何封装软件(第 1 部分)”,此处:

http:// /www.edmundkirwan.com/encap/overview/paper7.html

问候,

埃德。

Hej,

See, "How to encapsulate software (Part 1)," here:

http://www.edmundkirwan.com/encap/overview/paper7.html

Regards,

Ed.

︶ ̄淡然 2024-08-12 07:26:57

由于它已被标记为“osgi”,因此我可以提出与 OSGi 相关的观点。

简短的回答是,可以通过小步骤从完全意大利面条式代码转变为模块化;它不一定是一个大爆炸。例如,即使是意大利面条代码也依赖于某种博洛尼亚日志库,因此从某种意义上说,它已经是模块化的,只是其中包含一个非常大的 Metball(对不起,模块)。

诀窍是将大肉丸分解成一个较小的块,然后再分解成一个稍小的大肉丸,然后递归。这一切也不必一次性完成;只需每次多削一点,直到没有任何东西可以去除为止。

至于 OSGi,仍然可以将 uber-jar 放入捆绑包中。事实上,您可以在不更改位的情况下完成此操作;通过就地修改 Manifest.MF,或者将其包装在另一个 JAR 中并在清单中指定 Bundle-ClassPath:metaball.jar。

如果做不到这一点,BND 等工具可以帮助生成您需要的正确数据,然后可以轻松地将其放入 OSGi 运行时中。但要小心过度耦合的代码,以及与类加载器混在一起的东西——这些会让你陷入困境。

Since this has been tagged with 'osgi', I can throw in an OSGi-related perspective.

The short answer is that it is possible to go from completely spaghetti code to modular in small steps; it doesn't have to be a big bang. For example, even spaghetti code depends on some kind of bolognaise logging library, so in some sense, it's already modular, just with One Very Big Metball (sorry, module) in it.

The trick is to break the big meatball into one smaller chunk and then a slightly less big meatball and then recurse. It doesn't all have to be done in one go either; simply chip off a bit more each time until there is nothing left to remove.

As for OSGi, it's still possible to put an uber-jar into a bundle. In fact, you can do this without changing the bits; either by modifying the Manifest.MF in place, or by wrapping that in another JAR and specify Bundle-ClassPath: metaball.jar in the manifest.

Failing that, tools like BND can help generate the right data you'd need, and then it can be dropped in an OSGi runtime easily enough. But beware of overly coupled code, and stuff that mucks around with classloaders - those will trip you up.

指尖上的星空 2024-08-12 07:26:57

假设我理解你的问题,你想知道是什么使代码模块化,因为代码模块显然需要彼此之间的一些依赖关系才能工作。这是我的答案:

如果您可以将系统分解为模块,并且可以单独测试这些模块,那么这很好地表明系统是模块化的。

Assuming I understand your question, that you want to know what it is that makes code modular, since code modules will obviously need some dependency between each other to work at all. This is my answer:

If you can break your system down into modules, and you can test those modules in isolation, that is a good indication that a system is modular.

万人眼中万个我 2024-08-12 07:26:57

正如您所说,模块化不是一个二元的东西,所以它取决于您的相对定义。

我会说:你可以在任何需要执行该功能的程序中使用给定的方法吗?它是一个“黑匣子”吗?您不需要知道它在幕后做什么?如果答案是否定的,即该方法只能在该程序中正常工作,那么它就不是真正的模块化。

As you say modularity is not a binary thing so it depends on your relative definition.

I would say: Can you use a given method in any program where you need to perform that function? Is it the "black box" where you wouldn't need to know what it were doing under the hood? If the answer is no, i.e. the method would only work properly in that program then it is not truely modular.

软甜啾 2024-08-12 07:26:57

模块化与开发代码的人相关。但我认为普遍的共识是,模块化代码是具有可以轻松交换的部分而无需更改大部分原始代码的代码。

恕我直言,如果您有 3 个模块 AB 和 C,并且您想完全更改或替换模块 C,如果这样做是一项简单的任务,那么您就有模块化代码。

Modularity is relative to who ever is developing the code. But I think the general consensus is that modular code is code that has portions that can easily be swapped out without changing most of the original code.

IMHO, If you have 3 modules A B and C and you want to change or replace module C completely, if it is a SIMPLE task to do so then you have modular code.

染柒℉ 2024-08-12 07:26:57

您可以使用CAP等代码分析工具来分析类型和包之间的依赖关系。它们将帮助您找到并消除任何循环依赖关系,这在尝试开发模块化代码时通常是一个问题。
如果不存在循环依赖关系,您可以开始将代码分成离散的 jar。

一般来说,如果可以的话,对接口进行编码是一种很好的做法,这通常意味着您的代码可以更轻松地重构和/或在不同的上下文中使用。

Spring 等依赖注入框架也有助于实现设计的模块化。由于类型是通过某些外部配置过程注入其依赖项的,因此它们不需要对实现的直接依赖项。

You can use a code analysis tool such as CAP to analyse the dependencies between types and packages. They'll help you find and remove any cyclic dependencies, which are often a problem when trying to develop modular code.
If there are no cyclic dependencies, you can start separating your code into discrete jars.

In general it is good practice to code to interfaces if you can, this generally means your code can more easily be refactored and/or used in different contexts.

Dependency injection frameworks such as Spring can also help with the modularity of your design. As types are injected with their dependencies by some external configuration process they don't need a direct dependency on an implementation.

冰之心 2024-08-12 07:26:57

按功能打包的想法有助于使代码更加模块化。

网上看到的许多示例首先将应用程序划分为层,而不是功能

  • 模型
  • 数据访问
  • 用户界面

但是,使用与功能而不是层一致的顶级包来划分应用程序似乎更好。

以下是使用按功能打包的 Web 应用程序的示例。请注意顶级包的名称,它读取为应用程序中实际功能的列表。还要注意每个包如何包含与功能相关的所有项目 - 这些项目并没有散布在各处;大多数时候,它们都在一个包/目录中。

通常,删除此类应用程序中的功能可以通过单个操作来实现 - 删除单个目录。

The package-by-feature idea helps to make code more modular.

Many examples seen on the web divide applications first into layers, not features

  • models
  • data access
  • user interface

It seems better, however, to divide applications up using top-level packages that align with features, not layers.

Here is an example of a web app that uses package-by-feature. Note the names of the top-level packages, which read as a list of actual features in the application. Note as well how each package contains all items related to a feature - the items aren't spread out all over the place; most of the time, they are all in a single package/directory.

Usually, deletion of a feature in such an app can be implemented in a single operation - deletion of a single directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文