newLearner
newLearner

Reputation: 767

Generic Method with different argument but same return type

I have multiple methods which accept different types in arguments but have the same return type. I am planning to create a generic method but not sure of the implementation.

Method 1 :

private Details mapStudentDetails(final Student student) {
        return Details.builder()
            .line1(student.getLine1())
            .line2(student.getLine2())
            .postcode(student.getPostcode())
            .country(student.getCountryCode())
            .build();
    }

UPDATED Method 2 :

private Details mapTeacherDetails(final Teacher teacher) {
        return Details.builder()
            .line1(teacher.getAddressLine1())
            .line2(teacher.getAddressLine2())
            .postcode(teacher.getZipCode())
            .country(teacher.getCountryCode())
            .build();
    }

How can I implement a generic method for this usecase ?

private Details mapDetails ( T type) {
}

Upvotes: 0

Views: 755

Answers (1)

Alexander Ivanchenko
Alexander Ivanchenko

Reputation: 28978

Firstly, because you are using the same methods (getLine1(), getLine2(), getPostcode()) it's obvious that they are unrelated to the responsibilities of these classes and must reside somewhere else.

As far as these concerns are connected to postal service and don't related to the idiosyncratic responsibilities of the classes Student and Teacher they have to be externalized.

Let's group the behavior related to the post inside the interface PostDetails (sorry for a clumsy name). And I want to emphasize that it's highly advisable to use an interface for this purpose, not an abstract class like Person. Tomorrow you may wish to be able to send letters to the organizations but it doesn't make much sense for a class Campany to extend the class Person. Inheritance creates a tight coupling and can be easily misused. If you have a choice between an interface and abstract class in the class design interface takes precedence.

Methods of the PostDetails could be abstract or default (which will probably make sense because the procedure of sending letters except for special cases must be the same).

    public interface PostDetails {
        String getLine1();
        String getLine2();
        String getPostcode();
        String getCountryCode();
    }

Both Student and Teacher classes must implement the contract defined by the PostDetails.

    public class Student implements PostDetails {
        // implementation for Student 
    }

    public class Teacher implements PostDetails {
        // implementation for Teacher 
    }

As far as method mapDetails() isn't bound to any class and meant to use only behavior defined in the PostDetails it can accept any implementation of this interface (Student, Teacher, Company, etc).

    private Details mapDetails(PostDetails postDetails) {

    }

Update

If there's a need for two different sets of behavior related to postal service then it clearly has to be two interfaces (even if now you have only two classes this decision can bring benefits afterward). And as a consequence, there must be two overloaded versions of the mapDetails() method that accept as parameter instances of these interfaces.

Upvotes: 1

Related Questions