Facade for PhysicsTickListeners

Is there any use case for adding a PhysicsTickListener more than once to a PhysicsSpace?



If not, addTickListener should check if the the given PhysicsTickListener is already in the list of ticklisteners and don’t add a listener more than once.



The problem I’m facing at the moment is, that I have a facade class that implements PhysicsTickListener and knows some Objects that implement PhysicsTickListener, too. At the moment I delegate the physicsTick and prePhysicsTick methods to all known objects of the facade, but I don’t know if that is the right solution. I think I should rather expose all known PhysicsTickListener’s, so that one could add them manually.



Variant 1

An example with delegation:

[java]

public class Facade implements PhysicsTickListener {



protected List<PhysicsTickListener> objects;



public Facade() {

objects = new ArrayList<PhysicsTickListener>();

// add some PhysicsTickListeners

// objects.add(…)

// …

}



public void prePhysicsTick(PhysicsSpace ps, float f) {

// do something



// delegate

for (PhysicsTickListener listener : objects) {

listener.prePhysicsTick(ps, f);

}

}



public void physicsTick(PhysicsSpace ps, float f) {

// do something



// delegate

for (PhysicsTickListener listener : objects) {

listener.physicsTick(ps, f);

}

}



}

[/java]



would then be used as:

[java]

BulletAppState bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

PhysicsSpace ps = bulletAppState.getPhysicsSpace();



Facade facade = new Facade();

ps.addTickListener(facade);

[/java]



Variant 2

An example for exposing the listeners instead:

[java]

public class Facade implements PhysicsTickListener {



protected List<PhysicsTickListener> objects;



public Facade2() {

objects = new ArrayList<PhysicsTickListener>();

// add some PhysicsTickListeners

// objects.add(…)

// …

}



public List<PhysicsTickListener> getContainedTickListeners() {

return objects;

}



public void prePhysicsTick(PhysicsSpace ps, float f) {

// do something

}



public void physicsTick(PhysicsSpace ps, float f) {

// do something

}



}

[/java]



would then be used as:

[java]

BulletAppState bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

PhysicsSpace ps = bulletAppState.getPhysicsSpace();



Facade facade = new Facade();

for (PhysicsTickListener listener : facade.getContainedTickListeners()) {

ps.addTickListener(listener);

}

ps.addTickListener(facade);

[/java]



The advantage of the first variant is, that it is easier to add it to the PhysicsSpace. The disadvantage is that just the Facade is directly called by the PhysicsSpace, all other listeners are just delegated.

The advantage of the second variant is, that all listeners are directly put into the PhysicsSpace. The problem with the second solution is, that a listener may be added more than once to a PhysicsSpace, or eventually it is not added at all. (Depends on the handling after getContainedTickListeners was called.)



Which one is better? Or is there an other solution for PhysicsTickListeners inside of a PhysicsTickListener?

As you want. I you want other functionality (like the checks you mentioned) you could have just one class as a listener where others are again registered in your own way so you can add whatever functionality you need yourself then, yeah.