简体   繁体   English

C#中的接口问题

[英]Question about interfaces in C#

Here is my question...这是我的问题...

I work in Telecom industry and have a piece of software which provides the best network available for a given service number or a site installation address.我在电信行业工作,拥有一款软件,可为给定的服务号码或站点安装地址提供最佳网络。 My company uses the network of the wholesale provider and we have our own network as well.我公司使用批发商的网络,我们也有自己的网络。 To assess what services a customer might be able to get, I call a webservice to find out the services available on a given telephone exchange and based on the services available, I need to run some checks against either our network or the network of the wholesale provider.为了评估客户可以获得哪些服务,我调用了一个网络服务来找出给定电话交换机上可用的服务,并且根据可用的服务,我需要对我们的网络或批发网络进行一些检查提供者。

My question is how this can be modelled using interfaces in C#?我的问题是如何使用 C# 中的接口对其进行建模? The software that I have does not make use of any interfaces and whatever classes are there are just to satisfy the fact that code cannot live outside classes.我拥有的软件没有使用任何接口和任何类,只是为了满足代码不能存在于类之外的事实。

I am familiar with the concept of interfaces, at least on theoretical level, but not very familiar with the concept of programming to interfaces.我熟悉接口的概念,至少在理论上是这样,但对接口编程的概念不是很熟悉。

What I am thinking is along the following lines:我的想法大致如下:

Create an interface called IServiceQualification which will have an operation defined: void Qualify().创建一个名为 IServiceQualification 的接口,该接口将定义一个操作:void Qualify()。 Have two classes called QualifyByNumber and QualifyByAddress and both of these implement the interface and define the details of the operation Qualify.有两个名为 QualifyByNumber 和 QualifyByAddress 的类,它们都实现了接口并定义了操作 Qualify 的细节。 Am I thinking along the right lines or is there a different/better way of approaching this issue.我是按照正确的思路思考还是有解决这个问题的不同/更好的方法。

I have read a few examples of programming to interfaces, but would like to see this utilized in a work situation.我已经阅读了一些接口编程示例,但希望看到它在工作环境中得到应用。

Comments/suggestions are most welcome.评论/建议是最受欢迎的。

I would probably make it go a little bit deeper, but you are on the right track.我可能会让它 go 更深一点,但你在正确的轨道上。 I would personally create IServiceQualification with a Qualify method and then below that an abstract class called ServiceQualification which would have an abstract method called Qualify that any kind of qualifier class could implement.我会亲自使用 Qualify 方法创建 IServiceQualification,然后在其下方创建一个名为 ServiceQualification 的抽象 class ,它将有一个名为 Qualify 的抽象方法,任何类型的限定符 class 都可以实现。 This lets you define common behavior among your qualifiers (there is bound to be some) while still creating the separation of concerns at a high level.这使您可以定义限定符之间的共同行为(肯定会有一些),同时仍然在高层次上创建关注点分离。

Interfaces have a defined purpose and using them properly lets you implement in any way you want without having your code require that implementation.接口具有明确的用途,正确使用它们可以让您以任何您想要的方式实现,而无需让您的代码需要该实现。 So, we can create a service that looks something like:所以,我们可以创建一个看起来像这样的服务:

public bool ShouldQualify(IServiceQualification qualification)

And no matter the implementation we send it, this method will work.无论我们发送什么实现,这个方法都可以工作。 It becomes something you never have to change or modify once its working.一旦它工作,它就变成了你永远不必改变或修改的东西。 Additionally, it leads you directly to bugs.此外,它会直接导致您遇到错误。 If someone reports that qualifications by address aren't working, you know EXACTLY where to look.如果有人报告说地址资格不起作用,您确切地知道在哪里寻找。

Take a look at the strategy design pattern.看一下策略设计模式。 Both the problem and the approach that you have described sound like a pretty close fit.您描述的问题和方法听起来都非常合适。

http://www.dofactory.com/Patterns/PatternStrategy.aspx http://www.dofactory.com/Patterns/PatternStrategy.aspx

You should think of interfaces in terms of a contract.您应该根据合同来考虑接口。 It specifies that a class implements certain function signatures meaning you class can call them with known parameters and expect a certain object back - what happens in the middle is upto the developer of the interface to decide. It specifies that a class implements certain function signatures meaning you class can call them with known parameters and expect a certain object back - what happens in the middle is upto the developer of the interface to decide. This loose coupling makes your class system a lot more flexible (it has nothing to do with saving key strokes surfash)这种松耦合使您的 class 系统更加灵活(它与节省击键冲浪无关)

Heres an example which is roughly aimed at your situation (but will require more modelling).这是一个大致针对您的情况的示例(但需要更多建模)。

public interface IServiceQualification{
    bool Qualifies(Service serv);
}

public class ClientTelephoneService : IServiceQualification
{
    public bool Qualifies(Service serv){
       return serv.TelNumber.Contains("01234");
    }
}

public class ClientAddressService : IServiceQualification
{
    public bool Qualifies(Service serv){
       return serv.Address.Contains("ABC");
    }
}

//just a dummy service
public class Service{
    public string TelNumber = "0123456789";
    public string Address = "ABC";
}

//implementation of a checker which has a list of available services and takes a client who implements the
//interface (meaning we know we can call the Qualifies method
public class ClassThatReturnsTheAvailableServices
{
    //ctor
    List<Service> services = //your list of all services

    public List<Service> CheckServices(IServiceQualification clientServiceDetails)
    {
        var servicesThatQualify = new List<Service>();
        foreach(var service in services){
            if(clientServiceDetails.Qualifies(service)){
                 services.Add(service);
            }
        }
        return servicesThatQualify;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM