Here is an example that may help you understand the purpose of interfaces.
I often use interfaces to define a "plug-in" archetecture. If I have a
parent application that I want others to be able to write a module for, I
define an interface, (and provide the neccesary documenation for it). Then
others can develope a module (typically a dll) that provides a publicly
instatiatable class that defines my interface.
I have to provide some mechanism whereby they can "register" their module
with my app so that the app has the information it needs to dynamically load
an instance that public class. Once I load this class and place it in a
variable declared to by of my Interface type, I can used it with confidence
because I know it supports all the members I need in order for it to
interact with my app. Below is a simple inteface that I have used in the
past.
/// <summary>
/// This interface is implemented by any class that acts as the public
interface of a
/// Configuration Application Module. It defines the methods and
properties through which
/// the configuration application will comunicate with the module.
/// </summary>
public interface IConfigAppModule
{
/// <summary>
/// This method must be called after instantiating any new object module
prior to invoking
/// any other members of the object except ModuleName,
GetSupportedFileTypes and
/// GetSupportedNewDocumentTypes.
/// </summary>
/// <param name="Host"></param>
/// <returns>Returns true if the module successfully initializes, false
otherwise. A moduel
/// that returns false should be discarded and not used. A module that
returns false is
/// responsible for notifing the user of any errors encountered.</return s>
bool InitModule(IHostConfigApp Host);
/// <summary>
/// Returns a short human readable name of this module.
/// </summary>
string Name { get; }
/// <summary>
/// Provide the host application with a list of file types that this
module is capable of
/// opening with the Open() method.
/// </summary>
/// <returns>Returns a two dementional array of size [n,1] where n is the
number of file
/// types supported. For each file type x [x,0] is the human description
of the file type
/// and [x,1] is the file extention used by the file type. E.g. [0,0] =
"Text Files"
/// [0,1] = ".txt" </returns>
string[,] GetSupportedFileTypes();
/// <summary>
/// Opens a file of one of the types specified by GetSupportedFileTypes.
/// </summary>
/// <param name="FileName"></param>
/// <returns>Returns true if the document was opened, false
otherwise.</returns>
bool OpenFromFile(string FileName);
/// <summary>
/// Provide the host application with a list of Document types that this
module is capable of
/// opening with the OpenNewDocument() method.
/// </summary>
/// <returns>Returns an array of strings where each string is the display
text of the menu entry
/// (or other control) that the user will select to request a new document
of a supported type.
/// This same string is also passed to OpenNewDocument to indicate the
type of document to open.</returns>
string[] GetSupportedNewDocumentTypes();
/// <summary>
/// Opens a new document of the type specified.
/// </summary>
/// <param name="TypeName">The name of the document type to open as
orriginally provided by
/// GetSupportedNewDocumentTypes().</param>
/// <returns>Returns true a document was opened, false
otherwise.</returns>
bool OpenNewDocument(string TypeName);
}
Phil D said:
Hi,
I am new to c# and oop and have a question about interfaces which I hope
someone can help me with.
The book I am using (C# Step by Step) explains how to create and implement
interfaces (in terms of syntax and grammar), but what it doesn't cover is
why I'd use an interface, and the reasons for doing so.
Currently I just can't see what the value of an interface is, or why I'd
ever want to use one.
Could someone either explain this to me simply, or point me to some good
references (web or books) which cover this topic.
Many Thanks
Phil D