Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about interfaces in C#

Tags:

c#

interface

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#? 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(). Have two classes called QualifyByNumber and QualifyByAddress and both of these implement the interface and define the details of the operation 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.

like image 817
Michael Avatar asked Jul 21 '11 11:07

Michael


3 Answers

I would probably make it go a little bit deeper, but you are on the right track. 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. 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.

like image 173
Fourth Avatar answered Sep 30 '22 08:09

Fourth


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

like image 32
Scott Munro Avatar answered Sep 30 '22 08:09

Scott Munro


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. This loose coupling makes your class system a lot more flexible (it has nothing to do with saving key strokes surfash)

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;
    }
}
like image 27
Paul Sullivan Avatar answered Sep 30 '22 07:09

Paul Sullivan