New answers tagged

0 votes

Allowing users to add their own custom fields in a Spring MVC Hibernate application - What's an ideal approach?

You may find that external factors limit your technical choices. I've never worked in an organisation that allowed an application to create tables on the fly in a production database. The connection ...
  • 1,971
2 votes
Accepted

Dependency Injection vs Static Dependencies

Your public class DoTheThing { private static final DependencyOne dependencyOne = new DependencyOne(); private static final DependencyTwo dependencyTwo = new DependencyTwo(); ...
0 votes

How to refactor this code to obey the ‘open-closed’ principle?

In addition to not being consistent with the "Open Closed Principle", Order has feature envy, knowing too much about a product. To address both from an Order's point of view, move create ...
0 votes

How to refactor this code to obey the ‘open-closed’ principle?

The only variable here is the discount. So if you factor that out by having some discount provider/selector class, that hopefully gets values from some outside maintainable data store, then this class ...
  • 11
0 votes

How to refactor this code to obey the ‘open-closed’ principle?

Most onlineshops have a seperate PriceCalculationService that calculates the prices depending on orderQuantity (quantity-discounts), current stock-level, voucher-codes, .... . This obeys the open-...
  • 7,389
7 votes

How to refactor this code to obey the ‘open-closed’ principle?

The OCP is about allowing "team 1" to provide a black-box framework containing classes like Product, Order, and ShoppingCart, and "team 2" to change the list of products and the ...
  • 188k
3 votes

How to refactor this code to obey the ‘open-closed’ principle?

Let's forget the "Open-Closed Principle" for one second and concentrate on what you actually want. As far as I can tell, you have an implicit "non-functional" requirement, that ...
2 votes

Is the fluent interface pattern suitable in casual scenarios?

From what I see, the pure functionality should have been implemented as a method in the Role class, named outranks taking another role. So everybody in the future can check whether one role outranks ...
  • 7,114
2 votes

Is the fluent interface pattern suitable in casual scenarios?

It was this remark by @candied_orange that prompted me to add this answer: the opinion of the one who wrote the code should never be trusted over someone who didn't I would adopt the opposite ...
  • 4,479
13 votes
Accepted

Is the fluent interface pattern suitable in casual scenarios?

When it comes to readability, the opinion of the one who wrote the code should never be trusted over someone who didn't. It's called the curse of knowledge.= If you wrote it, you already know how it ...
4 votes

Is the fluent interface pattern suitable in casual scenarios?

I agree with Bob, fluent does nothing to improve code and adds an extra class/method in most cases. However. This is why you have rules for PRs and don't just leave it down to preferences. If there ...
  • 65.4k
0 votes

Is MVC a sensible design choice for a CLI application?

MVC can be a sensible design choice for a CLI application. Whether it is entirely depends on your requirements. You state the following requirement: "to make the possibility of a GUI/web ...
  • 101
0 votes

How to solve cyclic dependencies in a visitor pattern

Using the acyclic visitor pattern from Robert C. Martin, which completes the approach from the selected answer. Here's the link to his article presenting it, and below a screenshot of the solution in ...
  • 1
0 votes
Accepted

Should test resource files be stored inside the Java class source directories?

Disadvantages are disadvantages only if you consider them as such. That said, I can think of some. You will be ignoring Maven's Project folder structure, which has become (after many years) a defacto ...
  • 11.9k
5 votes

How do non-RAII languages free resources during stack unwinding?

In Java, the finally clause of a try statement is invoked as the stack unwinds, allowing the programmer to explicitly release resources that require this. For instance, early Java code to write to a ...
  • 3,854
7 votes

How do non-RAII languages free resources during stack unwinding?

Answering only of Java here, though other languages often have equivalents. First, I'd note that Java doesn't really have destructors... while it does support a finalize() method that kind of ...
43 votes
Accepted

How do non-RAII languages free resources during stack unwinding?

Both of the programming languages you mention (as well as many other programming languages) provide Automatic Memory Management. What this means is that the programming language is responsible for ...
19 votes

How do non-RAII languages free resources during stack unwinding?

In garbage collected languages (whether mark-and-sweep style like the JVM or reference counted like Python), there are generally two types of resource: Managed resources, which are handled directly ...
0 votes

Java Library - How to do Pure Dependency Injection When State is a Factor?

The composition root is where long lived objects are constructed. Build em in main and they die when main dies. But not everything lives that long or is born that early. These other, short lived ...
2 votes
Accepted

Java Library - How to do Pure Dependency Injection When State is a Factor?

I don't typically think of visitors as a dependency that you would inject. Visitors are just a control flow mechanism, an object-oriented switch statement of sorts. Any state within the visitor is ...
  • 4,371
0 votes

Java Library - How to do Pure Dependency Injection When State is a Factor?

There are no best practices for this, just requirements, and trade-offs. There is nothing wrong with passing the visitor as a constructor argument. As you noted, the CarVisitor would need a public ...
1 vote
Accepted

Approaches to 'promoting' release candidates in Java projects

Based on the comments, the simplest suggestion I can think is something similar to the Nexus staging workflow, pointing the existing process as well as all downstream integration tests at a new '...
  • 10.2k
0 votes

Is there a standard for describing information about Java Beans (Bean Introspections)

I'm not pretty sure about your need, but i think this could help you: https://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/ Take special attention on BeanInfo interface.
  • 41
0 votes

Is it bad practice to create new objects without storing them?

One good reason to create a reference is so you can give it a good, useful name to aid in documentation, especially of the class name is poor or overly general. e.g. (somewhat contrived) HelloThread ...
  • 8,458
0 votes

Spring Boot - What is the purpose of Autowired Constructors?

Others have already stated a few very important reasons like: Testability: fast unit test that do not need a Spring Context Object validation Avoid Spring coupling Let me add: Immutable properties: ...
  • 1,276
4 votes

Spring Boot - What is the purpose of Autowired Constructors?

Let me add one more reason: testeability. When you are going to test a class, @Autowire requires spring-context to work, which should not go dependant with unit testing, so you'll need reflection for ...
  • 41

Top 50 recent answers are included