r/javahelp • u/alishahidi • Aug 05 '24
Unsolved Is really important to create Interface and impl for every service we have?
Hello
I am confused to create the interface for each service I have
For example, I have a service to call a rest api, someone told me that first you should create an interface for the service and create an impl for the class, but why?
We have only one class and no polymorphism
this creation interface for every service not related for Interface Segregation Principle in solid?
12
u/infz90 Aug 05 '24
We have only one class and no polymorphism
Then no, you don't need interfaces. Some people just get taught these design patterns and then always implement them, whether they are needed or not.
If you only have one service, and never plan to have more than one. Then in my opinion the only benefit the interface gives, is a nice easy place to see the concrete methods that are implemented.
1
u/alishahidi Aug 05 '24
But what does Interface Segregation Principle mean in solid principles?
2
Aug 05 '24 edited Apr 17 '26
Redact cleaned up all of my comments. Bulk deletion and editing is a feature supported to make sure that AI scrapers can't access my data for training.
bright absorbed shy fact rainstorm instinctive scale groovy repeat dog
2
u/Bibliophile5 Aug 05 '24
Your question loosely pertains to the D in SOLID which is Dependency Inversion and to use abstractions.
Interface Segregation is where you split an interface into multiples.
14
u/amfa Aug 05 '24
No.
Just do your implementation.
If you need an interface in the future modern IDEs will help you to extract it.
6
u/JudgmentOld4975 Aug 05 '24
if you need to implement a new class, you can always extract the interface from the class, in IDE is usually even a built-in mechanism for this
3
u/glowinghands Aug 05 '24
There can be benefits not in the programming, but in the software engineering. Others have mentioned unit testing, which was a valid reason once upon a time. Another is if you're using behavior driven development, someone might provide you with a series of interfaces. It can also facilitate porting to a different platform or language down the road.
In general I would avoid it. It's probably not worth the extra hassle.
4
u/Jussins Aug 05 '24
Simple answer. no.
Most experienced developers I know don’t follow SOLID.
If you only have one implementation, use a concrete class unless you have a very specific plan to create more in the future. Also, if the only name you can think of for the class is to add Impl to the end of it, then there’s probably little value in creating an interface.
There was a time, before modern testing frameworks, where an interface was required for adequate testing without using an implementation class. Modern mocking utilities render that point mostly moot.
Also, I typically repeat functionality at least 3 times before extracting a common method. It helps to visualize how code can be used by different callers and enables you to pick and choose the best parts of each implementation.
The key here is that you have to be willing to, and follow through with a, refactor of code when it becomes appropriate; otherwise, it can become quite messy over time.
2
u/0xFatWhiteMan Aug 05 '24
It seems unnecessary, maybe it is, but helps to think about the design and testing
1
u/Dense_Age_1795 Aug 06 '24
you only need to create interfaces when you are separating layers, by instance, domain layer from infrastructure, application layer from infrastructure, etc...
if you don't need it, don't do it.
1
u/Andy76b Aug 07 '24
The approach I can suggest, make your simplest thing that works, refactor when needed.
Trying to apply everythong everytime can cause overengineering and paralisys
0
u/stefanosd Aug 05 '24
For me there are 2 reasons why to create an interface and impl, otherwise I just annotate the implementation directly without interace.
I have multiple implementations of the service and I get to decide which one to instantiate a bean on based on configuration or any other Conditional.
I am using spring modulith and I am creating a public API interface for a module, while keeping the implementation internal to the module. Although it's not necessary it helps me keep the module interface cleaner and documented.
0
Aug 05 '24
[deleted]
3
u/VirtualAgentsAreDumb Aug 05 '24
No modern mocking framework like mikito allow you to mock concrete classes.
I think you dropped this: w
That single missing letter completely changes the meaning of this sentence.
Also, Mockito. :)
0
Aug 05 '24
[deleted]
1
u/VirtualAgentsAreDumb Aug 05 '24
Ah. It was a dot missing, not a w (I thought it meant “Now modern frameworks …”). 😁
•
u/AutoModerator Aug 05 '24
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.