$P1 = new Player(array(
'name' => 'Player 1',
'strat' => new Strategy(&$this)
));
This is more or less what I'm trying to do. The reference to $this is obviously wrong. What is the best way to accomplish something like this?
Basically, the new 开发者_Go百科Strategy object needs to know what Player it belongs to.
Sure I could do like:
$P1 = new Player();
$P1->strat = new Strategy(&P1);
but that doesn't seem as concise.
Update:
I guess instead of passing the new strategy object in the array, I would pass the name of the strategy class instead, and then instantiate a new object in the Player constructor.
$P1->strat->owner =$P1;
That will be good enough.
Talk is on.
Okay, if we approach this question from theoretical and conceptual standpoint, we can address the implied question in a couple of different ways. Let's consider what Strategy to Player is.
As the name implies, Strategy is basically a driver, that could affect the behavior of the play in a particular environment. This further suggests that following approach would make sense:
$player->useStrategy($strategy);
further, player most likely would be to act in given scope, so, it would use the $strategy object as a controller, and would pass the input from whatever the conditions are met to the strategic behavior controller. That, given the player could either:
1) help on determining the activities to be undertaken
2) use players interface to respond to whatever the action the strategical behavior has to be applied to.
Now, we could assume that there could be different strategies, that could be easily applied to any Player, given that the strategy classes implement the Strategy Interface. In this scenario, Strategy, would not need to know anything about the player. If only, the there are different type of players having different interfaces that would allow for different set of controls. But in that case, it would be wiser to setup extended classes for the particular player classes to implement the extra functionality that those players provide.
class Strategy {
private $player;
public usePlayer($player){
$this->player = $player;
}
public makeNextMove($input){
1) return array($a,$b);
2) return new Behavior_NextMove()->setTarget($a,$b);
3) $this->player->move($a, $b);
}
}
class Strategy_MegaPlayer extends Strategy {
public makeNextMove($input){
parent::makeNextMove($input);
$player->useMegaPlayerSpecialPower();
}
}
Alternatively, there could be centralized Strategy object, which, through configuration interface, based on the type of the player would decided on the proper actions and strategies, both based on the player type and the configuration of its.
but it seems that properly structured strategic support classes would benefit you most.
You should use getters/setters:
$strategy = new Strategy();
$player = new Player('Player 1', $strategy);
$strategy->setPlayer($player);
Using the ampersand (&
) prefix is unnecessary because objects pass by reference.
You could have the Player constructor pass $this
to the Strategy
object that was passed in by calling a setter on the Strategy
.
However, you might want to reconsider whether the Strategy
object really needs to point to the Player
object or whether the Player
object really needs to point to the Strategy
object. If those two classes are that tightly coupled, it's a code smell. Perhaps some functionality needs to be moved from one to the other.
精彩评论