Java – What would be the best way to implement priority based programming without adding too much of if-else processing logic

java

We need to add priority based programming in our application using java programming language. For example, there are three priorities that a phone can have: A1, A2 and A3. Also there can be three different types of phone numbers like home (H), office(O) and mobile(M). There have to be default priorities like:

M – A1

H – A2

O – A3

Now the priorities will also change based on user preferences. For example, if O comes with priority A1, M and H will change to A2 and A3.

What will be the best way to implement this logic without adding too much of if-else processing logic in the code?

Best Answer

It seems to me that priority is not a property of a number type if priority is determined based on the priorities of the other number types, so I think the best approach would be to place the number types in a List.

In other words, you have a List of Lists. The outer List determines the priority of the numbers contained within while the inner List contains the numbers themselves.

Suppose the position 0 in your outer list corresponds to priority A1, position 1 corresponds to priority A2, and position 2 corresponds to priority A3. If you add more priorities later, you can add more, but suffice to say that the number of priorities corresponds to the number of possible number types.

Suppose the user wishes to change the priority of those of A1 to A3. Well according to our List, we'd simply need to remove the first list of numbers and append it to the end. The second item in the list now is the first and the third is now the second. The priority of each number therefore is determined by its position in the list, so you know the priority of a number based on which list you retrieved it from.

I hope that's clear. If not, let me know and I'll try to clarify in my answer.