samedi 28 mai 2016

Design pattern for bi directional data format conversion

I work on a project that will deal with different data formats.

for example, suppose i have the following data formats:

formatA, formatB, formatC, ...

with the following classes.

formatA:

public class dataFormatA {

    private String data;

    public dataFormatA(String data) {
        this.data = data;
        // convertData(this.data);
    }

    public dataFormatB convertToDataFormatB() {
        return new dataFormatB(data);
    }

    public dataFormatC convertToDataFormatC() {
        return new dataFormatC(data);
    }

}

formatB:

public class dataFormatB {

    private String data;

    public dataFormatB(String data) {
        this.data = data;
        // convertData(this.data);
    }

    public dataFormatA convertToDataFormatA() {
        return new dataFormatA(data);
    }

    public dataFormatC convertToDataFormatC() {
        return new dataFormatC(data);
    }

}

formatC:

public class dataFormatC {

    private String data;

    public dataFormatC(String data) {
        this.data = data;
        // convertData(this.data);
    }

    public dataFormatA convertToDataFormatA() {
        return new dataFormatA(data);
    }

    public dataFormatB convertToDataFormatB() {
        return new dataFormatB(data);
    }
}

What is the best design pattern to make? currently, each class has a method to convert its data type to the rest of data formats classes. But this is not a good design as it violat the open close principle

Aucun commentaire:

Enregistrer un commentaire