jeudi 17 décembre 2020

What is the best way to organize my listeners?

We are implementing a plugin for a tool, our goal is to provide accelerator for the customers.

For example, if the user is creating an object Property A then we will create another object XA. if the user delete this object A we will delete our object XA. If the user is creating an object B, and its parent type is A then we will create an object XB plus trigger a dedicated behavior.

Today we are using imbricated if to determine which pattern occurred.

for (PropertyChangeEvent event : events) {
if (isEventTypeOf(INSTANCE_CREATED) {
    if(object instanceOf A)
        createXA_Object(object)
    
    if(object instanceOf B){
        createXB_Object(object)
        if(object.parent instance of A)
            customXB_behavior(object)
    }
}else{
    if(isEventTypeOf(INSTANCE_DELETED) {
    ...
    }
}

}

Our problem is today we have 20 listeners and this number will increase during the time. So I was looking for a better way to organize our code. I need a solution where it is simple to add a behavior, with an easily readable code; which function will be called on which condition.

Here are a list of different ideas:

  • write boolean isPatternA() functions to make the code more readable
  • create a Class with a list of Predicate to evaluate and a behavior
  • this thread: Alternatives to embedded if statements?
  • Using Enum
  • Using Strategy
  • Using Rules Engine

I hope I was clear

Aucun commentaire:

Enregistrer un commentaire