Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Interfaces

I am still having trouble understanding what interfaces are good for. I read a few tutorials and I still don't know what they really are for other then "they make your classes keep promises" and "they help with multiple inheritance".

Thats about it. I still don't know when I would even use an interface in a real work example or even when to identify when to use it.

From my limited knowledge of interfaces they can help because if something implements it then you can just pass the interface in allowing to pass in like different classes without worrying about it not being the right parameter.

But I never know what the real point of this since they usually stop short at this point from showing what the code would do after it passes the interface and if they sort of do it it seems like they don't do anything useful that I could look at and go "wow they would help in a real world example".

So what I guess I am saying is I am trying to find a real world example where I can see interfaces in action.

I also don't understand that you can do like a reference to an object like this:

ICalculator myInterface = new JustSomeClass();

So now if I would go myInterface dot and intellisense would pull up I would only see the interface methods and not the other methods in JustSomeClass. So I don't see a point to this yet.

Also I started to do unit testing where they seem to love to use interfaces but I still don't understand why.

Like for instance this example:

public AuthenticationController(IFormsAuthentication formsAuth)
{
    FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
}

public class FormsAuthenticationWrapper : IFormsAuthentication
{
    public void SetAuthCookie(string userName, bool createPersistentCookie)
    {
        FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
    }
    public void SignOut()
    {
        FormsAuthentication.SignOut();
    }
}

public IFormsAuthentication FormsAuth
{
    get;
    set;
}

Like why bother making this interface? Why not just make FormsAuthenticationWrapper with the methods in it and call it a day? Why First make an interface then have the Wrapper implement the interface and then finally write the methods?

Then I don't get what the statement is really saying.

Like I now know that the statement is saying this

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

if formsAuth is null then make a new FormsAuthenticationWrapper and then assign it to the property that is an Interface.

I guess it goes back to the whole point of why the reference thing. Especially in this case since all the methods are exactly the same. The Wrapper does not have any new methods that the interface does not have and I am not sure but when you do this the methods are filled right(ie they have a body) they don't get converted to stubs because that would really seem pointless to me(it it would be converted back to an interface).

Then in the testing file they have:

var formsAuthenticationMock = new Mock<AuthenticationController.IFormsAuthentication>();

So they just pass in the FormsAuthentication what I am guessing makes all the fake stubs. I am guessing the wrapper class is used when the program is actually running since it has real methods that do something(like sign a person out).

But looking at new Mock(from moq) it accepts a class or an interface. Why not just again made the wrapper class put those methods in and then in the new Mock call that?

Would that not just make the stubs for you?

like image 520
chobo2 Avatar asked Jun 25 '09 04:06

chobo2


1 Answers

Ok, I had a hard time understanding too at first, so don't worry about it.

Think about this, if you have a class, that lets say is a video game character.

public class Character
{
}

Now say I want to have the Character have a weapon. I could use an interface to determin the methods required by a weapon:

interface IWeapon
{
    public Use();
}

So lets give the Character a weapon:

public class Character
{
    IWeapon weapon;

    public void GiveWeapon(IWeapon weapon)
    {
        this.weapon = weapon;
    }

    public void UseWeapon()
    {
        weapon.Use();
    }
}

Now we can create weapons that use the IWeapon interface and we can give them to any character class and that class can use the item.

public class Gun : IWeapon
{
    public void Use()
    {
        Console.Writeline("Weapon Fired");
    }
}

Then you can stick it together:

Character bob = new character();
Gun pistol = new Gun();
bob.GiveWeapon(pistol);
bob.UseWeapon();

Now this is a general example, but it gives a lot of power. You can read about this more if you look up the Strategy Pattern.

like image 133
Coding Monkey Avatar answered Sep 17 '22 19:09

Coding Monkey