I have a base class PlanItem which all other subclasses extend.
- An Objective extends PlanItem.
- A Strategy extends PlanItem.
- A Tactic extends PlanItem.
- A Task extends PlanItem.
The catch:
- An Objective has a list of Strategy objects. Call it its child item list.
- A Strategy has a child list of Tactics.
- A Tactic has a child list of Tasks.
I could have made 4 completely cloned classes, word for word, but that would make editing a total pain. I opted to use inheritance, but then comes the problem of circular dependency.
Currently I am:
Letting the base PlanItem class have a field:
private ObservableList<? extends PlanItem> childPlanItems = FXCollections
.observableArrayList();
I have an FXML with Controller, ViewPlanItemController, that displays a particular item's statistics and child list in a TableView.
public class ViewChildItemsController<E extends PlanItem> {}
That TableView has options for adding, editing, viewing, and removing child items.
If I want to view a child item, then we recursively load another ViewPlanItem.fxml with controller. The problem is, every time I want to load another ViewChildItem.fxml, I need to provide it with the child type so that it can view them.
If I were trying to view an Objective, I would init the controller so that it knows to display items of type Strategy.
Here is the controller's init method, if needed:
/**
* Initializes the values of the items to be displayed.
*
* @param list The list of child items to display.
* @param defCtor Default ctor of each child item.
* @param copyCtor Copy ctor of each child item.
*/
public void initValues(ObservableList<E> list, Supplier<E> defCtor, UnaryOperator<E> copyCtor) {
childItemsTableView.setItems(list);
statisticsPanelController.init(list);
this.defCtor = defCtor;
this.copyCtor = copyCtor;
}
How would I do this? If I am within the ViewPlanItemController class itself, how do I know the type of class the child item is? Seeing as it goes from Objective->Strategy->Tactic->Task, there needs to be some class-level recursion.