I'm trying to move beyond inheritance based design, but I've been doing it like that for 6 years. My current hurdle is this: What's the simplest( or proper) way to update all my objects? Before, everything was a subclass of an Entity class. It was as simple as throwing them into ArrayList and iterating through them all, calling update() on each object. How does this translate in composition? Where the Entity class is within classes that can't be simply passed as each other.

for(Entity e : Entites) // I know this is bad practice, but good for an example
// e could be any subclass of entity

Since moving to a component - composition based system I can't do that, as each game object is it's own class. I can't make an ArrayList of my objects and call update on each of them. And as far as I know,

My game objects are setup like this

class Apple implements iEntity {
    Entity  innerEntity; 
    public Apple{
        innerEntity = new Entity(1); // 1 represents Apple as an identifier
    @override // From iEntity
    public void update(){

Because Apple no longer extends Entity, I can't load it into an ArrayList with Pears(example). All my other objects are similar. Id' rather not make a AppleManager class for Apples, a PearManager for Pears, and build functionality between them. That just seems messy.

Maybe I'm missing a few points, but that's why I'm here. Before this gets closed for some obscure reason, I googled the heck out of this. Each article explained the advantages of a system like this, but failed explaining beyond that. Thanks and advance dudes.

  • \$\begingroup\$ i strongly recommend to use an Enum instead of magic numbers: enum EntityType = {APPLE, PEAR}; and apply it to your Constructor: innerEntity = new Entity(EntityType.APPLE);... \$\endgroup\$ – Martin Frank Jun 6 '18 at 4:46

why don't you make lists of your interface?

List<iEntity> fruitList = ...; //list of interfaces
fruitList.add(new Apple()); //can be added because it implements iEntity
fruitList.add(new Pear()); //can be added because it implements iEntity

for (iEntity entity: fruitList){
    entity.update(); //each objects implements iEntity - no matter if pears or apples
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.