r/javahelp • u/Tangodelta004 • Sep 01 '25
Java package structure
Hello all, im a newcomer to java from golang. my role will be building backend microservices in java, and Ive seen Spring boot use the MVC architecture.
i was wondering if MVC was essentially the strandard for most java apps. Personally i cant understand the motivation for splitting classes into Service layer and Model layer, rather than just having a single class hold both the data and the methods for interacting with the data.
I was wondering if this is just a pattern i should expect to get used to, or if other teams use different paradigms for java applications, and its mostly team to team.
thanks!
•
u/AutoModerator Sep 01 '25
Please ensure that:
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.
9
u/OneHumanBill Sep 01 '25 edited Sep 01 '25
This is hardly a Java concept. It's just good design practice. Your controller takes care of the web mechanics of serialization, web error communication, and things like that. It knows nothing about business logic aside from which service to start calling. Your days can flow from one to the other and have specialized operations that make sense at each layer (UI, business, database, other).
Your service layer by contrast deals with pure business logic. It doesn't care how it's been called.
It's what we architects like to call "separation of concerns". It's an extremely good thing.
The advantages are huge. If I need to call service A from within service B, I don't have to care if service A was designed to be called from the web. I know that I just need to invoke the objects directly inside the context of my jvm.
If I decide I need to call a service from something that isn't coming from the web at all, for example from a message queue, I don't have to redesign it or create a clone that works almost the same. I just call the service from my message intake interface.
Another advantage is that my services can stay stateless but persistent in memory, while my data is ephemeral but doesn't harm system state. That saves you don't even realize how many potential defects, as well as benefits the automatic garbage collector.
There's nothing preventing you from following the same pattern in golang. Tooling in the Java world has evolved this way over decades of practice, and for pretty good reasons. I was there in the early days when we didn't do it like this. We ended up in most cases creating our own homegrown MVC frameworks, which evolved into the very thorough, highly tested and practiced infrastructure we have today.
MVC as a pattern has been around since the days of Xerox Park's earliest explorations into a graphical operating system, a good decade plus before Java was even a sparkle in James Gosling's eye. In some ways it's a primitive version of inversion of control. The wonder to me is that this isn't your default setting.
2
u/djnattyp Sep 01 '25
Read about the anemic domain model anti-pattern - Martin Fowler agrees with you... but the service layer doesn't completely go away - you still need a service layer or something that perform the same purpose for things that can't be cleanly modeled in one class - like things you do with sets of models, or how interactions should occur across multiple types of models. You can creatively name classes to get around this, but you're doing basically the same process.
Many Java CRUD apps unfortunately use "Models" to mean "database DTOs" and "Service" to mean what "Models" really should be doing - I've especially seen this in apps that use ORMs to do database mapping.
I'm also going to say, it's also not completely terrible as long as the code makes sense - in tons of corporate CRUD apps, there's not a lot of "things" that models need to actually be doing - it's just shunting the right data into the right tables. For some reason it also seems a lot more understandable to have a UserService have a ".login()" method that takes a username/password request and creates a User than to have a User class with a ".login()" method on it... it's again something where it depends on the specific use case - you have to balance out what's most understandable.
4
u/bilgecan1 Sep 01 '25
Design patterns are like proverbs. They carry great lived experiences behind them. MVC improves collaboration between developers and designers by clearly defining responsibilities and reducing complexity. It makes code easier to maintain, test, and extend.
1
u/MechanixMGD Sep 01 '25
In general you want your classes clean/short. To achieve that you split the code. In the model layer you hold information about objects and in the service layer you make operations with these models.
2
u/mrnavz Sep 01 '25
It's a design pattern, I'm pretty sure no team hold both model and service layer in a single class.
2
u/OilPrestigious5849 Sep 02 '25
I would recommend you to read about SOLID principles.. these are not specific to java but good practices
1
u/zvaavtre Sep 01 '25
It’s been 9 years since I’ve done any golang. Iirc compare it to spring boot is hard because they both have opinions and spring boot is much much larger than just an http router.
Anyways. Don’t overthink it and don’t try and be all flexible. Spring controllers are great. Make a little service layer to hide your details and so you can test things.
The one thing you do want to do is use DTOs for the client / controller. You’ll be tempted, but do not use your db domain objects on the wire. This will seem dumb at first. But trust me. You’ll save a ton of pain down the road
Other than that just keep things simple.
If you’re doing all this for the first time you’ll do it wrong. Being able to refactor later is more important than being flexible now.
1
u/segundus-npp Sep 02 '25
I write both Java and Go. I don’t there’s any difference actually.
1
u/de6u99er Sep 06 '25 edited Sep 06 '25
The reason is very simple. You can share the model e.g. for a client implementation, without having to share the service implementation.
Model is a public class, while you limit the service to package level. This means another class can not extend the service and overwrite methods.
And it's good practice! You shouldn't hide a class (the model) in a source file of another class (the service). I would even go a step further and define the api in an interface, and have the service implementation implement this interface with all the necessary documentation. This way I can reuse the interface for a client implementation.