1
\$\begingroup\$

I created a project using VLCJ and a probability function to create a media player that plays your favorite media more often than the others, because I wanted to hear my favorite music and videos more than my least favorite. This involved a lot of event listeners that were all at the bottom of my main class file where the components that register the listeners reside. This seemed like a bad way to go about it.

I decided to move each listener to it's own class in the same package and added callback methods in my main class. This worked well, but after several listeners I had to start sifting through to find non-listener classes.

I then decided to put all the listeners in their own package. Now all my listeners and callback methods have to be public.

What is the best way to keep listeners separate from their callback methods? Public callback methods sounds like bad design to me.

I register all the listeners like this,

    // UI listeners
    private final AIPKeyListener keyListener = new AIPKeyListener(this);

This instantiation is with a public AIPListener class in another package.

/**
 *         A custom KeyListener used to listen for key events triggered from an EmbeddedMediaPlayerComponent's VideoSurfaceComponent
 */
public final class AIPKeyListener implements KeyListener {

    // The playlist that contains the methods needed to be called when events are triggered
    AIPlaylist playlist;

    /**       Creates a custom KeyListener used to listen for key events triggered from an EmbeddedMediaPlayerComponent's VideoSurfaceComponent.
     * @param playlist as the playlist that registers this listener with an embeddedMediaPlayerComponent's videoSurfaceComponent.
     */
    public AIPKeyListener(AIPlaylist playlist){
        this.playlist = playlist;
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    /* (non-Javadoc)
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
     */
    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_COMMA) {
            playlist.bad();
        } else if(e.getKeyCode() == KeyEvent.VK_PERIOD) {
            playlist.good();
        } else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
            playlist.getLogger().finest("Right");
            playlist.playNext();
        } else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
            playlist.getLogger().finest("Left");
            playlist.playPrevious();
        } else if(e.getKeyCode() == KeyEvent.VK_L) {
            playlist.loopSwitch();
        }
        else if(e.getKeyCode() == KeyEvent.VK_R) {
            playlist.repeatSwitch();
        } else if(e.getKeyCode() == KeyEvent.VK_P) {
            playlist.getLogger().finest("Resetting probabilities");
            playlist.resetProbabilities();
        }
    } 

    @Override
    public void keyPressed(KeyEvent e) {

    }

}

The callback methods are in the same class where the listeners are registered.

/** Switches whether looping is enabled or not.
     * 
     */
    public void loopSwitch() {
        if(looping) {
            aIPlaylistLogger.finest("Turning loop off");
            mediaListPlayer.controls().setMode(PlaybackMode.DEFAULT);
            if(repeating) {
                mediaListPlayer.controls().setMode(PlaybackMode.REPEAT);
            }
            looping = false;
        } else {
            aIPlaylistLogger.finest("Turning loop on");
            mediaListPlayer.controls().setMode(PlaybackMode.LOOP);
            looping = true;
        }       
    }

The full code is here: https://github.com/aljohnston112/AIPlaylist/releases/tag/v0.1.0-0.1.0

My main concern is that having public callback methods callable by anyone is bad practice, but I want to be able to keep the listeners separate from the rest of my project. What is the best way to go about this?

\$\endgroup\$
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.