1. True, for every new type of taxable item you need to visit, you'll need to add a new overloaded version of the visit() method. That's the disadvantage of the Visitor pattern.
2. Re "this patterns creates a monolithic class with various functions" -- first of all, the pattern doesn't create anything. Maybe you meant "... this pattern results in a monolithic class with potentially many overloaded versions of the visit() method" -- this is more accurate and is in line with #1 above.
3. The "visit" method doesn't always need to be literally named
visit(). In this case, I would probably name it "getTaxFor()" or something that more clearly expresses what it's doing in the language of the domain. The name "visit()" as documented in the pattern is just a generalization.
4. Your implementation has smells.
4a. This is smelly:
return Double.parseDouble(df.format(. . .)); -- why do this here? Why even do it at all?
4b. The
TaxVisitor is also a little smelly to me. Seems like this is more appropriately called something like
TaxCalculator. If you really want to retain the "Visitor" part of the name (which like the "visit" name is really not a requirement), I'd name it
TaxableItemVisitor instead but that's still not a name that clearly expresses what the visitor does.
5. Your problem does not appear to be something where the Visitor pattern would be appropriately applied.
You should understand the context in which a pattern is meant to be applied. If you apply a pattern in a context that is inconsistent with the intended purpose, you run the risk of turning it into an anti-pattern instead.
This is a pretty good description of the appropriate context for Visitor (from
https://sourcemaking.com/design_patterns/visitor)
Many distinct and unrelated operations need to be performed on node objects in a heterogeneous aggregate structure. You want to avoid "polluting" the node classes with these operations. And, you don't want to have to query the type of each node and cast the pointer to the correct type before performing the desired operation.