Reputation: 724
I have not seen interfaces implemented as class members before. Can someone explain what's going on? Are you allowed to instantiate an interface? Where is the implementation of the methods required by these interfaces?
public class MyClass
{
private readonly ITest1 interface1;
private readonly ITest2 interface2;
private readonly ITest3 interface3;
public MyClass(ITest1 interface1, ITest2 interface2, ITest3 interface3)
{
this.interface1 = interface1;
this.interface2 = interface2;
this.interface3 = interface3;
}
public void TestMethod()
{
var lines = interface1.GetData();
var file = interface2.Parse(lines);
interface3.Copy(file);
}
}
What's the difference compared to the way I've normally used interfaces:
public class Person : IEquatable<Dog>
{
public int Age { get; set; }
public bool Equals(Dog d)
{
if (d.Age == this.Age)
return true;
else
return false;
}
}
public class Dog
{
public int Age { get; set; }
}
Upvotes: 1
Views: 79
Reputation: 27049
The difference is:
What you have done with your Person
class is interface implementation.
MyClass
is not implementing any interface but it is accepting interfaces as constructor parameters. Whoever calls the constructor will provide instances which implement the interface. More to follow below.
Are you allowed to instantiate an interface?
NO. A class which implements the interface will be instantiated.
Where is the implementation of the methods required by these interfaces?
When the person creates a instance of your class, they will pass an instance of a class which implements the interface. Like this:
public interface ITest1
{
string GetData();
}
public class Test1 : ITest1
{
public string GetData()
{
return "Data";
}
}
They will call your class like this:
var test1 = new Test11();
// instead of nulls it will be other instances like test1
var myClass = new MyClass(test1, null, null);
I have not seen interfaces implemented as class members before. Can someone explain what's going on?
What is going on I have explained above with an example.
That is actually a very common way, and yes there are other ways, but this is one of the ways.
Advantages
This way your MyClass
has no idea what concrete type will be passed, but it knows whatever the concrete type is, it will support the interfaces.
Inside MyClass
, you will only have code which depends on the interface so your class is loosely coupled.
Dependency Injection can be used to inject dependencies into MyClass
's constructor. That is called constructor injection.
What's the difference compared to the way I've normally used interfaces:
There is no difference. But you need to think about how people will use the class. For example, if you wanted to use the MyClass
, you will do this which is exactly what you are used to:
public class Test1 : ITest1
{
public string GetData()
{
return "Data";
}
}
To clarify even further, imagine MyClass
had another constructor and field like this:
private readonly IEquatable<Dog> iEq;
public MyClass(IEquatable<Dog> iEq) { this.iEq = iEq; }
Then you would call it with an instance of your Person
class. So it is not any different.
NOTE: No person will be happy being equated to a dog ;)
Upvotes: 3
Reputation: 5332
You cannot instantiate interface, you can replace interface with his inherited type!
This is the regular usage of interfaces (your example):
public class MyClass
{
public MyClass(ITest1 interface1, ITest2 interface2, ITest3 interface3)
{
this.interface1 = interface1;
this.interface2 = interface2;
this.interface3 = interface3;
}
public void TestMethod()
{
var lines = interface1.GetData();
var file = interface2.Parse(lines);
interface3.Copy(file);
}
private readonly ITest1 interface1;
private readonly ITest2 interface2;
private readonly ITest3 interface3;
}
You have created three fields in class MyClass
: interface1, interface2 and interface3.
Through class constructor, you can inject real instances for ITest1
, ITest2
and ITest3
. These instances should be a type of some classes which implemented appropriate interface.
Example:
You can implement interfaces and provide objects like in following example:
public class Test1 : ITest1
{
public string GetData()
{
// code
}
}
public class Test2 : ITest2
{
public string GetData()
{
// code
}
}
public class Test3 : ITest3
{
public string GetData()
{
// code
}
}
Example of creating MyClass
will be:
var test1 = new Test1();
var test2 = new Test2();
var test3 = new Test3();
var myClass = new MyClass(test1, test2, test2);
Objects test1
, test2
and test3
implements interfaces ITest1
, ITest2
and ITest3
.
Conclusion:
There is no magic. Interfaces are replaced with types of their inherited types! Basically, you are providing implementation for abstraction in your code.
Upvotes: 1
Reputation: 1808
You can't instantiate an interface; a derived class will be what is passed to the constructor.
so you would have some classes:
class A : ITest1 { }
class B : ITest2 { }
class C : ITest3 { }
and use them something like this:
var a = new A();
var b = new B();
var c = new C();
var mc = new MyClass(a, b, c);
mc.TestMethod();
Upvotes: 1