在面向对象范式中,有人能准确地描述松耦合和紧耦合之间的区别吗?
当前回答
紧密耦合意味着一个类依赖于另一个类。 松耦合意味着一个类依赖于接口而不是类。
在紧密耦合中,方法中声明了硬编码的依赖项。 在松耦合中,我们必须在运行时向外部传递依赖项,而不是硬编码。(松散耦合系统使用接口来减少与类的依赖。)
例如,我们有一个系统可以以两种或多种方式发送输出,如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();
}
}
其他回答
紧密耦合意味着一个类依赖于另一个类。 松耦合意味着一个类依赖于接口而不是类。
在紧密耦合中,方法中声明了硬编码的依赖项。 在松耦合中,我们必须在运行时向外部传递依赖项,而不是硬编码。(松散耦合系统使用接口来减少与类的依赖。)
例如,我们有一个系统可以以两种或多种方式发送输出,如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();
}
}
这里有很多使用类比的很好的答案,但是一个工作中的朋友给了我一个例子,比这里提到的所有例子都更让我喜欢……眼睛和眼镜!
紧密耦合
紧密耦合是眼睛。如果我想修复我的视力,做眼部移植手术非常昂贵,而且有相当大的风险。但如果设计师(作为人类)找到了更好的方法呢?添加一个与主体松散耦合的特性,这样就可以轻松地更改它!(是的. .眼镜)
松散耦合
我可以很容易地更换我的眼镜,而不会破坏我的潜在视力。我可以摘下眼镜,我的视力就会恢复到以前的水平(不是更好或更差)。使用不同的眼镜改变了我们通过眼睛看世界的方式,而且风险很小,易于维护。
总结
所以下次有人问你“谁在乎我的代码是否紧密耦合?”答案都是关于努力改变,努力维持和改变的风险。
那么这在c#中是如何实现的呢?接口和依赖注入!
EDIT
这也是Decorator模式的一个很好的例子,其中眼睛是我们通过满足界面需求来装饰的类,但提供了不同的功能(例如太阳镜、老花镜、珠宝商的放大镜等)。
松耦合是老式硬编码依赖关系和相关问题的答案,比如当任何事情发生变化时频繁重新编译和代码重用。它强调在组件中实现工作者逻辑,避免解决方案特定的连接代码。
松耦合= IoC 请看下面的解释。
在面向对象设计中,耦合量指的是一个类的设计依赖于另一个类的设计的程度。换句话说,A类力的变化与B类力的变化相关的频率是多少?紧耦合意味着两个类经常一起更改,松耦合意味着它们大部分是独立的。一般来说,推荐使用松耦合,因为它更容易测试和维护。
你可能会发现Martin Fowler的这篇论文(PDF)很有帮助。
松耦合和紧耦合是关于程序组件之间的依赖关系。这意味着不仅依赖于编程类,还依赖于编程系统组件。
例如,如果你只使用简单的原始SQL查询从SQL Server接收数据,这是松耦合。与松耦合和简单原始SQL查询相反的是紧耦合和实体框架核心。在实体框架核心,你必须完整的模型与POCO类在你的代码反映数据库结构,这意味着任何变化在数据库中,你必须反映在代码。
因此,程序代码和数据库结构之间的紧密耦合是实体框架,这种方法的对立面是拒绝使用任何ORM,并拒绝在程序代码中使用完整的镜像数据库结构。
推荐文章
- 在JavaScript中根据键值查找和删除数组中的对象
- 在Java中使用“final”修饰符
- 我怎么知道什么时候创建一个接口?
- 在JSON键名中哪些字符是有效的/无效的?
- 构造函数vs工厂方法
- 在PHP5中创建单例设计模式
- 如何正确地设置和拆除带有测试的pytest类?
- 什么是依赖倒置原则?为什么它很重要?
- 访问对象内存地址
- 为什么在Python方法中需要显式地有“self”参数?
- 如何在方法中访问“静态”类变量?
- 使arrayList.toArray()返回更具体的类型
- 如何从对象数组中通过对象属性找到条目?
- 为什么c#不提供c++风格的'friend'关键字?
- new Object()和对象文字表示法的区别是什么?