How to use Interface
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 .