mardi 31 mars 2020

Java parent class field getter using reflection vs copy-paste field getter in child classes

A bit of an odd question this one :)

For context, I´m implementing a server using Domain-Driven design as core architecture. For this particular question, I have two classes as entities in my domain:

Vehicle:

public class Vehicle {
    long vehicle_id;
    String licence_plate;
    String make;
    String model;
    Date _date;

    public Vehicle(long vehicle_id, String licence_plate, String make, String model, Date _date) {
        this.vehicle_id = vehicle_id;
        this.licence_plate = licence_plate;
        this.make = make;
        this.model = model;
        this._date = _date;
    }

    public static Vehicle getVehicle(HashMap<String, Object> values, Repository repository) {
        HashMap<String, Object> map = repository.getDriver(values);

        Vehicle vehicle = new Vehicle((long)map.get("vehicle_id"),
                (String)map.get("licence_plate"),
                (String)map.get("make"),
                (String)map.get("model"),
                (Date)map.get("_date"));

        return vehicle;
    }

    public void insertDriver(Repository repository) {
        HashMap<String, Object> map = getStringFieldsMap();

        repository.insertDriver(map);
    }

    public void updateDriver(Repository repository) {
        HashMap<String, Object> map = getStringFieldsMap();

        repository.updateDriver(map);
    }

    public void deleteDriver(Repository repository) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("driver_id", this.vehicle_id);

        repository.deleteDriver(map);
    }

    private HashMap<String, Object> getStringFieldsMap() {
        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("vehicle_id", this.vehicle_id);
        map.put("licence_plate", this.licence_plate);
        map.put("make", this.make);
        map.put("model", this.model);
        map.put("_date", this._date);
        return map;
    }

    public long getVehicle_id() {
        return vehicle_id;
    }
    ...
}

And Driver:

public class Driver {
    long driver_id;
    String first_name;
    String middle_name;
    String last_name;
    String cc_number;
    String driver_licence_number;
    String phone_number;
    String email;

    public Driver(long driver_id, String first_name, String middle_name, String last_name, String cc_number, String driver_licence_number, String phone_number, String email) {
        this.driver_id = driver_id;
        this.first_name = first_name;
        this.middle_name = middle_name;
        this.last_name = last_name;
        this.cc_number = cc_number;
        this.driver_licence_number = driver_licence_number;
        this.phone_number = phone_number;
        this.email = email;
    }

    public static Driver getDriver(HashMap<String, Object> values, Repository repository) {
        HashMap<String, Object> map = repository.getDriver(values);

        Driver driver = new Driver((long)map.get("driver_id"),
                                   (String)map.get("first_name"),
                                   (String)map.get("middle_name"),
                                   (String)map.get("last_name"),
                                   (String)map.get("cc_number"),
                                   (String)map.get("driver_licence_number"),
                                   (String)map.get("phone_number"),
                                   (String)map.get("email"));

        return driver;
    }

    public void insertDriver(Repository repository) {
        HashMap<String, Object> map = getStringFieldsMap();

        repository.insertDriver(map);
    }

    public void updateDriver(Repository repository) {
        HashMap<String, Object> map = getStringFieldsMap();

        repository.updateDriver(map);
    }

    public void deleteDriver(Repository repository) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("driver_id", this.driver_id);

        repository.deleteDriver(map);
    }

    private HashMap<String, Object> getStringFieldsMap() {
        HashMap<String, Object> map = new HashMap<String, Object>();

        map.put("driver_id", this.driver_id);
        map.put("first_name", this.first_name);
        map.put("middle_name", this.middle_name);
        map.put("last_name", this.last_name);
        map.put("cc_number", this.cc_number);
        map.put("driver_licence_number", this.driver_licence_number);
        map.put("phone_number", this.phone_number);
        map.put("email", this.email);
        return map;
    }

    public long getDriver_id() {
        return driver_id;
    }
    ...
}

Both entities, and so far all others, have a getFieldsMap() method that puts all class fields into a map to pass on to insert and update operations.

My question is this: considering this objects will be used across most requests to the server, since they are part of the core domain, would there be any advantage, design vs performance, in refactoring the methods into a method in a parent class that would do the same thing, putting all class fields into a map, using reflection?

And as I'm writing this, might as well ask, although I'm not sure if it is unrelated (against the rules), and if it is I apolagize, but here it is: in delete() I'm doing basically the same thing but with just the id, and so it kinda breaks the neatness I'm going for, so should I use the getFieldsMap() even with the extra fields, from a design standpoint? This question serves for both answers to the first one.

Thank you :)





Aucun commentaire:

Enregistrer un commentaire