我刚刚开始学习Guice,但是我已经遇到了问题。我有一个实现PlayerFactory的接口BlackjackPlayer

PlayerFactory.java

public interface PlayerFactory {
    Player createPlayer(String name);
    Player createPlayer(String name, boolean isDealer);
}


BlackjackPlayer.java

public class BlackjackPlayer implements PlayerFactory {
    private PointsCalculator pointsCalculator;

    public BlackjackPlayer(){
        pointsCalculator = new BlackjackPointsCalculator();
    }

    @Override
    public Player createPlayer(String name) {
        return new Player(pointsCalculator, name);
    }

    @Override
    public Player createPlayer(String name, boolean isDealer) {
        return new Player(pointsCalculator, name, isDealer);
    }
}


播放器类

public class Player{
    private PointsCalculator pointsCalculator;
    private List<Card> cardsInHand;
    private Integer points;
    private String name;
    private boolean isDealer;
    private boolean endedTurn;

    @AssistedInject
    public Player(PointsCalculator blackjackPointsCalculator, String name){
        pointsCalculator = blackjackPointsCalculator;
        cardsInHand = new ArrayList<>();
        points = 0;
        this.name = name;
        isDealer = false;
        endedTurn = false;
    }

    @AssistedInject
    public Player(PointsCalculator blackjackPointsCalculator, String name, boolean isDealer){
        pointsCalculator = blackjackPointsCalculator;
        cardsInHand = new ArrayList<>();
        points = 0;
        this.name = name;
        this.isDealer = isDealer;
        endedTurn = false;
    }

    public void addCardToHand(Card card) {
        cardsInHand.add(card);
        updatePoints();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Player)) return false;
        Player player = (Player) o;
        return getPoints() == player.getPoints() &&
            isDealer() == player.isDealer() &&
            isEndedTurn() == player.isEndedTurn() &&
            Objects.equals(pointsCalculator, player.pointsCalculator) &&
            Objects.equals(getCardsInHand(), player.getCardsInHand()) &&
            Objects.equals(getName(), player.getName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(pointsCalculator, getCardsInHand(), getPoints(), getName(), isDealer(), isEndedTurn());
    }

    public void updatePoints() {
        points = pointsCalculator.calculatePoints(cardsInHand);
    }

    public List<Card> getCardsInHand(){
        return cardsInHand;
    }

    public Integer getPoints(){
        return points;
    }

    public String getName(){
        return name;
    }

    public boolean isDealer() {
        return isDealer;
    }

    public boolean isEndedTurn() {
        return endedTurn;
    }

    public void setName(String name){
        this.name = name;
    }

    public void setDealer(boolean isDealer){
        this.isDealer = isDealer;
    }

    public void setEndedTurn(boolean endedTurn){
        this.endedTurn = endedTurn;
    }
}


我想使用Guice辅助注入来创建Player。以前,我这样做如下:

install(new FactoryModuleBuilder().build(PlayerFactory.class));


我知道这是错误的方式,因为我收到错误消息:

1) com.github.blackjack.model.Player has @AssistedInject constructors, but none of them match the parameters in method com.github.blackjack.factory.PlayerFactory.createPlayer().  Unable to create AssistedInject factory.
while locating com.github.blackjack.model.Player
at com.github.blackjack.factory.PlayerFactory.createPlayer(PlayerFactory.java:1)

2) com.github.blackjack.model.Player has @AssistedInject constructors, but none of them match the parameters in method com.github.blackjack.factory.PlayerFactory.createPlayer().  Unable to create AssistedInject factory.
while locating com.github.blackjack.model.Player
at com.github.blackjack.factory.PlayerFactory.createPlayer(PlayerFactory.java:1)


我试图添加构造函数Player(String name)Player(String name, boolean isDealer),但没有帮助。有人知道我该怎么做才能解决问题?

提前致谢!

最佳答案

您需要在注入者参数上使用@Assisted批注:

PlayerFactory.java

public interface PlayerFactory {
  Player createPlayer(String name);
  Player createPlayer(String name, boolean isDealer);
}


BlackjackPlayer.java(将其从工厂更改为实际播放器)

public class BlackjackPlayer implements Player {
  private final PointCalculator pointsCalculator;
  private final String name;
  private final boolean isDealer;
  @AssistedInject BlackjackPlayer(PointCalculator pointsCalculator, @Assisted String name) {
    this.pointsCalculator = pointsCalculator;
    this.name = name;
    this.isDealer = false;
  }
  @AssistedInject BlackjackPlayer(PointCalculator pointsCalculator, @Assisted String name, @Assisted boolean isDealer) {
    this.pointsCalculator = pointsCalculator;
    this.name = name;
    this.isDealer = isDealer;
  }
}


并使用以下模块:

install(new FactoryModuleBuilder()
 .implement(Player.class, BlackjackPlayer.class)
 .build(PlayerFactory.class)
);

10-08 06:46