I often bump on problems like the ones in these examples :
The Game
class instantiate the class GamePhysics
.
But I need to have access to some Game
properties inside GamePhysics
.
I would say the straightforward design would be to have a GamePhysics(Game game)
constructor and call it inside Game
class using this
.
//Game class code
physics = new GamePhysics(this);
This simple way to solve the problem seams to be poor up to me.
Are there patterns that handle these kind of issues that would be more elaborated ?
Another example (which is the one I am currently bumping on) : Orchestrator
class contains a list of ActionBase
with different derived classes Action1
, Action2
... Depending on logics in action methods, Orchestrator
must register the action to a new list. Using the instance of Orchestrator
referenced inside ActionBase
, I could call orch.Register(this)
inside ActionBase
code.
class Orchestrator
{
List<ActionBase> startActions;
List<ActionBase> registeredActions;
public void Register(ActionBase action)
{
//some custom logic related to orchestrator
//...
registerdActions.Add(action);
}
//instantiate actions
public void CreateStartActions()
{
startActions.Add(new Action1(this)); //Do I need to have 'this' ?
startActions.Add(new Action2(this));
startActions.Add(new Action1(this));
}
}
And :
class ActionBase
{
private Orchestrator orch;
public ActionBase(Orchstrator orchestrator) //I am unhappy with this signature
{
orch = orchestrator;
//...
}
//...
//somewhere in the relevant place
orch.Register(this);
}
It seams odd to me that what is "inside" points towards what contains it. Is there a turnaround ?
Aucun commentaire:
Enregistrer un commentaire