samedi 6 juin 2020

Why use (and not use) generics in java ? Example of event driven programming [duplicate]

My question is more about conception choices than the actual implementation itself. In your experience, which one of Event interface implementation would you prefer ? why ? If you prefer another implementation, please add it in your answer.

// The most important classes and interfaces to look at are the `*Events`.

public interface Metadata {
}

public class EventMetadata extends Metadata {
}

public class SensorMetadata extends Metadata {
}

public interface Data<T> {
    T value();
}

public class SensorData extends Data<Double> {
}

/** (1)
* With this, I'll lose specific information about the data and the metadata returned by the respective methods.
* One way to get those specific information back will imply casting
* Arguably, I don't like casting objects that I wrote the implementation myself.
*/
public interface Event { // Event
    EventMetadata metadata();
    Metadata from();
    Data<?> data();
}

/** (2)
* I have control of the data type that this event hold
*/
public interface Event<T extends Data<?>> { // Event<T>
    EventMetadata metadata();
    Metadata from();
    T data();
}

/** (3)
* I can have specific information about the event metadata and the data
*/
public interface Event<T, U extends Data<?>> { // Event<T, U>
    EventMetadata metadata();
    T from();
    U data();
}

/** (4)
* This last one is what I call a generic hell ;)
* But yeah, it gives all the control one may need.
*/
public interface Event<T, U extends Data<V>, V> { // Event<T, U, V>
    EventMetadata metadata();
    T from();
    U data();
}

public class SensorEvent implements Event {
}

public class SensorEvent implements Event<SensorData> {
}

public class SensorEvent implements Event<SensorMetadata, SensorData> {
}

public class GenericHellEvent implements Event<SensorMetadata, SensorData, Double> {
}

Any implementation choice I choose will have a huge impact on the system. I have other classes like Listener / Observer and Observable that all depend on the Event class

P.S.: English is not my first language.

Aucun commentaire:

Enregistrer un commentaire