Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interfaces, static classes problem

Tags:

java

I am currently moving all my Game code to another package so that I can simply reuse it when I create another similar game.

I am having some problems with this though.

public interface Sprite {
...
}

abstract class AbstractSprite implements Sprite {
...
}

public interface Builder<T> {
    public T build();
}

class GameObjectImpl extends AbstractSprite {
    public static class GameObjectBuilder implements Builder<GameObjectImpl> {
    ...
    }
}

I am using a Builder Pattern to create my GameObjectImpl objects. However, the client (the person using my game engine) will only have access to the Sprite interface.

How can I get the client to create GameObjectImpl using builder and only having access to the Sprite Interface?

like image 302
jax Avatar asked Feb 21 '26 04:02

jax


1 Answers

You could add one more publicly visible class in the same package called Builders:

public final class Builders {

    public static Builder<? extends Sprite> newGameObjectBuilder() {
        return new GameObjectImpl.GameObjectBuilder();
    }

}
like image 200
Michael Barker Avatar answered Feb 22 '26 17:02

Michael Barker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!