Try to refactor the concept and not only the code. Refactoring to remove duplicate code leads to OnceAndOnlyOnce. But if not done properly, it might lead to more complex code. So when you refactor, step back and look at the concept. May be the concept also has to be refactored to make the system more simple to understand. I have found refactoring or changing the concept make the system much more simpler and flexible.
Could we get an example of code that needs its concepts refactored?
Suppose we have a requirement to validate the parameters passed to a controller class. Then we have the interface
public interface InputValidator() { public boolean validate(); } public interface Controller() { public void setInputValidator(InputValidator validator); public void executeReq(); // It will call validator.validate() before processing the request. }And the code that calls is
public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) { ... Controller usrContrl = new UserController(); usrContrl.setInputValidator(new UserInputValidator(req)); usrContrl.executeReq(); } }Now, a functionality is required by the client so that they can execute their own code with this Web Application. We can implement it as Executor. The controllers take a list of executors and execute them one by one. The list is configurable.
public interface Executor { public boolean execute(); } public interface Controller() { public void setInputValidator(InputValidator validator); public void setExecutors(Executors[] execs); public void executeReq(); // Now it will call both validator and executors before processing the request. } public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) { ... Controller usrContrl = new UserController(); usrContrl.setInputValidator(new UserInputValidator(req)); // No need for this. Use Executor instead. Executor[] execs = config.getExecs("UserController"); usrContrl.executeReq(); } }Here we can simplify the concept by removing Validator concept and implementing that code as an Executor. So instead of 2 concepts, we have only one concept. -- VhIndukumar