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

How to use an Apple Watch: Everything you need to know about the Apple Watch

IDG UK Sites

Why Scottish Tablet is better than the iPad mini

IDG UK Sites

How Microsoft's HoloLens AR headset will work without needing a computer or phone

IDG UK Sites

Apple MacBook 1.1 GHz review (Retina, 12-inch, Early 2015): The future of Apple laptops