Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:19:41+00:00 2026-06-12T17:19:41+00:00

Let’s say I have the following structure: abstract class Hand {} class Rock extends

  • 0

Let’s say I have the following structure:

abstract class Hand {}

class Rock extends Hand {}
class Paper extends Hand {}
class Scissors extends Hand {}

The goal is to make a function (or a method) Hand::compareHands(Hand $hand1, Hand $hand2), which would return the winning hand in a rock-paper-scissors match.

That would be very easy with a bunch of ifs, but the point is to have a more robust structure, that’s relying on polymorphism rather than on procedural code.

P.S. this is done in actual production code, if someone is asking. This isn’t some sort of challenge or homework. (It’s not really rock-paper-scissors, but you get the point).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:19:42+00:00Added an answer on June 12, 2026 at 5:19 pm

    The sole nature of your hand is that it is beating one single other one.

    Then you want to not repeat the code while having one concrete type per hand-form, so you need to parametrize. Depending on the level of freedom you can allow, this can be as simple as a protected member:

    abstract class Hand {
    
        protected $beats;
        
        final public function beats(Hand $opponent) {
        
            return $opponent instanceof $this->beats;
        }
    }
    
    class Rock extends Hand {
    
        protected beats = 'Scissors';
    }
    
    class Paper extends Hand {
    
        protected beats = 'Rock';
    }
    
    class Scissors extends Hand {
    
        protected beats = 'Paper';
    }
    

    I think this is the standard template method pattern here, in a very simple form.

    Compare this with Lusitanian’s answer who should get the credits for the actual code, I just have re-sorted it a little bit. But only a very little.

    Additionally I need to give credits to @Leigh for the far better function and parameter naming. This should reduce the need of comments.

    The second alternative that Lusistanian suggests can be represented with the strategy pattern. It is also somewhat straight forward:

    class EvaluateHands
    {
        private $rules;
    
        public function __construct(array $rules)
        {
            $this->rules = $rules;
        }
    
        public function compareHands(Hand $hand1, Hand $hand2)
        {
            return $this->rules[get_class($hand1)] === get_class($hand2) ? $hand1 : $hand2;
        }
    }
    
    new EvaluateHands(
        array(
            'Rock' => 'Scissors',
            'Paper' => 'Rock',
            'Scissor' => 'Paper'
        )
    );
    

    The comparison between two hands has been fully encapsulated into the EvaluateHands type which is even configureable (if the rules of the game change), while the hands would stay the same:

    abstract class Hand {}
    
    class Rock extends Hand {}
    
    class Paper extends Hand {}
    
    class Scissors extends Hand {}
    

    Credits for this code go to gordon (next to Lusistanian).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let me explain best with an example. Say you have node class that can
Let's say I have thousands of users and I want to make the passwords
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say you have a data frame generated by the following commands: date <-
Let's say I have a domain object with the following field: private Map<StatType, Double>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.