mardi 21 juin 2016

How do I refactor two classes with similar functionality?

I have classes with methods with the same names that do the same thing yet they are implemented differently.

Ex:

class converterA {
    map(Item1 item1) {
        // Implementation details.
    }

    convert(Item1 item1) {
        // Implementation details.
    }

    translate(Item1 item1) {
        // Implementation details.
    }
}

class converterB {
    map(Item2 item2) {
    // Implementation details.
    }

    convert(Item2 item2) {
    // Implementation details.
    }

    translate(Item2 item2) {
    // Implementation details.
    }
}

I considered using an interface but the issue is that is that the methods take in different parameters. Yet a template doesn't exactly fit either because Item1 and Item2 operate in different ways. In other words, they don't have common methods so a template doesn't exactly fit either.

Is there a solution here for refactoring the code?

Aucun commentaire:

Enregistrer un commentaire