什么是松耦合代码?

发布于 2024-12-03 04:54:00 字数 82 浏览 2 评论 0原文

我最近学习了 Ruby 和 Rails,遇到了这些术语,但我无法立即理解。

所以请解释一下(如果可能的话,用类比)什么是松散耦合代码。

I recently learned Ruby and Rails, I come across these terminologies, that I can't understand off the bat.

So Please explain, ( with analogies, if possible ) what is Loosely coupled Code.

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

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

发布评论

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

评论(2

你没皮卡萌 2024-12-10 04:54:00

外行人的解释是,

您有两个类,A 类和 B 类,它们可能会相互作用。如果它们是松散耦合的,A类会做它应该做的事情(你想要它做的事情),而不知道B类的实现细节

希望它有意义吗?

A Lay Man's Explanation,

You have two classes, Class A and Class B that probably interact together. if they are loosely coupled, Class A would do what it is supposed to Do(what you want it to do) without knowing the details of Class B's Implementation

Hope it makes some Sense?

心在旅行 2024-12-10 04:54:00

松耦合基本上意味着您希望程序的组件能够轻松地与其他组件连接,这是您可以享受代码重用并使事情变得更加高效,例如,有像 MVC(模型视图控制器)这样的设计模式可以帮助您分离值得关注的是,您可以让一些代码负责 GUI,而其他一些代码负责访问数据库,如果您想更改这些部分中的任何一个,您希望它们避免彼此过于依赖,即这就是为什么你应该依赖接口而不是实现,你想让事情变得更容易,因为软件总是在变化/发展。

通常松耦合是与“高内聚”的概念一起提到的,这个概念可能是主观的,但它无非是编写有意义的代码并以一种有助于理解、维护、可扩展性等的方式设计组件。

loose coupling basically means that you want the components of your program to connect easily with other components, this was you can enjoy code reuse and make things more efficient, e.g., there are Design Patters like MVC (Model View Controller) that help you with separation of concerns, you can have some of your code responsible for the GUI and some other code responsible for the access to a database, and if you want to change any of these parts you want them to avoid being too dependent on each other, i.e., that's why you should rely on interfaces instead of implementations, you want to make things easier because software is always changing/evolving.

Usually loose coupling is mentioned along with the concept of "High Cohesion", this one can be subjective but it's nothing more than writing code that makes sense and design your components in a way that will facilitate understanding, maintenance, extensibility etc.

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