Answer the question
In order to leave comments, you need to log in
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
)
)
Answer the question
In order to leave comments, you need to log in
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);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question