Question

This is some sort of a design-pattern question, I guess.

I want to model several operations (input + processing logic + output) as classes that implement some IOperation interface.

Each operation may have different input parameters and different output parameters.

I can specify different inputs for every operation using each one's constructor.

But I'd like to get a different output for each class in a orthogonal manner too, without having to cast the interface to the concrete class.

The obvious approaches for this is to [1] cast to the concrete class and call a specific method that returns the desired results or [2] wrap the results into some OperationResult object, which in turn will also need a downcast or some metadata information + extractor code.

I'd like to avoid dealing with reflection or downcast or metadata interpretation.

Is there any known design for this kind of situation in Java? What would be a good approach for this?

UPDATE - @Laf, here goes some concrete example. I'd like to extract "c"

interface IOperation{
    void execute();
}

public class AddOp implements IOperation{

    private int a;
    private int b;
    private int c;

    public AddOp(int a, int b){
        this.a = a;
        this.b = b;
    }

    @Override
    public void execute() {
        this.c = this.a + this.b;
    }

}

public class AndOp implements IOperation{

    private boolean a;
    private boolean b;
    private boolean c;

    public AndOp(boolean a, boolean b){
        this.a = a;
        this.b = b;
    }

    @Override
    public void execute() {
        this.c = this.a && this.b;
    }

}
Was it helpful?

Solution

Looks like you can use double dispatching.

Instead of returning a value (ie, Object), you return void and pass as a parameter a target object you want your result to be put into.

interface ResultProcessor {
    processTypeA(A item);
    processTypeB(B item);
    processTypeC(C item);
}

interface InputProcessor {
    processInput(ResultProcessor target);
}

class ConcreteInputProcessorA {
    processInput(ResultProcessor target) {
      A result = ...; // Do something
      target.processTypeA(result);
    }
}

class ConcreteInputProcessorB {
    processInput(ResultProcessor target) {
      B result = ...; // Do something
      target.processTypeB(result);
    }
}

class ConcreteInputProcessorC {
    processInput(ResultProcessor target) {
      C result = ...; // Do something
      target.processTypeC(result);
    }
}

Update: For example with your new edited code you may write:

public class AddOp implements IOperation{

    private int a;
    private int b;
    private int c;

    public AddOp(int a, int b){
        this.a = a;
        this.b = b;
    }

    @Override
    public void execute(Executor ex) {
        this.c = this.a + this.b;
        ex.executeAddOp(this);
    }
}

public class Executor {
    public void executeAddOp(AddOp op) {
        System.out.println("Executing an AndOp... " + op);
    }
}

OTHER TIPS

I myself came to another interesting approach, after considering some proposals posted here.

I won't accept it as the final answer since I don't think it's fair with those who spent their time trying to help. But I would like to share it anyway.

public class SO {

    public static void main(String[] args){
        new SO().new Example().exec();
    }

    interface IResponse{
        //just a tagging interface
    }

    public class IntegerResponse implements IResponse{
        private int i;

        private IntegerResponse(int i) {
            this.i = i;
        }

        protected int getI() {
            return i;
        }

    }

    public class BooleanResponse implements IResponse{
        private boolean b;

        private BooleanResponse(boolean b) {
            this.b = b;
        }

        protected boolean isB() {
            return b;
        }

    }

    interface IOperation<X extends IResponse>{
        void execute();
        <X extends IResponse> X someMethod();
    }



    public class AddOp implements IOperation<IntegerResponse>{

        private int a;
        private int b;
        private int c;

        public AddOp(int a, int b){
            this.a = a;
            this.b = b;
        }

        @Override
        public void execute() {
            this.c = this.a + this.b;
        }

        @SuppressWarnings("unchecked")
        @Override
        public IntegerResponse someMethod() {
            return new IntegerResponse(this.c);
        }

    }

    public class AndOp implements IOperation<BooleanResponse>{

        private boolean a;
        private boolean b;
        private boolean c;

        public AndOp(boolean a, boolean b){
            this.a = a;
            this.b = b;
        }

        @Override
        public void execute() {
            this.c = this.a && this.b;
        }

        @SuppressWarnings("unchecked")
        @Override
        public BooleanResponse someMethod() {
            return new BooleanResponse(this.c);
        }

    }

    public class Example{
        public void exec(){
            IOperation<?> op = new AndOp(true,false);
            BooleanResponse resp = op.someMethod();
            System.out.println(resp.isB());
        }
    }
}

A possible approach is something like this:

private <T extends SomeCommonAncestor> T someMethod(InputParamObject i,
        Class<T> respClass)  {...}

You still needs some common interface/abstract class for the returned object, and you need to specify the implementing/child class as part of the method params.

Hope this helps.

[EDIT]

public interface IOperation{

       public <T extends ResultObject> T execute(Class<T> respClass);
}

public class AndOp implements IOperation{

    private boolean a;
    private boolean b;
    private boolean c;

    public AndOp(boolean a, boolean b){
        this.a = a;
        this.b = b;
    }    


   @Override
   public <T extends ResultObject> T execute(Class<T> respClass)
   {
       BoolWrap ret = new BoolWrap (a & b);
       return ret;
   }

}

class BoolWrap extends ResultObject {
 ...
}

class AndOpTest {
    public void testIt ()
    {
         AndOp op = new AndOp (false, true);
         BoolWrap result = op.execute (BoolWrap.class);
         //Profit?
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top