r/learnjava • • Oct 30 '26

OOP DESIGN

I've been learning Java for about a month now. So far I've covered OOP principles, Java Collections, and I'm planning to dive deeper into exception handling next.

However, one area where I still struggle is OOP design and structuring. I understand concepts like inheritance, encapsulation, abstraction, and polymorphism, but when it comes to applying them in actual projects, I often find myself struggling to distribute them properly among classes.

No matter how many classes I create to separate different parts of the logic, I eventually end up with at least one class containing too many methods and responsibilities.

What are the best practices, concepts, or design principles I should study to improve my OOP design skills? How can I develop the ability to identify when a class is doing too much, decide when to create new classes, and structure a Java application in a clean, maintainable, and scalable way?

14 Upvotes

16 comments sorted by

View all comments

Show parent comment

1

u/SadCar1494 Oct 30 '26

https://github.com/sabinparajuli774-code/BankManagemntSystem

This is what I was talking about. The program does run and I am able to do what I wanted i.e. deposit the money, withdraw, transfer, and add the history, but it is all cluttered in a single class of Menu.

3

u/ITCoder Oct 30 '26 edited Oct 30 '26

For starter, don't use multiple classes in a single public class. Rather have separate public classes. Some classes are just the basic structure, with field and getter setter. These are known as POJO (Plain Old Java Object) or models, they should be in a single package maybe names models.

Your Menu class is doing most of the business operation. These are part of service layer. Better rename / refactor it banking service. Some of its methods can be clubbed together or refactored to a different service class, again inside a services package

The Bank class having main method, drives your application, better rename it to Driver or Main class. It will be outside of all packages.

Note that , if using a Java IDE you just don't rename a class, use refactor option to rename, so that the variable can be renamed at all the places it is used.

1

u/mandradon Oct 30 '26

It also looks like some of the problem is from a lack of planning from the start. 

There's a class for validation of input, which doesn't need to be a separate class in this instance (well, we won't get into writing a class for handling validation and whatnot, especially for a beginner).

There's a few other things that are split out to their own classes that problem should be restored into one class. 

I agree also on splitting things out into discrete files.

I also recommend taking time to clearly plan out your design from the start.  Think through how things are going to work (as much as you can, it won't always work out that way) from the start.  Think through the relationships and it can help you identify where things should be. 

Otherwise what happens is you'll realize you need some functionality and you end up just putting it somewhere.  Then you end up with spaghetti. 

1

u/KillerCodeMonky Oct 30 '26

These are known as POJO (Plain Old Java Object) or models, they should be in a single package maybe names models.

Disagree. Data types are as much part of any domain as the logic that acts on them. They should be in the same package as the rest of the domain.

1

u/ITCoder Oct 30 '26

Yeah, i have seen them both in package domains and models. Tbh i never paid much attention to theoretical aspects of DDD. iirc, one of mapper plugins in maven call them as models. Been some time since i used that tho