I'm working on one idea for existing Java project which can add new logic of operations depends on modules initialized and also downloaded (optional modules) by user.
Question is, that some operation depends on order of commands, which can be hardly coded with knowledge of all modules, or it can be modular and loaded by some hooks.
Example - scoring element (in game).
- Basic scoring
- Feature1 scoring - double basic score
- Feature2 scoring - add some extra points to Basic score, when Feature1 is - activated, double those score - different login if Feature1 is activated or not
- Feature3 scoring - add extra score Depends on Feature1 and also Feature3 if one or both activated
It is not problem to hard code it.
But how to sort those scoring when will be created plugin with Feature 4 which can have different requirements for scoring, for example Has to be run after Basic scoring but before Feature1 scoring if activated because it change basic scoring which affect Fature1 scoring.
So my idea is define
order requirements with defined which "known" scoring is required to be run after known scorings, but also requirement which tells to run before known scorings if defined
- basic: run_after: NULL, run_before: NULL
- feature1: run_after: basic, run_before: NULL
- feature2: run_after: basic, feature1, run_before: NULL
- feature3: run_after: basic, feature1, run_before: NULL
- feature4: run_after: basic, run_before: feature1
so result will be when all modules are activated
- basic
- feature4
- feature1
- feature3 -> feature 3 and feature2 have same definition doesn't mather of order 2->3 or 3->2
- feature2
when for example onle basic and feature2 and feature4 then result will be same order by skipping not activated or not found modules
- basic
- feature4
- feature2
I was looking for some plugins in Java like PF4J, which can uses dependencies to order execution extensions.
But there can be feature5 and feature6 as plugins, which for example feature6 can use different scoring when feature5 is loaded.
If using PF4J dependencies, and feature5 will not be available, also feature6 will not be loaded. This is reason to looking for some advanced sorting.
question from:
https://stackoverflow.com/questions/65844927/advanced-sorting-in-java 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…