-1

Suppose there is a third party library containing base class Transformer and concrete implementations TransformerA and TransformerB.

I need to write parallel classes for TransformerA and TransformerB outputting class say TransformerNew

public class TransformerAConverter {
  public TransformerNew convert(TransformerA transformerA) {
    // conversion logic
  }
}

public class TransformerBConverter {
  public TransformerNew convert(TransformerB transformerB) {
    // conversion logic
  }
}

I need to write following function:

public TransformerNew[] process(Transformer[] transformers) {
}

How can I achieve this without instanceof or explicit type casting. I have tried using visitor pattern but unable to express it.

  • What's the problem with `instanceof`? – dur Dec 19 '17 at 14:22
  • What exactly does _conversion logic_ mean? [This](https://stackoverflow.com/questions/350404/how-do-the-proxy-decorator-adapter-and-bridge-patterns-differ/350471#350471) might help. – Andrew S Dec 19 '17 at 15:08
  • @dur : There could be 10-15 such transformers and I do not wish to use so many instanceof. – P Chandra Dec 19 '17 at 16:49
  • @AndrewS : There would be TransformerNew as base class with TransformerNewA will read getters from TransformerA and populate fields in TransformerNewA according to my own custom logic. Basically downstream I wish to use my custom representation. – P Chandra Dec 19 '17 at 16:54

2 Answers2

0

I would suggest you using Strategy pattern here. The two transformers would be the Transformer strategy here. You may organize the code like this. Visitor won't fit in here, since it is used to decouple the traversal logic from the underneath data structure or representation.

public class TransformerConverter {
    private final Transformer transformerStrategy,

    public TransformerConverter(Transformer strategy) {
        this.transformerStrategy = strategy;
    }

    public TransformerNew convert() {
        // use the strategy to achieve the conversion.
    }
}
Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
0

From the comments - that sounds like the Facade pattern might be useful. For example, given this interface:

public interface TransformerNew {
    public int getInterestingValue();
}

Then have a few implementations:

public class TransformerNewA implements TransformerNew {
    private final TransformerA a;

    public TransformerNewA(TransformerA a) {
        this.a = a;
    }

    public int getInterestingValue() {
        return a.getSomeValue() + a.getSomeOtherValue();
    }

}

and

public class TransformerNewB implements TransformerNew {
    private final TransformerB b;

    public TransformerNewB(TransformerB b) {
        this.b = b;
    }

    public int getInterestingValue() {
        return b.getFirstPart() + b.getSecondPart();
    }

}

So there's really no conversion here - just wrapping the 3rd party type, and providing a common (hopefully simpler) interface for downstream use.

Andrew S
  • 2,509
  • 1
  • 12
  • 14