mercredi 24 juillet 2019

Java - reduce code duplication - General coding question

Im looking for the best practice for reducing duplication in my code.

I have this 2 classes with same methods and ONLY difference private final House number; and private final NewHouse number; :

public class Building {

    private final UUID id;
    private final House number;
    private final HouseType houseType;

    Building(UUID Id, House number, HouseType houseType) {
        this.id = id;
        //....//
    }

    public House getNumber() {
        return number;
    }

    //get/set
    //other methods

}

public class NewBuilding {

    private final UUID id;
    private final NewHouse number;
    private final HouseType houseType;

    NewBuilding(UUID id, NewHouse number, HouseType houseType) {
        this.id = id;
        //....//
    }

    public NewHouse getNumber() {
        return number;
    }

    //get/set
    //other methods
}

Is there some way that I can combine this two classes in one and reduce duplication in my code?

Is there some design pattern that I can use? Should I create some 3rd class with common functionality ?

Any suggestions welcome.

Aucun commentaire:

Enregistrer un commentaire