I have a util class that does some work. Obviously, it is closed for extension, and all methods are static. For simplicity, the class is as follows:
public final class Util { private Util() { } public static void doWork() {
The class has a doWork method that does a lot of calculations. By the way, the method calls the helper help method to get some result, and the rest of the code uses the result returned by the help method.
Now in the client code, I want to reuse the functionality of the doWork method, but instead of calling help I want to call the help2 method. The simplest solution is simply to create the doWork2 method with replacing help with help2 .
This is a very bad approach, because every change in doWork must be replicated in doWork2 . This is very similar to the Template Method template, but due to the fact that we do not have an extension here, we cannot use it.
The best solution I've come up with is to add a parameter to this method, but keep all existing doWork users:
public static void doWork() { doWorkWithParameter(true); } public static void doWorkWithParameter(boolean helpOrHelp2) {
What are the best design solutions to solve this problem? Is there a way to achieve flexibility, for example, the Template Pattern has, but in the application for using classes.
Thanks in advance.
source share