Programming for the OCD

  OCRIDION 22:13 06 Mar 2013
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(); } } );

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

Elsewhere on IDG sites

Fujitsu Lifebook P727 laptop review

Microsoft Paint set to die after 32 years

Mac power user tips and hidden tricks

Comment désactiver la saisie intuitive et paramétrer votre clavier ?