The main reason for using interfaces is to achieve polymorphism and to provide developers with the ability to implement interface methods in their own way in the future.
In this example I will use TypeScript and I will put the repository at the end of the note
Suppose we have an interface and three classes:
This is the connector interface.
Now we are going to implement the Wifi communication.
Here we have developed our concrete class called WifiConnector which has its own implementation of type Connector.
Now we are creating our System which has a Connector component. This is called dependency injection.
Let's look at this line of code ...
Connector is an interface and must have the method doConnect().
Now, since Connector is an interface, the System class has much more flexibility.
We can pass any type that has implemented the Connector interface.
In the future, the developer will achieve more flexibility.
For example, now the developer wants to add a Bluetooth connection module:
We see that WifiConnector and BluetoothConnector have their own implementation.
There is a different way to connect.
So we can pass any of them to the System class as a constructor parameter. This is called polymorphism.
The System class is now not aware of whether it is Bluetooth or Wifi. We can even add another communication module such as Infrared, Bluetooth5 and whatever, simply by implementing the Connector interface.
This is called Duck Typing. The connector type is now dynamic since doConnect() is just a placeholder and the developer implements it as their own.
Here is the repository in case you need it and thanks for reading!
Top comments (0)