mercredi 26 septembre 2018

Should i use a pattern

So I'm receiving a string from twitch irc and based on that command I execute some code. The question is can i simplify my code or use a pattern. You can see the code below. It looks very messy and adding new commands or functionality will probably be a pain(if I have 200 or more commands).

public void onCommand(User user, Channel channel, String command)
    {

        // some if statements 

        switch (command.toLowerCase()){
        case "hi":{

            //some simple action
        }
        case "fire":{

            vote(60, channel, "fire")
        }
        ...

        //timeout
    }

    void vote(int duration, final Channel channel, String voteFor){
        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //start voting
            }, duration);

            switch (voteFor){
                case "fire":{
                    if (voteYes > voteNo) {
                        //some action
                    }else
                        //some action
                    break;
                }
                ...
    }  

P.S I tried using the strategy pattern but it felt like there is no need for it.

Aucun commentaire:

Enregistrer un commentaire