Ankit Pramod Singh
Ankit Pramod Singh

Reputation: 11

Design pattern to reuse the existing interface implementation in java

Suppose i have an interface

    public interface Action<T> {
        void doWork(T t);
    //can have multiple methods
    }

Now this class is implemented by

    public class DefaultAction implements Action<String> {
        @Override
        public void doWork(String s) {
            System.out.println(s);
        }
    }

Now i have a flow which accepts action

public abstract class Flow<T> {

    void execute(T t){
        Action<T> action = getAction();
        action.doWork(t);
    }

    abstract Action<T> getAction();
}

so i have flow that accepts double

public class AnotherActionFlow extends Flow<Double> {

    @Override
    Action<Double> getAction() {
        return new AnotherActionSet();
    }
}

which in turn uses the default action

public class AnotherActionSet implements Action<Double> {

    DefaultAction defaultAction;

    @Override
    public void doWork(Double aDouble) {
        defaultAction.doWork(String.valueOf(aDouble));
    }
}

So in this case i have to always update the AnotherActionSet method whenever the action interface changes

Is their any workaround so that i can have the common logic to convert the double to string and then use the DefaultAction

Can any pattern help to convert the AnotherActionSet to DefaultAction in which i only specify String.valueOf(aDouble) so the values can be passed to DefaultAction instead of writing same thing for every method

Summary

I want to reuse use the logic inside the DefaultAction for other actions which are a wrapper around the DefaultAction

For eg;

public class AnotherActionSet2 implements Action<Long> {

    DefaultAction defaultAction;

    @Override
    public void doWork(Long long) {
        defaultAction.doWork(String.valueOf(long));
    }
}


public class AnotherActionSet3 implements Action<Integer> {

    DefaultAction defaultAction;

    @Override
    public void doWork(Integer int) {
        defaultAction.doWork(String.valueOf(int));
    }
}


As you can see i have to write the mapping each time manually, instead is it possible to just pass the function which converts the give class to string and then i can use DefaultAction

So i just define Function<Double,String> dToS and plug it to DefaultAction and don't need to define the method again

Upvotes: 1

Views: 167

Answers (1)

Nicolas Bousquet
Nicolas Bousquet

Reputation: 4000

You can use Java 8 lambda to get something that is much easier and more expressive.

Basically your Action is the same as standard Consumer from JDK, only that the name of the method change.

Then defaultAction look like that:

Consumer<String> defaultAction = s -> {System.out.println(s);}

And so AnotherActionSet can be expressed like that:

Consumer<Double> anotherActionSet = s -> {defaultAction.accept(String.valueOf(aDouble));}

You also discover that flow and action are doing exactly the same (taking a T and returning nothing) so one of the notion is redundant. And you could write:

Consumer<Double> anotherActionFlow = anotherActionSet;

Upvotes: 1

Related Questions