lundi 6 février 2017

DesignPattern to handle pojos

I have a file parser that reads text file line by line and creates a 'Event' object that represent the line.

There are 3 types of 'Event' so I created 4 pojos.
EventTypeA,EventTypeB,EventTypeC,EventTypeD that extend a BaseEvent that I push to an arraylist.

Pojos don't have nothing in common, as each pojo (event) has different set of fields.

Now I have a event handler that should handle all the events in the list based on the type.

I want the best elegant way to process them.

The obvious approach is via polymorphism, but those are pojos and I don't want to put any business code there.

The second option is simply to check 'instnaceoff' and casting BaseEvent to concrete EventType to, but it's not elegant.

Third option is to add 'Type' filed (enum) to pojo and then do a 'switch' checks (+ the casting) , but it's also not elegant.

Fourth option would be to create a hashmap, where key would be the name of the pojo class, and the key will a instance of class with code that handles it.
I don't like that as well.

What can you suggest? Thanks

Aucun commentaire:

Enregistrer un commentaire