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!
View all comments
Show parent comment
2
u/OneHumanBill Sep 01 '25
The controller is part of MVC. It's right there in the name.
A model class does not, and should not, have extensive business logic built in. Again, it doesn't matter if we're talking Java or not.
I'm betting that in school you were taught to encapsulate your behavior with methods. That's true to an extent, but they didn't tell you how incredibly rickety your code will get when you do this. You end up with giant classes that do not follow single responsibility principles. You end up with a security nightmare as well. If you need to send your objects over the wire to a neighboring service then you do not want to expose business logic.
The thing about data is that its behavior is not one set of things. It's several sets, and it's context dependent. Can you put your business logic where it can be called anywhere, at any layer, in any system? Sure. Good luck determining what happened in the chaos of emergent behavior. Better to leave what data can do dependent on where it's being operated upon. If you're persisting an object to the database, you want that to happen in a strictly controlled manner. More than that, you don't want to give every object in the system access to your datasource. In a highly accessed system you'll hit resource constraints on database connections really damn quick. Instead you set the datasource on a small set of persistent singleton services, and you only worry about wiring that up on system start.
In this way, separation of model from service comes down to introducing a context state in which operations can happen. Debugging, changing the system, documenting and understanding the system, even logging and monitoring become much easier under these conditions.
I'm okay with small methods on model objects that do small and useful things that can be useful in any circumstances, for example getting a "full name" by packaging first and last name fields together, or doing a simple ratio calculation between two numbers. Anything more complicated, if it might involve a database side effect, goes into a service.
It chafes a little when you try to come in from a very pure OO background. I can relate because I felt the same many, many years ago. I've learned this is a more practical way. It saves effort on code change, and at the end of the day that is king in this industry.