Interface is a very common term that we often comes across while writing code, reading article or discussing design aspect etc. but many
times we are not knowing the True Power of it. Here, I’ve tried to redefine it
so as to make it understand better.
A concise definition of Interface can be termed as “Interface is a Contract for a set of Functionality in Abstraction so as to
preserve or enhance the Maintainability, Readability, Extensibility and
Testability of an application through Inheritance and Loose Coupling.”
Let’s see how Interface is required to implement SOLID design principle.
- S: Single Responsibility
Principle (SRP) – Not
relevant for Interface, only for Class.
- O: Open closed Principle
(OSP) –
Can be achieved by implementing Interface which allows inheriting classes
to add more functionality without changing the Interface provided
features.
- L: Liskov substitution
Principle (LSP) – Interface
implementation ensures that child classes are replaceable without any
interventions.
- I: Interface Segregation
Principle (ISP) –
An Interface should have granular level of functionality so that no client
should be enforced to implement a functionality which is not required.
- D: Dependency Inversion
Principle (DIP) – Interface
implementation ensure that high-level module/classes does not depend on
low-level module/classes.
Let’s break all the letters in I-N-T-E-R-F-A-C-E to find its
best representation. I could figure out as:
I - Inheritance (supported multiple)
N - Negligible (Loose) Coupling
T - Testability
E - Extensibility
R - Readability
F - Functionality
A - Abstraction
C - Contract
E - Easy Maintenance
I – Inheritance
(supported multiple)
Inheritance is one of the vital OOPS concepts. Interface
helps us achieve multiple Inheritance through multiple Interface implementation
which is otherwise not supported in .Net Framework. Even if there is a method
with same in name in two Interfaces we can implement both without conflict
through Explicit Interface implementation.
N – Negligible
(Loose) Coupling
Interface implementation helps us achieve Loose Coupling in
our code as we are not dependent on any concrete type implementation and referencing
to an interface only. We are free to change the underlying implemented class in
any stage of SDLC (Software Development Life Cycle) with minimal changes (only
at IoC Type registration).
T – Testability
Interface helps us write Testable code, we can easily “plug and play” by replacing external
Libraries or Dependencies with Fake objects. Often we face difficulties to test
our code because it is associated with or using some external Library or
Dependency which we don’t want to test if we are focused on Unit level testing.
Inducing Fake objects with minimum required output to pass the test is a “Life
Saving” trick in many scenarios. Interface plays a key role in TDD implementation.
E – Extensibility
Interface supports Extensibility by allowing the concrete
class to extend its features by implementing any number of methods beyond what
is defined in Interface contract.
R – Readability
Interface enhances Readability of code by replacing the
unorganized, cluttered code in concrete class with a well-formed “Clean Code”.
F – Functionality
Interface defines a set of functionality which has to be
implemented by implementation class. An interface can contain events, indexers,
methods, and properties declaration which has to be implemented by class
inheriting to it.
A – Abstraction
Abstraction is one of the vital OOPS concepts. Interface
helps us achieve abstraction by defining the related set functionality in an Interface.
C – Contract
Interface helps us defining a contract to ensure that we are
following a single standard and there is no deviation which can in turn result
in a disaster while working on mid or Enterprise level applications. Even if a new
or novice Developer is joining a team, he would be bound to follow the contract
for any implementation. It forces to implement code deliberately.
E – Easy Maintenance
Interface helps us achieve highest Maintainability of our
code through testable, loosely coupled, extensible and clean code.
Please refer the Repository Pattern implementation code at my Github repository which shows how can plug and play various repositories without having a single line code change at our client (UI) side.
Please feel free to post your comments or feedback.
Please refer the Repository Pattern implementation code at my Github repository which shows how can plug and play various repositories without having a single line code change at our client (UI) side.
Please feel free to post your comments or feedback.
