排序算法接口 [英] Interface for sorting algorithm

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

问题描述


请让我知道使用接口有何不同....
一个简单的问题,接口对任何类型的对象的排序算法有多大的帮助.....

Hi,
Please let me know how the use of Interface makes difference....
one quick question how really Interface is useful for a sorting algorithm for any type of object.....

推荐答案

针对接口进行编程可轻松替换实现晚些时候.该接口定义了实现类和使用该类的代码之间的约定.想要使用接口的实现者的代码甚至可以完全忽略实现者的具体类型.
我喜欢将接口与依赖注入(也称为IoC(控制转换))结合使用.需要使用接口IxxxYYY的实现者的类A声明一个属性或类型为IxxxYYY的构造函数参数,并且当我的IoC容器实例化类A时,可以插入满足IxxxYYY约定的任何类.
关于将接口用于排序算法的问题,我想说,在这里拥有一个接口将使以后更容易以更好,更快,更高级的实现交换排序算法.

还有问题吗?给我留言.

问候,
Manfred
Programming against an interface makes it easy to replace the implementation at a later time. The interface defines the contract between the implementing class and the code that uses the class. The code that wants to use an implementor of an interface can even be totally oblivious of the concrete type of the implementor.
I like to use interfaces in combination with dependancy injection (also known as IoC (inversion of contgrol)). The class A that needs to use an implementor of interface IxxxYYY declares a property or maybe a constructor parameter of type IxxxYYY and when my IoC container instantiates class A any class that satisfies the IxxxYYY contract can be plugged in.

In regard to your question of using an interface for a sorting algorithm I''d say that having an interface here would make it easier at a later date to exchange the sorting algorithm with a better, faster, more advanced implementation.

Still have questions? Leave me a comment.

Regards,
Manfred


接口将成为排序算法的定义-它将定义排序的基础.它不会包含任何排序算法(也不会为您进行排序).

基本上,您将创建您的接口并使用该接口实现排序算法A.
明天,您可能会发现排序算法B优于A,因此您将使用相同的接口实现排序算法B,因此需要更改实现排序的代码.

因此,没有任何一个界面并不能真正帮助您进行排序.这将为您提供一种扩展机制,以便以后更改您的排序算法.

干杯
An Interface is going to be definition for your sorting algorithm - it is going to define the basis for sorting. It is not going to contain any sorting algorithm (nor is it going to do the sorting for you).

Basically you will create your interface and implement sorting algorithm A using this interface.
Tomorrow, you may find sorting algorithm B to be better than A, and so you are going to implement sorting algorithm B, using the same interface so that the code implementing the sorting need to change.

So, no an interface is not really going to help you in sorting. It is going to give you an extensibility mechanism for changing your sort algorithm later.

Cheers


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

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