需要帮助理解接口 [英] Need Help understanding Interfaces

查看:131
本文介绍了需要帮助理解接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然无法理解哪些接口是良好的。我读了一些教程,我仍然不知道他们真的是为其他然后他们让你的类守信用和他们帮助与多重继承。

这就是它。我仍然不知道什么时候我甚至可以用一个接口在实际工作中的例子,甚至当确定何时使用它。

从我有限的知识接口,他们可以因为帮助,如果实现的东西,那么你可以只通过接口允许通过像不同的类,而不必担心它不是正确的参数。

但我从来不知道这个真正的问题,因为他们通常是短在这一点上展示它通过接口后,code会做什么,停下来,如果他们那种做,好像他们不这样做做任何有用的,我可以看看去哇,他们将在现实世界的例子帮助。

所以我想我要说的是,我试图找到一个现实世界的例子,我可以看到在行动接口。

我不明白,你可以做一个参考的对象是这样的:

  ICalculator MyInterface的=新JustSomeClass();

所以,现在,如果我会去MyInterface的点和智能就拉起我只能看到接口的方法,而不是在JustSomeClass其他方法。所以,我没有看到一个点,这个还没有。

此外,我开始做单元测试,他们似乎更喜欢使用的接口,但我还是不明白为什么。

比如像这个例子:

 公共AuthenticationController(IFormsAuthentication formsAuth)
{
    FormsAuth = formsAuth?新FormsAuthenticationWrapper();
}公共类FormsAuthenticationWrapper:IFormsAuthentication
{
    公共无效SetAuthCookie(用户名字符串,布尔createPersistentCookie)
    {
        FormsAuthentication.SetAuthCookie(用户名,createPersistentCookie);
    }
    公共无效SignOut()
    {
        FormsAuthentication.SignOut();
    }}公共IFormsAuthentication FormsAuth
{
    得到;
    组;
}

就像为什么还要做这个界面?为什么不只是让FormsAuthenticationWrapper在它的方法和收工?为什么首先请再界面有包装实现了接口,然后最后写的方法呢?

然后,我没有得到什么说法实际上是说。

就像我知道知道这种说法是说这

FormsAuth = formsAuth?新FormsAuthenticationWrapper();

如果formsAuth为null,则创建一个新FormsAuthenticationWrapper,然后将其分配给一个接口的属性。

我想这又回到了为何参考事整点。特别是在这种情况下,因为所有的方法是完全一样的。包装没有任何的接口没有新的方法和我不知道,但是当你这样做的方法是填权(即他们有一个机构),他们没有得到转化为存根,因为这将真正看似毫无意义我(那将是转换回接口)。

然后在测试文件中他们有:

  VAR formsAuthenticationMock =新的模拟< AuthenticationController.IFormsAuthentication>();

因此​​,他们只是通过在FormsAuthentication什么,我猜,使所有的假存根。我猜包装类是用来当程序实际运行,因为它有真正做一些事情的方法(如签署一个人出来)。

但寻找新的模拟(从MOQ)它接受一个类或接口。为什么不再次进行包装类把这些方法,然后在新的模拟呼叫?

但愿不只是让存根吗?

感谢


解决方案

接口定义的合同的。

在你提供的例子中, ?? 运营商只是提供了一个默认值,如果你通过来的构造函数,其实没有什么关系的接口。

什么是更相关的是,你可以用一个实际的 FormsAuthenticationWrapper 的对象,但你也可以实现自己的 IFormsAuthentication 有无关包装类在所有类型。界面告诉你什么方法,你需要实现履行合同性质,并允许编译器来验证你的对象确实兑现了合同(在一定程度上 - 这是简单的兑付名义订立合同,而不是在灵) ,所以你不必使用pre-建 FormsAuthenticationWrapper 如果你不想。你可以建立一个不同的类,它完全不同的工作,但仍然尊敬所需的合同。

在这方面,界面很像正常的继承,有一个重要的区别。在C#中一个类只能从一种类型的继承,但可以实现多个接口。所以接口,让您实现在一个类中的多个合同。一个对象可以的的一个IFormsAuthentication对象的的别的东西,像IEnumerable的。

当你从另一个方向看它

接口是更加有用:他们让你仿佛他们都是相同的治疗许多不同的类型。这方面的一个很好的例子是与各种集合类。借此code样品:<​​/ P>

 无效OutputValues​​(字符串[]值)
{
   的foreach(在值字符串值)
   {
       Console.Writeline(值);
   }
}

此接受一个数组,并将其输出到控制台。现在,应用这种简单的更改使用界面:

 无效OutputValues​​(IEnumerable的&LT;串GT;数值)
{
   的foreach(在值字符串值)
   {
       Console.Writeline(值);
   }
}

这code的还是的以一个数组并输出到控制台。但它也需要一个列表&LT;串&GT; 或其他任何你关心给它一个实现的IEnumerable&LT;串&GT; 。因此,我们已经采取了一个接口,并用它使code的的功能更加强大。

的简单块

另一个很好的例子是ASP.Net成员资格提供程序。你告诉ASP.Net,你孝敬通过实现所需的接口会员合同。现在,你可以轻松地定制内置ASP.Net身份验证使用任何来源,都要归功于接口。数据提供商System.Data命名空间中工作以类似的方式。

最后一点:当我看到有一个默认的包装实现这样一个界面,我认为这是一个有点ANIT图案的,或至少是code气味。这表明,我认为也许界面太复杂,您可能需要来拆分,或考虑使用成分+事件+的代表,而不是派生的组合来完成同样的事情。

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 know 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?

Thanks

解决方案

Interfaces define contracts.

In the example you provide, the ?? operator just provides a default value if you pass null to the constructor and doesn't really have anything to do with interfaces.

What is more relevant is that you might use an actual FormsAuthenticationWrapper object, but you can also implement your own IFormsAuthentication type that has nothing to do with the wrapper class at all. The interface tells you what methods and properties you need to implement to fulfill the contract, and allows the compiler to verify that your object really does honor that contract (to some extent - it's simple to honor a contract in name, but not in spirit), and so you don't have to use the pre-built FormsAuthenticationWrapper if you don't want to. You can build a different class that works completely differently but still honors the required contract.

In this respect interfaces are much like normal inheritance, with one important difference. In C# a class can only inherit from one type but can implement many interfaces. So interfaces allow you to fulfill multiple contracts in one class. An object can be an IFormsAuthentication object and also be something else, like IEnumerable.

Interfaces are even more useful when you look at it from the other direction: they allow you to treat many different types as if they were all the same. A good example of this is with the various collections classes. Take this code sample:

void OutputValues(string[] values)
{
   foreach (string value in values)
   {
       Console.Writeline(value);
   }
}

This accepts an array and outputs it to the console. Now apply this simple change to use an interface:

void OutputValues(IEnumerable<string> values)
{
   foreach (string value in values)
   {
       Console.Writeline(value);
   }
}

This code still takes an array and outputs it to the console. But it also takes a List<string> or anything else you care to give it that implements IEnumerable<string>. So we've taken an interface and used it to make a simple block of code much more powerful.

Another good example is the ASP.Net membership provider. You tell ASP.Net that you honor the membership contract by implementing the required interfaces. Now you can easily customize the built-in ASP.Net authentication to use any source, and all thanks to interfaces. The data providers in the System.Data namespace work in a similar fashion.

One final note: when I see an interface with a "default" wrapper implementation like that, I consider it a bit of an anit-pattern, or at least a code smell. It indicates to me that maybe the interface is too complicated, and you either need to split it apart or consider using some combination of composition + events + delegates rather than derivation to accomplish the same thing.

这篇关于需要帮助理解接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆