Wednesday, 15 February 2012

Can we achive dynamic loading with the hlep of Interface?

Yes,we can achive the dynamic loading(runtime/on-fly) with the help of interface based implementation.

Ex:
      interface ICommoanOperations
    {
          string ExportData();
    }
the above Interface is used to Exprort data from different ways(pdf,word,excel..ect) with the help of different derived classes.


class ExporttoPdf ICommoanOperations
    {
        public string ExportData()
        {
            return "pdf";  // returns the data in the format of pdf
        }
    }
as well as I want to return the data in Excel format

 class ExporttoWord:ICommoanOperations
    {      
        public string ExportData()
        {
            return "excel";   // returns the data in the format of word
        }  
    }

so till we findout only one objective is  Standardaization. and now we will see dynamic/runtime/on-fly exectation.

 class SendData
    {
        public string Export(ICommoanOperations comrefvariable)
         {  // reference variable can hold both objects(any object of class which implements  Icomm interface                              
            string msg = comrefvariable.ExportData();
            return msg;
        }
        static void Main()
        {
            SendData sendobj = new SendData();
            ExporttoWord eobj = new ExporttoWord()
            string strobj = sendobj.Export(eobj);  // now data is returns as Word format.
            Console.WriteLine(strobj);
            Console.Read();                  
        }
    }
Conculsion : If we send object(of word/ of pdf) to Export method as per our object type it will return the the type of data.

No comments:

Post a Comment