mercredi 23 août 2017

abstracting iterative object conversions

I have a collection of objects that I wish to convert freely in-between. Lets call them A through F.

The relationship between the objects might look something like this

A -- B -- C -- F
     |         |
     D -- E ----

What this means is A can be converted to B but not C, if you want to convert A to C then you have to convert it to B first, then to C from there.

I'm having trouble coming up with an extensible and flexible implementation of this that would allow the easy addition of new types and the conversions that go with them. Even after combing through every design pattern I could find I'm no closer to coming up with an answer.

Initially I had one Converter class that looked something like this

public class Converter

public B convA2B(A in){...}
public A convB2A(B in){...}
public C convB2C(B in){...} etc

this proved to be unwieldy as I tried to add more types. Next I tried having multiple converter objects all extending the same abstract class

public abstract class Converter

final static int typeA = 0;
final static int typeB = 1;
final static int typeC = 2;
int type;

public abstract A toA(Object in);
public abstract B toB(Object in);
public abstract Object fromA(A in);
public abstract Object fromB(B in);
...

public Object convertTo(int type, Object data)
{
    switch(type){
        case 0: return new A().toA(data)
etc etc

Essentially what would happen is each converter would convert the data to the next object type in the path, before passing that data on to the next converter.
ie, if I wanted to convert from A to C, A.toC(x) would call B.toC(A.toB(x)).

this didn't work because each converter type needed to have some basic understanding of the relationship between all the types in order to know when converter to call next, which meant adding new converters became quite difficult in places and could even lead to endless loops if handled poorly.

What should I do? Many of the design patterns I read about seem to be close to what I'm looking for, like mediator, chain of responsibility, interpreter, but I'm not certain how to adapt them to do what I want.

Aucun commentaire:

Enregistrer un commentaire