C# Interface and why Interface

Chee Hou
7 min readMay 14, 2022

--

Preface

Interface implementation is one of the most important features in C# to implement object oriented programming. However, based on my previous experience in reading online articles (including programming books) for interfaces , most of the time those articles have pointed out how to use interfaces, but never mentioned WHY very clearly.

So, I want to share some of my experience, why the Interface is useful in daily development. (Disclaimer: This is just my own experience in using Interface, I’m not saying it is the only way to achieve it or there only can be achieved by Interface).

Example Study Case: Employee in a company

Consider the following scenario, a company has 3 types of employee: Executive, Manager and C-suite level employee.

Executive has a name, designation and KPI. This is the example class for employee.

Manager has the same thing with the Executive, but they have a extra rights to evaluate Executive level employee only. This is the example class for manager.

A C-Suite level employee has only name and designation, but not KPI. We presume that their KPI is tied to company stock performances or other performances. C-Suite level employees also have the rights to evaluate Manager level employees only, and furthermore, they have the right to terminate any underperforming employee. This is the example class for C-Suite .

Please take note that

  1. Even the Manager and CSuite class has an EvaluateSubordinate method,but they accept different types of arguments.
  2. CSuite has two TerminateExecutive functions, that accept different types of arguments.

Consider that we want to group all the class in one list, and iterate each employee to perform following tasks.

  1. Display their name and designation
  2. Evaluate their KPI by their corresponding supervisor
  3. C-Suite level employee will terminate those have KPI less than 70.

Something like this:

And now in my main program, I’m initializing all my employee.

Then now I immediately encounter my first problem, I cannot group all these employees together , since they share different class types.

Benefit #1 of Interface: Act as Generic Class

So now we reach the first reason if this article why we need Interface, group all the different class together.

Since all different employees have name and designation, I will create my generic Interface that contains these two properties, which this Interface will hold the responsibility to group all employees together. I called it IEmployee

Second, since only Executive and Manager class employees has KPI to be evaluated, so I created an Interface called IEvaluatedEmployee, which only has one property : KPI. Please take note that my IEvaluatedEmployee Interface also Implements IEmployee Interface. This means that whichever class that implements this Interface, will also have the properties of IEmployee (has name and designation).

I created another Interface called IManagementLevelEmployee, which indicates this interface has the right to manage people, aka, evaluates employees for their KPI for our example.

Finally, we knew that only C-Suite has the privilege to terminate employee, so I created an Interface called ICSuite_Privilege, with the terminate employee function.

I’m done with the Interface, so now I will let my concrete class to implements those Interfaces.

For Executive class:

For Manager class:

Please take note that Manager class implements IManagementLevelEmployee and IEvaluatedEmployee together, so this indicates that employees belonging to this class have the right to manage and evaluate other employees, but at the same time will also be managed and evaluated by someone else.

Finally our C-Suite class:

The Class Diagram after all classes implement Interfaces are look like this:

So now in the main program, I can group all three different class, under IEmployee and iterate each to display their info.

The benefit of Interface is not only limited to group related classes together, it also provides the flexibility in writing a function. Considering the terminate function by C-Suite Employee , we only need to write one function TerminateEmployee that accepts arguments with type IEvaluatedEmployee (which cater for Executive and Manager), instead of my above mentioned original two functions to terminate Executive and Manager respectively.

Benefit #2 of Interface: Contact Binding and Class Empowerment

Many people consider Interface as a ‘contract’ between classes. Consider a real life example, a factory owner signs a contract with an investor that the factory will produce 100 shoes in 1 month. Factory MUST produce 100 shoes to the investor, and failing to do so will cause a penalty.

For example, CSuite class has two Interfaces IManagementLevelEmployee and ICSuite_Privilege. This is a ‘Contact’ to force this class must have all the features of these two Interfaces (Evaluate and Terminate Employee), failed to do so, the compiler will return an (penalty in the real world case).

Lets say in future, a new class called “Board” to be introduced, so we can assign the similar privilege to Board class, to ensure they has the power as C-Suite do. With this we can ensure all Board class will have the terminate function. With this feature, a programmer can understand the responsibility of each class quickly, by looking on the Inteface/s each class implemented.

Benefit #3 of Interface: Multiple Inheritance to perform responsibility segregation

You may notice the IManagementLevelEmployee Interface has a function called EvaluateSubordinate, and the parameter it accept is IEvaluatedEmployee instead of IEmployee.

The EvaluateSubordinate function will still works if it accept IEmployee, since Executive and Manager class also implement IEmployee, why I’m using IEvaluatedEmployee instead?

This is because, in our case

  1. C-Suite level also implement IEmployee
  2. But C-Suite level do not evaluated by anyone
  3. And what is the indicator for employee who will be evaluated? For our case, it is they have KPI.

So, in order to fulfill every requirement, I created an Interface called IEvaluatedEmployee, that have KPI property, and at the same time it implements IEmployee. So this means whoever implements this Interface (Executive and Manager), will not only have KPI, but also have all the signatures of IEmployee (name and designation).

But, CSuite class do not implements IEvaluatedEmployee Interface, so for the EvaluateSubordinate function, we can prevent object from CSuite class being pass into and evaluated, since we have set this kind of contact and restriction, by using Interface. This can keep the business logic of our code always consistent.

Benefit #4 of Interface: Impact Analysis

And let say in future, the company’s board director comes out a new policies for the company

  1. C-Suite employee need to be evaluated
  2. Introduce two more parameter a and b for the evaluation

Since my example is a small program, I know where to change my code in a split of seconds without any error. However, in real life the system can be huge and very complex, without Interface it is very difficult to maintain or apply changes.

For the sake of demo, to implement the two company’s new policy, I will

  1. Let the CSuite class implement IEvaluatedEmployee Interface

2. Introduce two more parameters a and b for EvaluateSubordinate in Interface IManagementLevelEmployee

The Visual Studio will immediately give me the error warning that indicates my CSuite does not implement IEvaluatedEmployee (missing property of int KPI ), and my Manager and CSuite class do not correctly implement IManagementLevelEmployee Interface ( EvaluateSubordinate function needs two new parameters).

Imagine without Interfaces, a programmer will not be able to identify all the parts that need to be changed, and only realize it after the system has gone live. So again, Interface can make sure our code is always consistent with the latest business logic.

Benefit #5 Abstraction for Planning

In real life, normally there will be more than one developer to develop the project. And sometimes we have the start developing even before the finalize business requirement. So, with Interfaces, the lead programmer or solution architecture are able to standardize the features of each class. By using the same example, the tech lead might not know what is the exact business logic to evaluate an employee, but by using the Interface, he/she can ensure all programmer that develop Manager or Csuite class must contains EvaluateSubordinate function, and also have Name and Designation for all employee etc, by stated it in the Interface.

Benefit #6 Unit Test

Last but not least, or in fact this might be one of the most important reasons to implement Interface, for Unit Test. This benefit is quite similar with Benefit #5, since a lot of companies applied Test Driven Development (TDD) methodology in their software development process, so during the planning state , unit test setup is very important before the actual development kick start.

Let say there is a function called CheckEmployee() for Executive and Manager class, and this class will login to database and check the info of the employee, before we can terminate it.

But for the Unit Test, we may be unable to connect to the real production database, or not allowed to do so in case we may jeopardize the production data, so for the unit test we will use a mockup feature to assume the login to database and check employee function pass. The Unit Test code will look like this (I’m using Moq framework)

We used the Interface IEvaluatedEmployee to mockup the Executive class, and mock the CheckEmployee function to always return true.

If we do not have Interface , and use the real Executive class to do the mockup, visual studio will return an error. This is because the system cannot override the concrete class function to make it always return true or certain mockup data.

Conclusion

I hope this article can help you to understand the importance of Interface , and you know when and why you should apply it in your programming career.

All the source code can be found via my Github.

--

--

No responses yet