Navigator - examples

Printer-friendly versionPrinter-friendly version
How to use the Navigator framework

 

For the implementation in the model, one sample should be sufficient:

public class ConcreteParent1 extends AbstractParent implements Navigable {
    
    @Override
    public void admit(Navigator navigator) {
        if (navigator instanceof AbstractParentNavigator) {
            ((AbstractParentNavigator) navigator).access(this);
        }
    }
    
    @Override
    public Collection<Navigable> getNavigables() {
        return super.getChildren();
    }
    
}

At least each concrete type in our domain model must implement the Navigable interface (but it is recommended to inherit that interface) and thus the "admit(Navigator)"-operation, where the model object calls back the Navigator and passes itself as actual parameter, and the "getNavigables()"-operation, where the model object returns its navigable children and possibly other navigable objects known to that model object, including the parent if applicable. To return any navigable object is possible, because the Navigator framework is designed to navigate on cyclic object graphs as well. Which navigable objects we return actually will depend on our domain model.

Note: the "admit(Navigator)"-operation is identical for all navigable types in our model so we can simply copy paste or generate it. For the "getNavigables()"-operation we may use an advanced Java generator. The implementation in the model will be done just once per type in a model lifetime, so a manual implementation even in a larger model should be okay, and adaptions to model changes are easy since a model type just simply returns the Navigables.

 

After the implementation in the model we are ready to implement our first Navigator:

public class ConcreteNavigator1 extends AbstractParentNavigator {
    
    @Override
    public void access(ConcreteChild1 concreteChild1) {
        super.access(concreteChild1);
        if (canEnter(concreteChild1)) {
            // access public members of "concreteChild1"
        }
        if (canReenter(concreteChild1)) {
            // access public members of "concreteChild1"
        }
    }
   
    @Override
    public void access(AbstractChild abstractChild) {
        super.access(abstractChild);
        if (canEnter(abstractChild)) {
            // access public members of "abstractChild"
        }
        if (canReenter(abstractChild)) {
            // access public members of "abstractChild"
        }
    }
}

 

 

Seiten