在面向对象范式中,有人能准确地描述松耦合和紧耦合之间的区别吗?
当前回答
摘自我关于耦合的博客文章:
什么是紧密耦合:-
正如上面定义的那样,紧密耦合对象是一个需要了解其他对象的对象,并且通常高度依赖于彼此的接口。
当我们更改紧耦合应用程序中的一个对象时,通常需要更改许多其他对象。在一个小的应用程序中没有问题,我们可以很容易地识别变化。但在大型应用程序中,这些相互依赖关系并不总是为每个消费者或其他开发人员所了解,或者将来可能会有很多变化。
让我们用购物车演示代码来理解紧耦合:
namespace DNSLooseCoupling
{
public class ShoppingCart
{
public float Price;
public int Quantity;
public float GetRowItemTotal()
{
return Price * Quantity;
}
}
public class ShoppingCartContents
{
public ShoppingCart[] items;
public float GetCartItemsTotal()
{
float cartTotal = 0;
foreach (ShoppingCart item in items)
{
cartTotal += item.GetRowItemTotal();
}
return cartTotal;
}
}
public class Order
{
private ShoppingCartContents cart;
private float salesTax;
public Order(ShoppingCartContents cart, float salesTax)
{
this.cart = cart;
this.salesTax = salesTax;
}
public float OrderTotal()
{
return cart.GetCartItemsTotal() * (2.0f + salesTax);
}
}
}
上面例子中的问题
紧密耦合产生了一些困难。
在这里,OrderTotal()方法为我们提供了购物车当前项目的完整金额。如果我们想在这个购物车系统中添加折扣功能。在上面的代码中很难做到这一点,因为我们必须在每个类中进行更改,因为它是非常紧密耦合的。
其他回答
松耦合是在不提供依赖关系的所有信息的情况下,间接地给出类所需要的依赖关系的过程。E在from的接口中),以防紧耦合,你直接给出依赖关系,这不是编码的好方法。
松耦合是老式硬编码依赖关系和相关问题的答案,比如当任何事情发生变化时频繁重新编译和代码重用。它强调在组件中实现工作者逻辑,避免解决方案特定的连接代码。
松耦合= IoC 请看下面的解释。
在面向对象设计中,耦合量指的是一个类的设计依赖于另一个类的设计的程度。换句话说,A类力的变化与B类力的变化相关的频率是多少?紧耦合意味着两个类经常一起更改,松耦合意味着它们大部分是独立的。一般来说,推荐使用松耦合,因为它更容易测试和维护。
你可能会发现Martin Fowler的这篇论文(PDF)很有帮助。
紧密耦合意味着一个类依赖于另一个类。 松耦合意味着一个类依赖于接口而不是类。
在紧密耦合中,方法中声明了硬编码的依赖项。 在松耦合中,我们必须在运行时向外部传递依赖项,而不是硬编码。(松散耦合系统使用接口来减少与类的依赖。)
例如,我们有一个系统可以以两种或多种方式发送输出,如JSON输出、CSV输出等。
紧耦合的
public interface OutputGenerator {
public void generateOutput();
}
public class CSVOutputGenerator implements OutputGenerator {
public void generateOutput() {
System.out.println("CSV Output Generator");
}
}
public class JSONOutputGenerator implements OutputGenerator {
public void generateOutput() {
System.out.println("JSON Output Generator");
}
}
// In Other Code, we write Output Generator like...
public class Class1 {
public void generateOutput() {
// Here Output will be in CSV-Format, because of hard-coded code.
// This method tightly coupled with CSVOutputGenerator class, if we want another Output, we must change this method.
// Any method, that calls Class1's generateOutput will return CSVOutput, because Class1 is tight couple with CSVOutputGenerator.
OutputGenerator outputGenerator = new CSVOutputGenerator();
output.generateOutput();
}
}
在上面的例子中,如果我们想要更改JSON中的输出,那么我们需要在整个代码中找到并更改,因为Class1与CSVOutputGenerator类紧密耦合。
松散耦合
public interface OutputGenerator {
public void generateOutput();
}
public class CSVOutputGenerator implements OutputGenerator {
public void generateOutput() {
System.out.println("CSV Output Generator");
}
}
public class JSONOutputGenerator implements OutputGenerator {
public void generateOutput() {
System.out.println("JSON Output Generator");
}
}
// In Other Code, we write Output Generator like...
public class Class1 {
public void generateOutput(OutputGenerator outputGenerator) {
// if you want to write JSON, pass object of JSONOutputGenerator (Dependency will be passed externally to this method)
// if you want to write CSV, pass object of CSVOutputGenerator (Dependency will be passed externally to this method)
// Due to loose couple with class, we don't need to change code of Class1, because Class1 is loose coupled with CSVOutputGenerator or JSONOutputGenerator class
// Any method, that calls Class1's generateOutput will desired output, because Class1 does not tight couple with CSVOutputGenerator or JSONOutputGenerator class
OutputGenerator outputGenerator = outputGenerator;
output.generateOutput();
}
}
一般来说,紧密耦合是不好的,但大多数时候,因为它降低了代码的灵活性和可重用性,它使更改更加困难,它阻碍了可测试性等。
Tightly Coupled Object is an object need to know quite a bit about each other and are usually highly dependent on each other interfaces. Changing one object in a tightly coupled application often requires changes to a number of other objects, In small application we can easily identify the changes and there is less chance to miss anything. But in large applications these inter-dependencies are not always known by every programmer or chance is there to miss changes. But each set of loosely coupled objects are not dependent on others.
简而言之,我们可以说,松耦合是一种设计目标,它寻求减少系统组件之间的相互依赖关系,目的是降低一个组件的更改将要求任何其他组件进行更改的风险。松散耦合是一个更通用的概念,旨在增加系统的灵活性,使其更可维护,并使整个框架更“稳定”。
耦合指的是一个元素对另一个元素的直接认识程度。我们可以说A和B,只有B在A改变其行为时才改变其行为。松散耦合的系统可以很容易地分解为可定义的元素。
推荐文章
- 在JavaScript中根据键值查找和删除数组中的对象
- 在Java中使用“final”修饰符
- 我怎么知道什么时候创建一个接口?
- 在JSON键名中哪些字符是有效的/无效的?
- 构造函数vs工厂方法
- 在PHP5中创建单例设计模式
- 如何正确地设置和拆除带有测试的pytest类?
- 什么是依赖倒置原则?为什么它很重要?
- 访问对象内存地址
- 为什么在Python方法中需要显式地有“self”参数?
- 如何在方法中访问“静态”类变量?
- 使arrayList.toArray()返回更具体的类型
- 如何从对象数组中通过对象属性找到条目?
- 为什么c#不提供c++风格的'friend'关键字?
- new Object()和对象文字表示法的区别是什么?