mardi 5 mai 2020

Changing implementation on runtime with cmd

I have a database and an interface, let's call it Updater. The updater is stored in the Timer class and will be invoked once a day to update my database.

 DailyUpdater updater;

    public ProductUpdatingTimer(DailyUpdater updater) {
        this.updater = updater;
    }

    public void changeUpdater(DailyUpdater updater) {
        this.updater = updater;
    }

    public void start() {
        TimerTask repeatedTask = new TimerTask() {
            public void run() {
                System.out.println("Task performed on " + new Date());
                updater.update();
            }
        };
        Timer timer = new Timer("Timer");
        long delay = 1000L;
        long period = 1000L * 600L;//this period is used for testing 
        timer.scheduleAtFixedRate(repeatedTask, delay, period);
    }

Now let's imagine I need to change the behavior of my updater and switch to another implementation with a command from cmd.

Is it possible to do it with some kind of proxy class and listener of command line events? And if yes, would this approach considered 'clean soulution?

Aucun commentaire:

Enregistrer un commentaire