یک مثال کاربردی از کاری که Interface انجام میده بنویسید (تواناییش و اینکه چرا باید از اون استفاده کرد)
من که مثال زدم!
اگه نمونه کد هم می خواهید به لینک دومی که توی قسمت اینترفیس دادم توجه کنید:
این یک اینترقیس هست برای استفاده در کلاسهایی که کار Input رو انجام می دهند:
//The interface IInput is responsible handling string information.
public interface IInput
{
// Returns a string value from a service
string NotifyOutput();
}
این یک string رو به شما برمی گردونه همونطوری که می بینید پیاده سازی نداره و فقط تعریف داره.
حالا اینها کلاسهایی هستند که از این قرارداد(اینترفیس) استفاده می کنند:
// XMLInput reads some elements from an xml file.
// XMLInput is a subtype of the interface IInput.
public class XMLInput : IInput
{
// An empty constructor
public XMLInput() {}
// Implementation of interface
public string NotifyOutput()
{
return ReadXML();
}
// Simple read of an XML file
public string ReadXML()
{
string str = "";
try {
XmlDocument doc = new XmlDocument();
string xmlFile = System.Windows.Forms.Application.StartupPath +
"\\input.xml";
if (System.IO.File.Exists(xmlFile)){
doc.Load(@xmlFile);
XmlNodeList node1 = doc.GetElementsByTagName("node2");
foreach(XmlNode node2 in node1){
str += node2.ChildNodes[0].FirstChild.Value;
}
}
else {
Console.WriteLine("Missing the input.xml from" +
" application directory.");
}
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
return str;
}
}
// INIInput reads some elements from a simple ini text file.
// INIInput is a subtype of the interface IInput.
public class INIInput : IInput
{
// An empty constructor
public INIInput() {}
// Implementation of interface
public string NotifyOutput()
{
return ReadIni();
}
private string ReadIni()
{
string str = "";
string iniFile = System.Windows.Forms.Application.StartupPath +
"\\input.ini";
try {
if (System.IO.File.Exists(iniFile)){
using (StreamReader sr = new StreamReader(iniFile)) {
String line;
while ((line = sr.ReadLine()) != null) {
str += line;
}
}
}
else {
Console.WriteLine("Missing the input.ini " +
"from application directory.");
}
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
return str;
}
}
// BinInput reads a string from a string object in a binary file.
// BinInput is a subtype of the interface IInput.
public class BinInput : IInput
{
public BinInput() { }
// Implementation of interface
public string NotifyOutput()
{
return ReadBin();
}
public string ReadBin()
{
AStringClass SomeStringClass = null;
string binFile = System.Windows.Forms.Application.StartupPath +
"\\input.bin";
try {
if (File.Exists(binFile)){
IFormatter formatter = new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
Stream stream = new FileStream(binFile, FileMode.Open,
FileAccess.Read, FileShare.Read);
SomeStringClass = (AStringClass) formatter.Deserialize(stream);
stream.Close();
}
else {
Console.WriteLine("Missing the input.bin" +
" from application directory.");
}
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
return SomeStringClass.SomeString;
}
}
// We need this class to cast the serialized objects from the stream,
// when we read the input.bin file. Is also used to
// create the binary file.
// The create of the binary file is not used in this sample instead the
// input.bin file is coming with the source code.
[Serializable]
public class AStringClass
{
public string SomeString;
public AStringClass()
{
this.SomeString = "Text from a bin file";
string binFile = System.Windows.Forms.Application.StartupPath +
"\\input.bin";
SaveToBinaryFile(binFile);
}
public void SaveToBinaryFile(string binFile){
try {
IFormatter formatter = new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
Stream stream = new FileStream(binFile, FileMode.OpenOrCreate,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, this);
stream.Close();
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
}
این 3 کلاس هر کدوم یک نوعی از ورودی رو می گیرند(xml-ini-bin) و هر سه تاشون استرینگ NotifyOutput رو به نوعی مقدار دهی می کنند.
متد ها و پراپرتی هایی که توسط اینترفیس تعریف می شن. باید توسط کلاسهایی که توی اون قرارداد هستند حتما پیاده سازی بشن.
توی مثال بالا نیاز بود که سه کلاس BinInput و INIInput و XMLInput هر سه شون پراپرتی ای داشته باشن که نتیجه خوندن رو توی اون نشون بدن. پس ما یک اینترفیس به نام IInput ایجاد کردیم و یه پراپرتی NotifyOutput توش تعریف کردیم و گفتیم که همه ی این کلاسها باید اون رو ایمپلیمنت کنند(به اصطلاح باید این قرارداد رو اجرا کنند).
یک مثال خوب هم توی کتاب C# omid_ahmadi عزیز هست می تونید مطالعه کنید.