服务的松散耦合与紧密耦合 [英] Loose coupling vs tight coupling of services
问题描述
我了解到,我们几乎始终希望在应用程序和应用程序中的各个组件之间实现松散耦合.为什么.
I understand that we at almost all times want to aim for loose coupling between components within an application & why.
但是在下面的示例中,如果有人可以澄清为什么当我们将服务作为参数传递给构造函数时它没有紧密耦合的原因,那么我就可以理解背后的逻辑.
But in the below example if someone could clarify as to why it isn't tightly coupled when we pass in a service as a parameter to a constructor so I could understand the logic that's going on under the hood.
export class Test(){
dataResults;
constructor(service: testService){
this.dataResults = service.getData()
}
}
推荐答案
紧密耦合方式:编写的代码仅取决于一个类,例如,如果我编写
Tight coupling means : Code written is dependent on one class only, for example if i write
service:TestService;
constructor(){
this.service = new TestService();
this.dataResults = service.getData()
}
上面的代码紧密耦合,因为如果明天如果我想用ActualService
替换TestService
,我必须打开消耗TestService
的文件并必须进行修改.
above code is tightly coupled because if tomorrow if i want to replace TestService
with ActualService
, I have to open files which is consuming TestService
and have to do modification.
现在,松散耦合与紧密耦合相反,这意味着类不再直接依赖于所消耗服务的类.在像C#这样的高级语言中,对于松散的耦合代码,应这样编写
Now, Loose Coupling is reverse of tight coupling , means class is not dependent directly on the class whose service it consuming. In higher order language like C# , for loose coupling code is written like this
public class A{
A(IB service) {//here IB is interface , on which A class is depedant
//use service here
}
}
public class B : IB { }
public class C : IB { }
因此,现在IB上的ID紧挨着,您可以轻松地完成此操作
so now A id dedpend on IB , you can easily do this
A a = new A(new B());
or
A a = new A(new C());
因此它的创建是A取决于IB,它可以是B或C.因此它与B和C松散耦合.
so its create that A is depend on IB which can be B or C. so it becomes loosely coupled with B and C.
现在进入您的代码,哪个Angular代码,
Now coming to your code which Angular code,
当我们使用out组件中的任何服务时,在angular中,我们确实通过提供者在模块或组件处注册了它.
In angular when we make use of any service in out component , we do register it via provider either at module or at component.
[{ provide: Logger , useClass: Logger}]
,然后在组件中就像
constructor(service: Logger){
this.dataResults = service.getData()
}
现在如果我想用BetterLooger代替Logger,我只需要这样做
Now tomorrow if i want to replace Logger with BetterLooger I just need to do like this
[{ provide: Logger , useClass: BetterLogger}]
or
[{ provide: Logger , useClass: TestLogger}]
无需输入组件代码,那就是它如何变得松散耦合的.甚至它对组件的测试也非常有帮助,其中一个人想用MockService
替换actul服务(请在此处检查: https ://angular.io/guide/testing )
with out going in component code , thats how it becomes loose coupled. Or even its very helpful in testing of components also, where one want to replace actul service with MockService
(check here : https://angular.io/guide/testing)
扩展Logger
.您可以去那里进行详细检查: https://angular.io/guide/dependency-injection
for above to work BetterLogger and TestLogger
must extend Logger
. you can go there and check in detail : https://angular.io/guide/dependency-injection
这篇关于服务的松散耦合与紧密耦合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!