All Questions
Tagged with javaobject-oriented-design
250 questions
5votes
5answers
910views
Handling class specific behavior: polymorphism vs instanceof
I'm designing a system with different types of components, and I'm trying to decide whether to use polymorphism or instanceof checks for handling type-specific behavior. I see two possible approaches: ...
3votes
4answers
2kviews
Avoiding instanceofs with GUI composites
I have a stack of custom Row widgets. Each of them is a label with some editComponent (often a JTextComponent). Rows are aware of their previous and next siblings (if a Row has no such sibling, the ...
2votes
3answers
951views
Is breaking encapsulation a necessary compromise for serialization?
I've been considering the way to solve this problem for a while, and I'm currently stuck between two options I both feel are suboptimal. Here's an abstract example of what I'm dealing with: I have a ...
2votes
6answers
939views
Is it ok to assert on the behavior of return values of a testable class?
So I have a dialog for generating a random password. The user can set min, max, character categories (upper, lower, etc.). What the user basically does is configuring a StringGenerator that does the ...
0votes
5answers
616views
Is it bad to pass builders as constructor arguments?
Note. It's a "spin-off" from my previous question. Not a duplicate — it focuses on a different topic I got to know builders from Bloch's Effective Java. However, I made two changes to his ...
1vote
3answers
418views
Should everything be buildable?
You have some class that performs a certain job public class MyClass { public MyClass() { // ... } // ... } Then, a spec change comes around. The responsibility of the class is the ...
2votes
2answers
276views
Is it ok to extend utilities?
Apache Commons has StringUtils. It's great, but I wish it had a shuffle() method or similar Is it ok to create my own StringUtils that extends Apache's StringUtils and adds the method (Apache's class ...
0votes
3answers
151views
Exposing dependencies results in "fat" constructor. What should you do next?
You take a non-testable class with a lot of static dependencies and expose, and expose until they are all explicitly declared in a constructor But halfway through that nice plan, you notice your ...
1vote
2answers
221views
Abstraction for user notification
We have a desktop Swing application. It executes operations against a DB. It could be plain DML, it could be procedures. In both cases, it could result in an error. In that case, we need to display a ...
6votes
4answers
1kviews
How to avoid init methods when 2 objects need the reference of each other?
According to https://softwareengineering.stackexchange.com/a/334994/432039, I know init is a code smell and should be avoided, and one of the solutions is to use a builder to hold the state first ...
1vote
2answers
127views
How do I reduce number of FieldValidator derivations?
I am trying to write RSQL Parser which checks if the RSQL is logically correct. while the RSQL Java library checks whether the RSQL expression is grammatically correct, it doesn't check if the ...
19votes
6answers
8kviews
Is utilizing a singleton for a cache an antipattern?
I'm currently writing an MVC application and I need a class that can: A: get, add and remove data(specifically a TreeSet of sorted strings that I want stored in memory, but I doubt the data itself is ...
14votes
5answers
5kviews
How to "Tell, don't ask" when 2 objects involves in the condition and the decision at the same time?
According to Explanation on how "Tell, Don't Ask" is considered good OO, I know the following is bad: if(a.isX){ a.doY(); } public class A{ public boolean isX; public void ...
0votes
0answers
98views
By creating an architecture, it is better to have many classes that handles different scenarios, or a single one that handles all? [duplicate]
During my limited professional experience, I have been involved in microservices projects with a common structure: The Controller takes a request and validates it using the jakarta.validation....
2votes
5answers
389views
Refactoring Java class for a cleaner design
I inherited some code that I have spent some time reviewing to get a better handle on its design. There is one class that I came across that I have an idea for refactoring, but I am wondering if it I ...