我该如何重构这个方法? (春天+ java8)

时间:2018-03-27 21:58:30

标签: spring java-8

我正在尝试重构下面的Java类。我有一个方法,根据它所属的实例保存POJO(实体)。

以下代码仅显示3项服务,但总共有13项服务。 每个服务都调用一个单独的* RepositoryImpl。 例如,ActiviteService是一个接口,activiteService.create(activity)将调用该接口的实现。

@Autowired
private ActiviteService       activiteService;
@Autowired
private AdresseMsSanteService    adresseMsSanteService;
@Autowired
private AttributionParticuliereService     attributionParticuliereService;



private boolean sauvegarder(Object object, Long idIdPlay, String game,
    Integer gameIndex) {


    boolean isSaved = false;

    if (idIdPlay == null) {
        throw new IllegalArgumentException("IdPlay id is null");
    }

    if (object instanceof Activite) {
        Activite activite = (Activite) object;
        activite.setIdIdPlay(idIdPlay);
        if (this.isGameOn(activite, game, gameIndex)) {
            activiteService.create(activite);
            isSaved = true;
        }
    } else if (object instanceof AdresseMsSante) {
        AdresseMsSante adresseMsSante = (AdresseMsSante) object;
        adresseMsSante.setIdIdPlay(idIdPlay);
        if (this.isGameOn(adresseMsSante, game, gameIndex)) {
            adresseMsSanteService.create(adresseMsSante);
            isSaved = true;
        }
    } else if (object instanceof AttributionParticuliere) {
        AttributionParticuliere attributionParticuliere = (AttributionParticuliere) object;
        attributionParticuliere.setIdIdPlay(idIdPlay);
        if (this.isGameOn(attributionParticuliere, game, gameIndex)) {
            attributionParticuliereService.create(attributionParticuliere);
            isSaved = true;
        }
    } else if 

1 个答案:

答案 0 :(得分:2)

首先,我将创建一个代表您的游戏实体的界面。例如:

public interface GameEntity {
    void setIdIdPlay(Long idIdPlay);
}

之后,您将创建实现GameEntity接口的类:

@Entity
@Table
public class AdresseMsSante implements GameEntity {
    @Id
    Long idIdPlay;

    public void setIdIdPlay(Long idIdPlay) {
        this.idIdPlay = idIdPlay;
    }
}


@Entity
@Table
public class Activite implements GameEntity {
    @Id
    Long idIdPlay;

    public void setIdIdPlay(Long idIdPlay) {
        this.idIdPlay = idIdPlay;
    }
}

然后,创建一个通用存储库,它将保存每个游戏实体。

@Repository
public class Repo {
    @Autowired
    EntityManager entityManager;

    @Transactional
    public void save(GameEntity obj) {
        entityManager.merge(obj);
    }
}

最后,您的方法将是这样的:

 @Autowired
    Repo repo;

 private boolean sauvegarder(Object object, Long idIdPlay, String game,
                                Integer gameIndex) {
        boolean isSaved = false;
        if (idIdPlay == null) {
            throw new IllegalArgumentException("IdPlay id is null");
        }
        GameEntity gameEntity = (GameEntity) object;
        gameEntity.setIdIdPlay(idIdPlay);
        if(this.isGameOn(gameEntity, game, gameIndex)) {
            repo.save(gameEntity);
            isSaved = true;
        }
        return isSaved;
    }


    boolean isGameOn(GameEntity gameEntity,  String game, Integer gameIndex) {
        // do something
        return true;
    }