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
Contact Forum Editor

Send an email to our Forum Editor:


PLEASE NOTE: Your name is used only to let the Forum Editor know who sent the message. Both your name and email address will not be used for any other purpose.

Tech Helproom


It's free to register, to post a question or to start / join a discussion


 

Programming for the OCD


OCRIDION

Likes # 0

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(); } } );
Like this post

Reply to this topic

This thread has been locked.



IDG UK Sites

Spotify launches on PS4 as Tidal arrives on Sonos: It's Tidal vs Spotify music streaming

IDG UK Sites

It's World Backup Day 2015! Don't wait another minute: back up now

IDG UK Sites

Adobe Comp CC iPad app review

IDG UK Sites

April Fool's Day pranks: play these geeky pranks on April Fools Day and fool your friends