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 ...
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 ...
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
3answers
2kviews
How to design an API wrapper with bulky operations on domain object? (Need guidance)
I need some guidance in designing an API wrapper for my backend APIs. I have tried to keep it as specific as possible. Context: We have a project which supports certain file operations like edit, ...
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 ...
4votes
6answers
2kviews
The ID of an object is null at start. Does that make the object state invalid and violate encapsulation?
This question is related to How should an `Employee` class be designed? In the above question, to uniquely identify an employee, each Employee object has an id field as shown below class Employee { ...
6votes
2answers
3kviews
Trouble with circular dependency in state machine design
I am trying to develop the structure for a basic state machine that can also take in input and produce output. I've hit a bit of a mental block in trying to figure out how to model the relationship ...
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 ...