Question about interfaces in C #

Here is my question ...

I work in the telecommunications industry and have a piece of software that provides the best available network for a given service number or site installation address. My company uses a network of a wholesale supplier, and we also have our own network. To evaluate what services a client can receive, I will call the web service to find out what services are available on this telephone exchange and based on the services available, I need to perform some checks against our network or the wholesale network of the provider.

My question is, how can this be modeled using interfaces in C #? The software that I have does not use any interfaces or classes, it is only to satisfy the fact that the code cannot live outside the classes.

I am familiar with the concept of interfaces, at least at the theoretical level, but am not very familiar with the concept of programming interfaces.

What I think is as follows:

Create an IServiceQualification interface that will have a specific operation: void Qualify (). You have two classes, QualifyByNumber and QualifyByAddress, and both of them implement the interface and determine the details of the Qualify operation. I think in the right lines or is there another / better way to approach this problem.

I have read some programming examples for interfaces, but would like this to be used in a working situation.

Comments / suggestions are welcome.

+7
source share
3 answers

I would probably do it a little deeper, but you're on the right track. I will personally create an IServiceQualification using the Qualify method, and then below - an abstract class called ServiceQualification, which will have a Qualify abstract method that any class of qualifiers can implement. This allows you to determine the overall behavior among your qualifiers (there must be a certain amount), while still creating a high-level separation of problems.

Interfaces have a specific purpose, and their proper use allows you to implement the way you want, without your code requiring implementation. So, we can create a service that looks something like this:

public bool ShouldQualify(IServiceQualification qualification) 

And no matter what implementation we send, this method will work. This becomes something that you never have to change or change after it works. In addition, this leads to errors. If someone reports that qualifying at the address does not work, you know exactly where to look.

+1
source

Take a look at the strategy design template. Both the problem and the approach you described sound like a fairly close form.

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

+1
source

You need to think about interfaces in terms of a contract. He points out that a class implements certain function signatures, meaning that the class can call them with known parameters and expect that some kind of object back, what happens in the middle, depends on the interface designer. This loose clutch makes your class system much more flexible (it has nothing to do with keeping surfash key touches)

Here is an example that roughly targets your situation (but will require more modeling).

 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; } } 
+1
source

All Articles