How to use Interface

本文探讨了面向对象编程中接口的重要性,以及如何通过依赖注入来实现类之间的解耦。介绍了依赖注入的三种方式:构造函数注入、方法注入和setter注入,并讨论了接口在不同架构层间的作用。

Interface

In OO world , interface is a very important role whichis responsible for connected classes or architect layers .in another word ,it’s the classes or the architecture’s bridge .

With Interface , Classes can“talk”

How does class talk ?

Without interface , as we know ,if class1 want to callclass2 method or use class2 something , it will new a instance of class2 like :

var c2 = new class2();

what’s wrong , class1 know class2 too much .in anotherword , class1 should tell class2 what he need ,but not ask class2 to dosomething for him .

so need interface .



Class1 “tell” what he want to class2 , then class2 doit .

Then How does class1 get what he need ?

Dependency injection . How?

1.Constructor

2.Method

3.Setter

Usually , We use constructor :

As We can see, interface perfectly be the bridge between two classes who need to “talk

AnotherInterface usage is to connect two architect layers .

A sampleASP.NET project structure .






Conclusion :

when you need to call some method , do not new the class , but list what you want (we call itinterface ), then let other class implement it.Then by dependency injection get the interface do whatever you need .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值