Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why classes tend to be defined as interface nowadays?

These 2-3 last years, many projects I see, like Cuyahoga open source C# CMS, tends to define persistent and non persistent classes as Interface. Why? Is there a good reason? TDD? Mocking? A design pattern? ...

like image 365
Afshar Mohebi Avatar asked Jul 19 '10 13:07

Afshar Mohebi


People also ask

Why do we use interface instead of class?

An interface defines common functionality across unrelated classes. For example, all sorts of classes that look nothing like each other may have the need to safely get rid of the resources they use.

What makes a class an interface?

Like a class, an interface defines methods. Unlike a class, an interface never implements methods; instead, classes that implement the interface implement the methods defined by the interface. A class can implement multiple interfaces.

Why do we define interface?

An interface contains definitions for a group of related functionalities that a non-abstract class or a struct must implement. An interface may define static methods, which must have an implementation. An interface may define a default implementation for members.

Why are interfaces so important?

A good User Interface is important because it can turn potential visitors to buyers as it facilitates interaction between the user and your website or application. It does not only focus only on the aesthetics but also maximizes responsiveness, efficiency and accessibility of a website.


1 Answers

The main reason is that this makes techniques like dependency injection easier. This in turn allows for more flexibility in the software and easier reuse and recombination of existing code. Examples for where this is useful include the various forms of unit testing (as you mentioned), but also most other forms of "regular" code reuse.

A simple example:

Say you have a method that calculates emplyoee salaries. As part of its signature, it accepts an object that calculates their benefits, say an instance of BenefitCalculator:

calculateSalary(... BenefitCalculator bc, ...)

Originally, your design has only one class BenefitCalculator. But later, it turns out that you need more than one class, e.g. because different parts of the software are supposed to use different algorithms (maybe to support different countries, or because the algorithm is supposed to be user-configurable...). In that case, rather than bloat the existing implementation of BenefitCalculator, it makes sense to create new class(es), e.g. BenefitCalculatorFrance, or BenefitCalculatorSimple etc.

Now if you use the signature

calculateSalary(... BenefitCalculator bc, ...)

, you are kind of screwed, because you cannot supply different implementations. If however you use

calculateSalary(... IBenefitCalculator bc, ...)

you can just have all classes implement the interface.

This is actually just a special case of "loose coupling": Demand as little as possible from other parts of the code. In this case, don't demand a certain class; instead just demand that certain methods exist, which is just what an Interface does.

like image 138
sleske Avatar answered Sep 27 '22 19:09

sleske