Access to objects through their interfaces

What does it mean? I am reading a book of design templates. It says: objects are accessible only through their interfaces , and I can’t circle it around myself, can any body give me an example (really appreciate it in C #)

What do we really achieve using it?

thank

+4
source share
5 answers

It really depends. If the variable is of type "interface", then in this case only the type of interface can be accessed by the object.

Let's look at an example - suppose I have an interface as defined below -

interface IMyInterface
{
   string B();
}

and if I implement this interface using the "MyClass" class as shown below -

public class MyClass:IMyInterface
   {
      public string B()
      { 
           return "In Class";
      }
   }

   public class MyAnotherClass:IMyInterface
       {
          public string B()
          { 
               return "In Another Class";
          }
       }

, .

   IMyInterface myinst = new MyClass();

B(), myinst, MyClass.

, , , IMyInterface, -

    public class UseOfInterface{
    public void InterfaceUse(IMyInterface myPara)
    {
      myPara.B();
    }
}

, -

        IMyInterface myInst = new MyClass();
        IMyInterface myAnotherInst = new MyAnotherClass();

        UseOfInterface interfaceUse = new UseOfInterface();
        interfaceUse.InterfaceUse(myInst); // returns "In Class"
        interfaceUse.InterfaceUse(myAnotherInst); // returns "In Another Class"

, , , Interface.

MyClass, MyClass, -

   MyClass myinst = new MyClass();

B() , MyClass. , , .

: ?

, , #. , .

, - "" , ,

namespace CarNameSpace
{
   public class Car()
   {
      public void Drive(IDriver driver)
      {
         if(driver.Age > 18)
         {
            driver.Drive();
         }
      }
   }
}

, , IDriver, , ,

interface IDriver
{
   string Age{get; set;}
   string Name {get set;}
   string Drive()
}

, , IDriver , , , , ,

public class PerfectDriver:IDriver
{
   public PerfectDriver()
   {
     Name = "Vikram";
     Age = 30;
   }
   public int Age{get; set;}
   public string Name {get; set;}
   public string Drive()
   {
      return "Drive perfectly";
   }
}

,

  PerfectDriver perf = new PerfectDriver
  Car myCar = Car();  
  myCar.Driver(perf);
+1

Espson, IPrinter, .

IPrinter printer = new Espson();

Epson , IPrinter, . , , , IPrinter, Print

, PrintDocument(IPrinter printer), , , , Print

+2

, . , ( # ).

(, ).

/ . , interface #.

+2

, . ( , ) , , , .

, . "INamed" , , , "Name", .

public interface INamed{
    string Name{get;}
}

public class Person : INamed{
    public string Name{get;set;}
}

public class Place : INamed{
    public string Name{get;set;}
}

public class Thing : INamed{
    public string Name{get;set;}
}

... , .

static void PrintName(INamed namedObject){
    Console.WriteLine(namedObject.Name);
}

"PrintName" , . " " , , , , .

, , IDbCommand, SqlCommand, , , .

, , , .

0

, . , , , .

, #, , , , . , , Scala, , # , . , .. public interface <interface name>, , , .

, #:

public class MyType
{
  public void Method1()
  {
    ...
  }

  private void Method2()
  {
    ...
  }

  public int Method3()
  {
    ...
  }
}

Then the interface through which I interact with a class - two methods that it provides, void Method1and int Method2and implicit no-argument constructor.

Now suppose I define the following interfaces and class:

public interface IInterface1
{
    void Method1();
}

public interface IInterface2
{
    int Method3();
}

public class MyType2 : IInterface1, IInterface2
{
  public void Method1()
  {
    ...
  }

  private void ADifferentMethod()
  {
    ...
  }

  public int Method3()
  {
    ...
  }
}

The interface through which the user interacts with instances MyType2is the same as the interface through which the user interacts with instances MyType1(with the exception of different constructors), because the signatures of public methods (and other public members) are identical: void Method1and int Method3.

0
source

All Articles