Photo by Clint Patterson on Unsplash

Why Interface types are important in Software Design?

Sriram Kumar Mannava
2 min readMar 21, 2024

--

An Interface in any object oriented language is a plain template class. It doesn't contain any logic, just method declarations. And the compiler asks us to implement all of these methods, if your class is deriving from this interface.

This is known stuff, but why are they important to a software design?

For two reasons - compliance and security.

Interfaces for Compliance

An Interface is used to define common behavior for unrelated classes. Using this, multiple classes can comply with a single contract even if they serve different purposes.

For example, I can write two different repository classes for two different data sources - one for SQL Server and one for say, Oracle.

Now my Service layer needs to use one of these Repository classes to work with database.

A simple design will be that the Service layer takes an object of a BaseRepository interface and these two data source repositories can derive from this RepositoryBase.

High level modules should not depend on low-level modules. Both should communicate by means of abstractions. - Dependency Inversion Principle.

Interfaces for Security and Portability

Security aspect of an interface comes when you're writing a library that would be shipped and used in some other unknown client components.

If you're writing and exposing concrete types, there's a high chance that your functionality could be exposed.

It's a good design where your library methods return interfaces instead of concrete types. That way any client library that uses these methods can work with any of its derived types without problems.

Interfaces and abstract types are core to a good software design that's robust, portable and clean.

Subscribe to my Newsletter — https://referbruv.substack.com/

--

--

Sriram Kumar Mannava

I make Full Stack Development Easy for You | Full Stack .NET Dev | 3× AWS Certified | Blogger