Interface issues, static classes

Currently, I am transferring 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.

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 use the Builder pattern to create 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 a GameObjectImpl using the builder and only have access to the Sprite interface?

+3
source share
3 answers

You can add another public class in the same package as Builders:

public final class Builders {

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

}
+2
source

Builder ? factory? , .

public final class SpriteBuilder {
  private final Foo foo;
  private int property = 0;

  public SpriteBuilder(Foo importantMandatoryValue) {
    this.foo = importantMandatoryValue;
  }

  public SpriteBuilder setProperty(int theProperty) {
    this.property = property;
    return this;
  }

  public Sprite build() {
    return new GameObjectImpl(foo, property);
  }
}

SpriteBuilder a GameObjectImplFactory... Sprite. , Sprite.

- public class GameObjectImplFactory implements Builder<Sprite>.

, : -).

0

, Builder generic... Builder.build() Sprite. build() T, GameObjectImpl

0

Source: https://habr.com/ru/post/1735362/


All Articles