Interface-Based Programming - Explicit Interface Implementation
(Page 3 of 4 )
The way of implementing an interface shown in the previous section is called implicit interface implementation, because a public method with a name and signature that match those of an interface method is implicitly assumed to be an implementation of that interface method.
Example 3-3 demonstrates a simple technique that allows server developers to enforce the separation of the interface from the implementation. The server implementing the interface can actually prevent clients from accessing the interface methods directly by using explicit interface implementation. Implementing an interface explicitly means qualifying each interface member name with the name of the interface that defines it.
Example 3-3. Explicitly implementing an interface
public interface IMyInterface
{
void Method1();
void Method2();
}
public class MyClass : IMyInterface
{
void IMyInterface.Method1()
{...}
void IMyInterface.Method2()
{...}
//Other methods and members
}
Note that the interface members must be implicitly defined as private at the class’s scope; you can’t use any explicit access modifiers on them, includingprivate. The only way clients can invoke the methods of explicitly implemented interfaces is by accessing them via the interface:
IMyInterface obj;
obj = new MyClass();
obj.Method1();
To explicitly implement an interface in Visual Basic 2005, you need to explicitly set the method access toPrivate, as in Example 3-4.
Example 3-4. Explicitly implementing an interface in Visual Basic 2005
Public Interface IMyInterface
Sub Method1()
Sub Method2()
End Interface
Public Class SomeClass
Implements IMyInterface
Private Sub Method1() Implements IMyInterface.Method1
...
End Sub
Private Sub Method2() Implements IMyInterface.Method2
...
End Sub
End Class
You should avoid mixing and matching explicit and implicit interface implementations, as in the following fragment:
//Avoid mixing and matching:
public interface IMyInterface
{
void Method1();
void Method2();
}
public class MyClass : IMyInterface
{
void IMyInterface.Method1()
{...}
public void Method2()
{...}
//Other methods and members
}
Although .NET lets you mix and match implementation methods, for consistency, you should avoid it. Such mix and match forces the client to adjust its references depending on whether a particular method is accessible via an interface or directly via the object.
Assemblies with Interfaces Only
Because interfaces can be implemented by multiple components, it’s good practice to put them in a separate assembly from that of the implementing components. Maintaining a separate assembly that contains only interfaces allows concurrent development of the server and the client, once the two parties have agreed on the interfaces. Such assemblies also extend the separation of interface from implementation to the code-packaging units.
Next: Working with Interfaces >>
More .NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter three of Programming .NET Components, Second Edition, written by Juval Lowy (O'Reilly, 2006; ISBN: 0596007620). Check it out today at your favorite bookstore. Buy this book now.
|
|