Programming for the OCD

  OCRIDION 22:13 PM 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.

Sony Xperia Z5 vs Xperia Z5 Premium vs Xperia Z5 Compact: What's the difference between Sony's new ?…

1995-2015: How technology has changed the world in 20 years

Get $800 of free online Nuke and VRay rendering from Google Zync

iPhone 6S preview: What to expect from Apple's next iPhone