0

I'm looking for a way to reduce duplication in my code.

I have these 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?

2
  • 1
    This question should be on codereview.stackexchange.com not here. Please consider posting there. Commented Jul 24, 2019 at 11:46
  • 1
    Secondly, what is the difference between your House and NewHouse class? Commented Jul 24, 2019 at 11:53

4 Answers 4

2

Just use parametrized types

public class Building<T> {

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

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

public T getNumber() {
    return number;
}

//get/set
//other methods

}

So you can use the following syntax

 Building<House> building = new Building<>(UUID.randomUUID(), new House(), new HouseType());
 Building<NewHouse> newHouse = new Building<>(UUID.randomUUID(), new NewHouse(), new HouseType());
Sign up to request clarification or add additional context in comments.

1 Comment

Also, if House and NewHouse share a common ancestor or interface (let's say IHouse), you'd better use Building<T extends IHouse> to restrict the accepted types.
2

YES.

You can create single class with Generic Type + Inheritance extend new classes.

Let's say:

public class Structure<T> {

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

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

    public T getNumber() {
        return number;
    }

    //get/set
    //other methods
}

Your House class:

public class Building extends Structure<House> {

    //number attribute instanceof House 
    //get/set
    //other methods
}

Your NewHouse class:

public class NewBuilding extends Structure<NewHouse> {

    //number attribute instanceof NewHouse 
    //get/set
    //other methods

}

2 Comments

Surely it's just a typo, but the constructor of Structure should be Structure, not Building
Thanks. Copy + paste issue :-)
0

Create third class which will be a parent to the 2 existing siblings:

public class GenericBuilding {
  private final UUID id;
  private final HouseType houseType;
  //...
}

public class Building extends GenericBuilding {
  private final House number;
  //...
}

public class NewBuilding extends GenericBuilding {
  private final NewHouse number;
  //...
}

Comments

-3

You can create an Abstract class (say Construction) containing all the common variables and methods. Then have the two class Building and NewBuilding extend the Abstract class(Construction)

Link explaining abstraction

1 Comment

If you create abstract class and exclude the House and NewHouse which are not common, what will rest of the functions do without these important variables? Your abstract class would have no implementation which would make it just like an interface. And even in that scenario code will be duplicated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.