Programming for the OCD

  OCRIDION 06 Mar 13
Locked

Hi all, implementing the Command Pattern here using a hashMap (java). However, each menu item requires a new class implementing an Interface (Abstract class), and the Method invoking a seperate static method.

However, I'm OCD, and I don't like the obvious repetition here, and I can't seem to find a way to refactor this down to less lines. Any suggestions?

        scoreAsCommands.put(0, new menuFunction() { public void execute() { CardScorer.scoreOnes(); } } );
        scoreAsCommands.put(1, new menuFunction() { public void execute() { CardScorer.scoreTwos(); } } );
        scoreAsCommands.put(2, new menuFunction() { public void execute() { CardScorer.scoreThrees(); } } );
        scoreAsCommands.put(3, new menuFunction() { public void execute() { CardScorer.scoreFours(); } } );
        scoreAsCommands.put(4, new menuFunction() { public void execute() { CardScorer.scoreFives(); } } );
        scoreAsCommands.put(5, new menuFunction() { public void execute() { CardScorer.scoreSixes(); } } );
        scoreAsCommands.put(6, new menuFunction() { public void execute() { CardScorer.scoreThreeOfAKind(); } } );
        scoreAsCommands.put(7, new menuFunction() { public void execute() { CardScorer.scoreFourOfAKind(); } } );
        scoreAsCommands.put(8, new menuFunction() { public void execute() { CardScorer.scoreFullHouse(); } } );
        scoreAsCommands.put(9, new menuFunction() { public void execute() { CardScorer.scoreLongStraight(); } } );
        scoreAsCommands.put(10, new menuFunction() { public void execute() { CardScorer.scoreShortStraight(); } } );
        scoreAsCommands.put(11, new menuFunction() { public void execute() { CardScorer.scoreDicezy(); } } );
        scoreAsCommands.put(12, new menuFunction() { public void execute() { CardScorer.scoreDicezyBonus(); } } );
        scoreAsCommands.put(13, new menuFunction() { public void execute() { CardScorer.scoreChance(); } } );
Advertisement

This thread is now locked and can not be replied to.

Windows 10 Home vs Windows 10 Pro comparison: Here are the extra features you get in the Pro version

Why Formula-e needs to go faster

How Instagram spots trends

How to use Apple Music in the UK: Complete guide to Apple Music's features

We use cookies to provide you with a better experience. If you continue to use this site, we'll assume you're happy with this. Alternatively, click here to find out how to manage these cookies

hide cookie message