lundi 30 octobre 2017

Interaction between methods in child and parent class in java

I am new to programming and I have the following question about architecture and method interaction.

My question is how best to make the following interaction in the following classes. Editor is base abstract class and EditorController is ManagedBean/CDI controller for the jsf. Load method is used directly in the jsf(xhtml) file and becouse of this i don`t want to overload it and repeat most of the logic in the child classes.

the first way that comes to my mind is:

public abstract class Editor {
    ...
    ...

    void delete(id) {
        preLoad();
        this.currentEntity = getService().findByPrimaryKey(id);
         loadAdditionalData();
         this.editMode = true;
         this.newObject = false;
         loadComplete();
    }

    protected void preLoad() {
    }

    protected void loadAdditionalData() {
    }

    protected void loadComplete() {
    }
}

@Named("editor")
@ViewScoped
public class EditorController {
    ...
    ...
    @Override
    protected void loadComplete() {
        // do stuff
    }
}

Second way in my mind is with listener like this:

public abstract class Editor {
    ...
    ...

    final protected OnLoadListener DEFAULT_ON_LOAD_LISTENER = new OnLoadListener() {};

    void delete(id) {
        getOnLoadListener.onPreLoad();
        this.currentEntity = getService().findByPrimaryKey(id);
        getOnLoadListener.onPostLoad();
        this.editMode = true;
        this.newObject = false;
        getOnLoadListener.onLoadComplete();
    }

    public abstract class OnLoadListener {
        void onPreLoad() {}
        void onPostLoad() {}
        void onComplete() {}
    }

    @NotNull protected OnLoadListener getOnLoadListener() {
        return DEFAULT_ON_LOAD_LISTENER;
    }
}

@Named("editor")
@ViewScoped
public class EditorController {

    private OnLoadListener onLoadListener = new OnLoadListener() {
        @Override
        public void onLoadComplete() {
            // do stuff 
        }
    }

    @Override
    @NotNull protected OnLoadListener getOnLoadListener() {
        return onLoadListener;
    }
}

If there is a better way, please tell me :).

Aucun commentaire:

Enregistrer un commentaire