N
N
nepster-web2014-01-19 21:07:53
PHP
nepster-web, 2014-01-19 21:07:53

Pattern "strategy" on the example of determining the winner from the list of users

Earlier I had a problem, I described it in the question Comparing users and calculating the rating
. I was advised to dig in the direction of the "strategy" pattern. Previously, I did not work with this pattern, so I had questions about the organization.
I read several materials about this pattern and now I have questions.
I have an array:

[users] => Array
        (
            [0] => Array
                (
                    [hash] => de2983f989ccf1ea09d1e29a0465fb51
                    [earn] => 32
                )

            [1] => Array
                (
                    [hash] => ef21eb9ea4b5dc9f359024198bf40742
                    [earn] => 23
                )

            [2] => Array
                (
                    [hash] => fff294012895b487157efd570232f0fc
                    [earn] => 22
                )

            [3] => Array
                (
                    [hash] => 3bf51dd7bb302ccf81e335879b97bc5b
                    [earn] => 12
                )

        )

This is a list of users sorted by achievements (points earned). I need to determine the winner and write down the logs (Who won, who lost, give a rating, etc.).
In one of the examples of the implementation of the strategy, I saw how they implement a form validator, that is, they create methods that check the data and simply run the whole thing through a cycle in one file, thereby getting rid of the big if else construct.
In this situation, I have several options that need to be worked out:
- determine the winner (the one who scored the most points)
- if there is a special flag, then the winner will be 1st and 2nd place
- if suddenly 1st and 2nd place scored the same number of points
- if suddenly there is a special flag and 2nd and 3rd place scored the same number of points
- if all users scored the same number of points
These are all the moments I need to process as follows:
- if there is a winner, give him a rating, add a game counter, and so on.
- if there is a special flag, then the winner will be 1st and 2nd place. Throw 1st place 70% rating, 2nd place 30%, and so on.
So, when I started doing this without a pattern, a big fat code with a lot of checks grew up.
So I sat down to think about how to implement all this with a pattern. Since I have not worked with a strategy before, I want to understand how the possibility of implementation will be in my example.
As I understand it, the first thing we need is an abstract class from which all the rest will be inherited.
then I implement a class with methods of all possible options. That is methods:
- Winner 1st place
- Winner 1st and 2nd place
- Draw 1st and 2nd place
- Draw 2nd and 3rd place
- Draw for all
But what's next? That is, what will the first file from which we will initialize this whole thing look like? Can you please tell me the best way to start?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
redc0de, 2014-01-19
@redc0de

In the strategy pattern, some behavior is set for the class, each behavior has its own algorithm, first you need to look at a simple example what classes and interfaces there are, but the point is that you will have one Member class and there will be 5 Behavior classes.

IBehavior
- public function calculate($data);

WinnerFirstBehavior implements IBehavior
WinnerFirstSecondBehavior ... 
DrawFirstSecondBehavior ... 
DrawSecondThirdBehavior ...
DrawAllBehavior ...

class Player
{
    private $behavior;
    private $score;

    public function __construct(IBehavior $behavior, $score)
    {
        $this->behavior = $behavior;
        $this->score = $score;
    }

    public function calculate()
    {
        return $this->behavior->calculate($this->score);
    }
}

M
M_PRO, 2014-01-20
@M_PRO

Here is just not the case when this pattern is not applicable. More precisely, if you use it, you will have to write only two "strategies" (with and without a special flag). But the classes will still turn out 3-4. Such is the price for using patterns.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question