0

Lets say we need some logic change in multiple methods of multiple classes on basis of a flag, while keeping backwards compatibility.

There are two ways..

1.overload every method in every class. then end up with an if-else ladder in caller code to call correct method.

2.Make a common interface and a Factory. Return objects of either on basis of flag passed to factory. Callers don't need any change. only a little change is needed while object creation. Is it logical to create factory for two types only ?

Based on your experience which will you choose ? How to decide between these two ways ? Any better approach you can suggest ?

1
  • 1
    I suggest that you code up a realistic example using both approaches and see for yourself. Commented May 18, 2017 at 16:25

1 Answer 1

3

Logic change suggests behavior which suggests the Strategy Pattern. This avoids a change to the existing method signature.

But you can still use a factory to centralize the creation of the concrete strategy object which handles the logic.

import java.util.Random; public class App { public static void main(String[] args) { App app = new App(); app.calculateSomething(new Random().nextBoolean()); } private void calculateSomething(boolean isUsingLegacyLogic) { CalculationStrategyFactory factory = new CalculationStrategyFactory(); CalculationStrategy strategy = factory.getCalculationStrategy(isUsingLegacyLogic); Calculator calculator = new Calculator(strategy); calculator.calculate(); } class Calculator { CalculationStrategy calculationStrategy; Calculator(CalculationStrategy calculationStrategy) { this.calculationStrategy = calculationStrategy; } // ... public double calculate() { // original code // ... // System.out.println("Calculation steps were done in sequential order."); // return 0; return calculationStrategy.calculate(this); } } private interface CalculationStrategy { double calculate(Calculator c); } private class SequentialCalculationHandler implements CalculationStrategy { public double calculate(Calculator c) { // ... System.out.println("Calculation steps were done in sequential order."); return 0; } } private class ParallelCalculationHandler implements CalculationStrategy { public double calculate(Calculator c) { // ... System.out.println("Calculation steps were done in parralel."); return 0; } } private class CalculationStrategyFactory { public CalculationStrategy getCalculationStrategy(boolean isUsingLegacyLogic) { if (isUsingLegacyLogic || Runtime.getRuntime().availableProcessors() == 1) { return new SequentialCalculationHandler(); } return new ParallelCalculationHandler(); } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Awwwww! I couldn't type fast enough you beat me to it. I love the Strategy Pattern. Yes, this problem is not a creational one but a behavioral one so you would "inject" the new implementation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.