+ 2
Is there a reason to define interfaces if nobody will ever use my classes as libraries for their own software?
I know interfaces define what methods need to exist within the class implementing them or the code won't compile otherwise. So, how can I benefit from defining interfaces for my own classes? They would only be useful for me as a reminder of what methods a class must have declared in its body and nothing else? Or are there any other major reason to define and implement interfaces?
2 Antworten
+ 2
In a real life application you never know what changes you will make during the life-cycle of your software. There is the so called "Open/close principle" that says that (for a component) multiple implementations could be created and substituted for each other. For example you need a service for retrieving user information stored in a file. Later the customer might say that you should get users from a database or LDAP. If you created an interface correctly you can just create a new implementation for LDAP and replace the file based service with minimal effort, without having to touch your code base everywhere where the user service is referenced. Hope this helps.
+ 1
if u don't go with the concept of interface ur application will be tightly coupled i.e u will need to change whole program when the client needs some changes ....
so to avoid this changing of whole program we go with concept of loose coupling with the help of interface