1

I read a lot about C# and had my first practical exercises, but I am still a beginner and kind of lost at a certain point of my try understanding an existing, but not finished, MVC-concepted program.

I understand what interfaces are for and how I must implement an interface to a class or another interface to gain acces to its containing members, functions etc, but in the existing code I found another use of interfaces (in the declaration of a class):

  private IViewControl m_viewControl = null;
  private IModelControl m_modelControl = null;

This code doesn't come up in the class, which implemented those two interfaces, but in the class which doesn't implement those two interfaces at all!

So my questions are:

  • How is this usage of interfaces called? It is clearly not the regular implementation of an interface.
  • What kind of possibilities do I get through this way of using an interface?

Thanks a lot!

Bent

Please excuse my english, I'm not a native speaker.


Hey,

thank you all so much for your answers, can't even say which is the best since all answers seem to be helpful! I think I'm starting to get what this is about.

Thanks again!

Bent V.
  • 47
  • 3
  • "I found another use of interfaces (in the declaration of a class)" - The examples you show are not declarations of classes, nor are they declarations of interfaces. They are declarations of variables of types `IViewControl` and `IModelControl`. – mbeckish Oct 16 '13 at 12:52
  • 1
    http://msdn.microsoft.com/en-us/library/vstudio/ms173156.aspx read it and you won't have such questions. BTW these are fields. – Kamil Budziewski Oct 16 '13 at 12:52
  • 1
    "How is this usage of interfaces called?" - These are variables, not methods, so they aren't called. – mbeckish Oct 16 '13 at 12:52
  • its more or less the same as if you would write private IViewControl m_viewControl = new ViewControl() the only difference is that you assign its value later... – MakePeaceGreatAgain Oct 16 '13 at 12:53
  • 1
    Read up on [Polymorphism](http://msdn.microsoft.com/en-us/library/ms173152.aspx). It seems from your question you don't yet understand this concept. – Chris Pickford Oct 16 '13 at 12:57
  • possible duplicate of [C# interfaces - What's the point?](http://stackoverflow.com/questions/6802573/c-sharp-interfaces-whats-the-point) – Bassam Alugili Oct 16 '13 at 12:58
  • Although it's hard to tell without seeing more of the code, this may have something to do with a technique known as Dependency Injection where a concrete implementation of a class isn't stated at the point of use. This allows for flexibility whereby your code can use any class which implements a certain interface. This link explains it well: – James Oct 16 '13 at 13:00
  • 4
    Why the downvotes? Don't you downvoters tell me that you were already born with OO knowledge imprinted in your minds. – Geeky Guy Oct 16 '13 at 13:02

9 Answers9

3

The class which contains these lines

private IViewControl m_viewControl = null;
private IModelControl m_modelControl = null;

Has 2 references to other classes which implement these Interfaces. So to answer your first question, this is not the implementation of an interface, it is the usage of an interface.

To answer your second question: That is exactly why we use interfaces. The class which uses these interfaces does not care about their implementation. In your development process you can write a dummy implementation for one or the other, because you don't need it right now, but you can still run and test the rest of the application.

An other example: Let's image you want to write an application which uses some Database. Put all your database logic behind an interface. In version 1 of your app you might use an SQL Database. Do your classes, which write to the database, know that it is an SQL database? No, and they don't need to. So now you move on and decide you want to use a different database system. You just change the implementation behind the interface and your done.

Hope this helps.

metacircle
  • 2,438
  • 4
  • 25
  • 39
1

These are two variables (actually member variables, which are known as fields, as they are members of an enclosing type).

They can be used to store any item that implements the interface, so you could put anything that implements IViewControl into m_viewControl and anything that implements IModelControl into m_modelControl.

Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
0

It does mean, that the object you can assign to your variable has to have the interface implemented. So it has to be the type of the interface.

mero
  • 1
0

What you see there is called composition. It means that your class has two fields that are instances of those types, not that it is implementing their interfaces.

Let's use cars for an analogy. "Car" is a pretty generic concept, so let's make it the interface. The Toyota someone own is an instance of some class (e.g.: Corolla), which in turn implements the Car interface. The wheels, on the other hand, are fields of the car. The tires in your Corolla may belong to the Pirelli class, which implements the Tire interface. But your car is not a tire - it has tires.

An interface is a way to make a type without any implementation at all, but which cannot be instantiated. You can then have other types implementing that interface, giving it logic - so you have many variations of that interface, each doing something in a different way. The point is that you are making sure that all the implementors of an interface have a set of known method signatures and properties - you may not know how they are implemented, but you can be sure they are there.

If you look at some of the namespaces in C# that have a lot of classes implementing the same interface, you may get a better idea of how they behave. For example, a lot of classes in System.Collections implement the (surprise) ICollection interface. That makes sure that all collections have, for example, a Count property, and a CopyTo method with a known signature.

Community
  • 1
  • 1
Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
0

This type of usage is great to restrict the usage of a particular object, or to write common code that can work on any number of classes. Let's say we have a class called Car that implements an interface called IDriveable:

public class Car : IDriveable

Now, in some other class, we can instantiate a Car object easily, like so:

Car myCar = new Car();

But what if the Car class has several public methods that we don't want to be accessed in this other section? Or we want to write a function that can work on any class that implements the IDriveable interface? We could instead create an instantiation of the IDriveable interface itself, and then assign a Car to it, like so:

IDriveable myDriveable = new Car();

Then, if the following code works on the IDriveable interface, ANY class that implements IDriveable would work fine on it, such as this example:

private void TurnLeft(IDriveable vehicle)

P.S. Your English usage is great!

pineconesundae
  • 332
  • 1
  • 13
0

The important thing about interfaces is that you aren't interested in what they are but what they can do. Consequently in this case you are only interested in the IViewControl elements of whatever object is assigned to that local variable, so it could be of any class that implements IViewControl and very probably that class can do lots of other things as well, but for these purposes the fact that it is an IViewControl is all that we care about.

An example might be that we have a class that is interested in things that can fly, it doesn't care about anything else, so we create an interface called IFlyingThing with an IFlyingThing.Fly() method. Then we can have a Bird, Plane, Butterfly and all kinds of other types that implement IFlyingThing and we can pass it to our class and it will just see IFlyingThing and call IFlyingThing.Fly() which might be Bird.Fly(), or Plane.Fly() on the actual object it has been passed. It doesn't care what the object is, only that it can fly.

Bird might also implement IFeatheredAnimal, plane might implement IHasJetEngines too but our class is only interested in the IFlyingThing interface so it doesn't want or need to know about these.

This helps us to avoid tying our code together too tightly and makes techniques such as Inversion of Control and Mock Objects possible.

As you progress through learning C# you will use interfaces a lot.

glenatron
  • 11,018
  • 13
  • 64
  • 112
0

Suppose you have a class, that you don't develop. You just consume it. You know it can generate some file and return it to you as a filestream. You don't know how it is generated, and you need not. You just know it returns you a filestream, which you then use for your own purpose. In order to implement it, you make a contract with a developer of the class that the class should provide you a method, which should return you a file stream and the name of the method should be ReturnStream, for example. This contract is called an Interface. By the time the developer of the class can change it's logic of file generation. But it would still have the same name ReturnStream and it would still return you a file stream. So you don't have to change anything in your code.

As for your code, you have two objects of IViewControl and IModelControl. You don't develop the model and view. You just consume the logic of other developers, who write the classes with the interface implementation. And you can use them in your code in a way you want. But many developers can create different classes, which implement IViewControl and IModelControl interfaces. And you can use them by simply changing the class instance, which implements the interface.

Alex
  • 8,827
  • 3
  • 42
  • 58
0

Doesn't sound like you've grasped properly how interfaces can be used. Let me enlighten you with a simple example:

class Driver{ 

     // A driver has two cars - they are cars, since they are 
     // of types (classes Bmw and Audi) that implement the interface ICar:
     ICar firstCar = MethodThatReturnsInstanceOfBmw(); 
     ICar secondtCar = MethodThatReturnsInstanceOfAudi(); 

     public void DriveAllCars(){

        // The interface ICar has a method "Start()", which both 
        // cars must therefor implement. The Driver class can call 
        // these methods, because it knows about them from the interface.
        firstCar.Start(); 
        secondCar.Start();
     }
}

The class Driver still does not need to implement ICar - just know about it (have a reference to it), so it knows what it can do with "things" of that type. It can then tell a car to Start(), without giving a rodents rear part about how the engine actually works.

Compare it to the real world: You don't need to be a car to drive, nor do you need to be a mechanic - you just need to know the basics of driving, and those are common to most cars, though engines and other things may differ greatly.

That abstraction and agreement on common functionality, is the purpose of interfaces.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
0

Interface is basically used to implement similar feature among different classes. Interface is also used to create object of class only when it is required via a dependency injection.

eg:

Interface IMyClass{}
Class MyClass1:IMyClass
{
}

and

IMyClass obj;

thus you can register obj with the class that implements IMyClass in one class(Bootstrapper) and inject obj into all the class through constructor or method that required it with out need of initializing it.

thus Interface Prevents unnessecary creation of object thus prevent memory leak and as I mentioned above it helps in implementing same feature among different classes in different way.

Joel
  • 7,401
  • 4
  • 52
  • 58
Dcoder
  • 1